Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

no_std support for formatting #43

Merged
merged 11 commits into from
Aug 3, 2018

Conversation

Pzixel
Copy link
Contributor

@Pzixel Pzixel commented Jul 20, 2018

Includes parsing fixes as well.

Fixes #31
Fixes #1

@Pzixel
Copy link
Contributor Author

Pzixel commented Jul 20, 2018

Btw tests doesn't pass in debug mode. It seems that you abuse fact that compiler doesn't check for overflow in release. How could I change behvior to make it work? We either must write tests without overflow or make it wrapping explicitely.

Copy link
Contributor

@debris debris left a 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);
Copy link
Contributor

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

Copy link
Contributor Author

@Pzixel Pzixel Jul 22, 2018

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 {
Copy link
Contributor

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

Copy link
Contributor Author

@Pzixel Pzixel Jul 22, 2018

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.

Copy link
Contributor

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.

Copy link
Contributor Author

@Pzixel Pzixel Jul 23, 2018

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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).

Copy link

@mkpankov mkpankov Jul 23, 2018

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.

Copy link
Contributor

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..])};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unjustified unsafe

Copy link
Contributor Author

@Pzixel Pzixel Jul 22, 2018

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.
Copy link
Contributor

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

Copy link
Contributor Author

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.

@Pzixel
Copy link
Contributor Author

Pzixel commented Jul 23, 2018

I don't understand this. So why they suddenly started failing for your branch and they work on master?

Because, if you check your travis file you see that all rests are executed in release mode. I don't think it's just ad coincendence I found my mistake. Sorry about that.

@Pzixel
Copy link
Contributor Author

Pzixel commented Jul 23, 2018

Now also fixes #1 (MAX was useful to test against).

@Pzixel Pzixel changed the title WIP: no_std support for formatting no_std support for formatting Jul 27, 2018
@NikVolf
Copy link
Contributor

NikVolf commented Aug 1, 2018

looks good enough now to me, @debris ?

@NikVolf
Copy link
Contributor

NikVolf commented Aug 1, 2018

isn't it a bit outdated for the nightly though?

@Pzixel
Copy link
Contributor Author

Pzixel commented Aug 1, 2018

All tests pass on 2018-08-01

@debris
Copy link
Contributor

debris commented Aug 3, 2018

isn't it a bit outdated for the nightly though?

What do you mean?

@debris debris merged commit 91f6c7a into paritytech:master Aug 3, 2018
@Pzixel Pzixel deleted the feature/no_std_format branch August 3, 2018 11:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

std::fmt::Display not implemented U*::max() function
4 participants