From 4a2b8e3c5224b70c7c975716d612056f6fd7523e Mon Sep 17 00:00:00 2001 From: evgeniy Date: Tue, 18 Oct 2016 19:09:36 +0200 Subject: [PATCH] cat: conditional compilation added --- src/cat/cat.rs | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/cat/cat.rs b/src/cat/cat.rs index 90db91ee8..d1e003576 100644 --- a/src/cat/cat.rs +++ b/src/cat/cat.rs @@ -321,22 +321,32 @@ impl<'a, W: Write> UnsafeWriter<'a, W> { } // This method was used in Write::write only. It must not be called in the correct cat code -// and is placed in the bottleneck place, so it is not used anymore. -// Uncomment this and code in Write::write for more convenient testing of a new cat version. -//#[inline(never)] -//fn fail() -> ! { -// panic!("assertion failed"); -//} +// and is placed in the bottleneck place, so it is not used anymore in release build. +// In debug buld it is used to support bug finding +#[cfg(debug_assertions)] +#[inline(never)] +fn fail() -> ! { + panic!("assertion failed"); +} impl<'a, W: Write> Write for UnsafeWriter<'a, W> { + #[cfg(debug_assertions)] + fn write(&mut self, buf: &[u8]) -> Result { + let dst = &mut self.buf[self.pos..]; + let len = buf.len(); + if len > dst.len() { + fail(); + } + unsafe { copy_nonoverlapping(buf.as_ptr(), dst.as_mut_ptr(), len) } + self.pos += len; + Ok(len) + } + + // a condition check excluded because it is false for current and every correct code + // see fail method comment for more information + #[cfg(not(debug_assertions))] fn write(&mut self, buf: &[u8]) -> Result { - //let dst = &mut self.buf[self.pos..]; let len = buf.len(); - // the condition is false for current code and every correct code - // see fail method comment for more information - // if len > dst.len() { - // fail(); - // } unsafe { copy_nonoverlapping(buf.as_ptr(), self.buf[self.pos..].as_mut_ptr(), len) } self.pos += len; Ok(len)