-
Notifications
You must be signed in to change notification settings - Fork 24
Conversation
Btw tests doesn't pass in |
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.
It seems that you abuse fact that compiler doesn't check for overflow in release
I don't understand this. So why they suddenly started failing for your branch and they work on master?
src/uint.rs
Outdated
assert_eq!(U256::from("f00000000000000000000001adbdd6bd6ff027485484b97f8a6a4c7129756dd1").leading_zeros(), 0); | ||
assert_eq!(U256::from("0000000000000000000000000000000000000000000000000000000000000001").leading_zeros(), 255); | ||
assert_eq!(U256::from("0000000000000000000000000000000000000000000000000000000000000000").leading_zeros(), 256); | ||
assert_eq!("000000000000000000000001adbdd6bd6ff027485484b97f8a6a4c7129756dd1".parse::<U256>().unwrap().leading_zeros(), 95); |
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.
why would you change this? imo it's less readable
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.
Because into
shouldn't panic. Current implementation cannot convert any string into U256. This is what parse
is for. See comment below.
src/uint.rs
Outdated
@@ -955,13 +956,6 @@ macro_rules! construct_uint { | |||
Ok(()) | |||
} | |||
} | |||
|
|||
#[cfg(feature="std")] | |||
impl From<&'static str> for $name { |
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.
there is no reason to remove this
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.
If you cannot convert into type without panic then you shoulnd't implement From
.
https://doc.rust-lang.org/std/convert/trait.From.html
Note: this trait must not fail.
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.
It's a convenience wrapper used only in tests (that's why it requires 'static
lifetime). We can argue here whether it's a good practice or not, but I won't accept this change cause it is used thousands of times in tests utilizing this library.
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.
Ok, then it could be hidden under #cfg[test]
? Or it's better to provide some helper trait, if you want to save 6 chars.
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.
panic is not fail, it's panic (unlike typed Result<T, E> which is fail)
[cfg(test)]
does not propagate through dependencies, and those tests mentioned are not inside this repo exclusively
and anyway, @Pzixel , how is this related to the pr topic?
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.
panic is not fail, it's panic (unlike typed Result<T, E> which is fail)
I'm pretty sure that this quote says conversion must always succeed. If you disagree, what do you think they added this note in the documentation and what does it mean? I'd really like ot make my code better, and such a basic concept worth discussing it.
how is this related to the pr topic?
I was looking at all things with std
attribute to deretmine if they could work in no-std environment. And then I realised that this method couldn't work anywhere.
I probably should create another PR for it, but I strongly think that it should be done. I understand that in may require changes in depended crates, but it's really goes against rust guidelines (as I understand the docs).
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.
@debris @NikVolf guys, if you need a convenience wrapper used only in tests, you can make any wrapper you'd like, just call it parse_unwrap
or anything else, not an ecosystem-wide method with defined semantics.
There's nothing preventing a user of your library from calling .into()
that would unexpectedly fail.
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.
@mkpankov not unexpectedly, because it will fail always or not fail always (since there is no dynamic behaviour, input is static)
src/uint.rs
Outdated
} | ||
|
||
write!(f, "{}", s) | ||
let s = unsafe {::core::str::from_utf8_unchecked(&buf[i..])}; |
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.
unjustified unsafe
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.
String of numbers is guaranteed to be a valid UTF8 string. I can leave a comment though.
src/uint.rs
Outdated
@@ -430,7 +430,7 @@ macro_rules! construct_uint { | |||
use core::cmp; | |||
use rustc_hex::ToHex;; | |||
|
|||
if self.is_zero() { return "0".to_owned(); } // special case. | |||
if self.is_zero() { return "0".to_owned(); } // construct_uintspecial case. |
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.
it should be reverted or there should be a space between uint and special
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.
this will be reverted. Just a typo.
|
Now also fixes #1 (MAX was useful to test against). |
looks good enough now to me, @debris ? |
isn't it a bit outdated for the nightly though? |
All tests pass on 2018-08-01 |
What do you mean? |
Includes parsing fixes as well.
Fixes #31
Fixes #1