dtolnay/itoa

itoa::Integer trait bounds

henninglive opened this issue · 3 comments

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.

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

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.

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>(...) {}