dtolnay / itoa

Fast function for printing integer primitives to a decimal string
Apache License 2.0
308 stars 37 forks source link

itoa::Integer trait bounds #13

Closed henninglive closed 5 years ago

henninglive commented 7 years ago

The Integer trait should probably have some bounds making to more convenient to use in generic code. I suggest adding at least Sized, Clone and Copy. We could also add math ops traits, but if you need this you should probably be using the num crate. For the other traits implemented by integers, I don’t know. The std::fmt traits might make sense, but they are not no-std if we want support that later.

dtolnay commented 7 years ago

Can you give an example of generic code you would want to write where this would be helpful?

henninglive commented 7 years ago

Base10 serialization with generic integers. You would write your generic serializing function on top of iota::Integer::write, but you might need the integer to be Sized or Copy to do what you want. If Integer had those bounds, you wouldn’t need to add the bounds yourself. I don’t see any harm in adding bounds, the trait is only supposed to be implemented by integers.

dtolnay commented 5 years ago

I would prefer to keep this without the additional bounds. The itoa::Integer trait isn't intended to model the general concept of a primitive integer, but only that this is a type printable by itoa. Downstream code can add their own additional trait bounds that they require.

trait DownstreamInteger: itoa::Integer + Copy {}
impl<T> DownstreamInteger for T where T: itoa::Integer + Copy {}

fn f<T: DownstreamInteger>(...) {}