rust-ux / uX

Non standard integer types like `u7`, `u9`, `u10`, `u63`, `i7`, `i9`
Apache License 2.0
121 stars 37 forks source link

incr: added type dispatch #62

Open explocion opened 1 year ago

explocion commented 1 year ago

I was trying to do some meta-programming involving ux, but I found I couldn't connect const generics to ux very well and thus I created this pull request.

bbaldino commented 1 year ago

Out of curiosity, could you include some examples of the type of code this enables?

explocion commented 1 year ago

Of course! I was using ux to write embedded system traits, e.g., OutputPins<const N: usize>, which is a group of N output pins. The trait is defined as

pub trait OutputPins<const N: usize> {
  type Error;
  fn write_pins(&mut self, state: ??) -> Result<(), Self::Error>;
}

Then, what is a type safe way to fill in the ?? part? I come up with ux, and with this pull request, I can write

pub trait OutputPins<const N: usize> where Const<N>: ToUnsignedType {
  type Error;
  fn write_pins(&mut self, state: Unsigned<N>) -> Result<(), Self::Error>;
}