danlehmann / arbitrary-int

A modern and lightweight implementation of arbitrary integers for Rust
MIT License
32 stars 13 forks source link

le and be #9

Closed hecatia-elegua closed 1 year ago

hecatia-elegua commented 1 year ago

https://doc.rust-lang.org/src/core/num/uint_macros.rs.html to_le/to_be from_le/from_be

After reading issues like this: https://github.com/Robbepop/modular-bitfield/issues/46 I wondered how this could be handled by us. Adding these 4 functions would mirror the primitive numbers and they afaik just need to call their UnderlyingType's functions.

hecatia-elegua commented 1 year ago

I've tried implementing it but need to test it with bitfields first. The only difficulty is that we of course have some unfilled bits:

/// u24
pub const fn from_ne_bytes(bytes: [u8; 3]) -> Self { //mem::size_of::<Self>() can't be used, would be 4
    unsafe { mem::transmute(bytes) } //can't be used, we're 4 bytes wide, so we need to add a 0 byte
}

swap_bytes works in a similar fashion.

danlehmann commented 1 year ago

Would such functions even be well-defined for everything? What does it mean for a u19 to be be vs le?

I can see how it makes sense in bitfield, but in arbitrary_int, should we limit this to multiples of 8?

hecatia-elegua commented 1 year ago

I noticed that as well while implementing and proceeded to do multiples of 8.

hecatia-elegua commented 1 year ago

This is still lying around somewhere, will try to add this soon.

hecatia-elegua commented 1 year ago

"soon"

danlehmann commented 1 year ago

Working on this. Have swap_bytes and to_le_bytes (and ne and be) ready. from_ne_bytes is slightly more tricky, macro wise. Will upload a patch shortly

danlehmann commented 1 year ago

https://github.com/danlehmann/arbitrary-int/pull/17 https://github.com/danlehmann/arbitrary-int/pull/16