New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove calls to debug_assert #953
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,7 +140,7 @@ impl From<psbt::Error> for Error { | |
pub fn serialize<T: Encodable + ?Sized>(data: &T) -> Vec<u8> { | ||
let mut encoder = Vec::new(); | ||
let len = data.consensus_encode(&mut encoder).expect("in-memory writers don't error"); | ||
debug_assert_eq!(len, encoder.len()); | ||
assert_eq!(len, encoder.len()); | ||
encoder | ||
} | ||
|
||
|
@@ -240,7 +240,7 @@ macro_rules! decoder_fn { | |
($name:ident, $val_type:ty, $readfn:ident, $byte_len: expr) => { | ||
#[inline] | ||
fn $name(&mut self) -> Result<$val_type, Error> { | ||
debug_assert_eq!(::core::mem::size_of::<$val_type>(), $byte_len); // size_of isn't a constfn in 1.22 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why remove this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool. I'm no macro expert but is this as good or am I missing something?
This works with Rust 1.29 as well. ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OoO I didn't think that this can be done in 1.29! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to double check, is the simplification of the original [(); 0 - !{ const ASSERT: bool = $x; ASSERT } as usize] = [] to [(); 0 - !$x as usize] = [] ok? I'm going to force push this change but wanted to explicitly double check with you. Thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes :) |
||
const_assert!(::core::mem::size_of::<$val_type>() == $byte_len); | ||
let mut val = [0; $byte_len]; | ||
self.read_exact(&mut val[..]).map_err(Error::Io)?; | ||
Ok(endian::$readfn(&val)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,7 +78,7 @@ impl Encodable for CommandString { | |
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, io::Error> { | ||
let mut rawbytes = [0u8; 12]; | ||
let strbytes = self.0.as_bytes(); | ||
debug_assert!(strbytes.len() <= 12); | ||
assert!(strbytes.len() <= 12); | ||
rawbytes[..strbytes.len()].copy_from_slice(strbytes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slicing already performs this check. OT: maybe we could serialize the slice directly and fill in extra zeroes to avoid a bunch of copies. Something for opt nerds. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will revisit this one after other discussion no the PR resolves. Thanks. |
||
rawbytes.consensus_encode(s) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -429,7 +429,7 @@ impl TaprootBuilder { | |
} | ||
// Every iteration of the loop reduces the node_weights.len() by exactly 1 | ||
// Therefore, the loop will eventually terminate with exactly 1 element | ||
debug_assert_eq!(node_weights.len(), 1); | ||
assert_eq!(node_weights.len(), 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opinion: debug was fine here. |
||
let node = node_weights.pop().expect("huffman tree algorithm is broken").1; | ||
Ok(TaprootBuilder{branch: vec![Some(node)]}) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opinion:
debug_assert_eq
was fine here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FTR this is one of the ones that @TheBlueMatt alluded to in saying this is really a test of the
consensus_encode
function and as such belongs in a test not here in a debug build.re
debug_assert
vsassert
, and I'm just learning this this week, I have been attempting to follow the Rust docs which state thatdebug_assert
should only be used if the code was profiled andassert
was found to be a bottle neck. Do you disagree with this @Kixunil?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I like that line? I mean I'm not sure why we should be crashing the users' application because we're doing something wrong. Its not really a big deal, like hopefully this stuff is all right and we wont hit it anyway, but I've never heard of "only use asserts if they're slow" - coming from C, -DNDEBUG is a thing that (usually) disables all assert()s.