Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add byte conversions #72

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions bilge-impl/src/bitsize_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,32 +233,26 @@ fn generate_common(ir: ItemIr, arb_int: &TokenStream) -> TokenStream {
}
impl #name {
pub fn to_ne_bytes(&self) -> [u8; (<Self as Bitsized>::BITS + 7) / 8] {
//this works on little endian, since zeroes are cut out (i.e. u40 has 3 bytes left until u64 but they are cut out)
// 0 0 0 [1 1 1 1 1]
//but not on big endian, since it still copies from the back
// 1 1 1 [1 1 0 0 0]
// unsafe { core::mem::transmute_copy::<Self, [u8; (<Self as Bitsized>::BITS + 7) / 8]>(self) }

//this doesn't work for non-repr enums
// let mut full = unsafe { core::mem::transmute_copy::<Self, [u8; core::mem::size_of::<Self>()]>(self) };

//this doesn't work for non-repr enums either, since `Self > Dst` is still getting size checked in `transmute_copy`
//and `Self` is 1 byte even if bitsize(32)
// let mut full = unsafe {
// core::mem::transmute_copy::<Self, [u8; (<<Self as Bitsized>::ArbitraryInt as Number>::UnderlyingType::BITS as usize) / 8]>(self)
// };

//so, let's use the first one since it's readable:
// u40, as 8 bytes
let mut full = unsafe { core::mem::transmute_copy::<Self, [u8; core::mem::size_of::<Self>()]>(self) };
// u40 = u64 = 8 bytes
const ARB_INT_BYTES: usize = core::mem::size_of::<#name>();
// u40 = (40 + 7) / 8 = 5 bytes
const MIN_BYTES: usize = (<#name as Bitsized>::BITS + 7) / 8;

// u40, as 8 bytes
let mut full = unsafe { core::mem::transmute_copy::<Self, [u8; ARB_INT_BYTES]>(self) };
// u40, as 5 bytes
#[cfg(target_endian = "big")]
{
let size_difference = core::mem::size_of::<Self>() - ((<Self as Bitsized>::BITS + 7) / 8);
full.rotate_left(size_difference);
// = 3
let size_difference = ARB_INT_BYTES - MIN_BYTES;
// 0 0 0 [1 1 1 1 1]
return full[size_difference..].try_into().unwrap()
}
#[cfg(target_endian = "little")]
{
// [1 1 1 1 1] 0 0 0
return full[..MIN_BYTES].try_into().unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this just transmute_copy(self)? because it's already in little endian?

Copy link
Owner Author

Choose a reason for hiding this comment

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

that's true, that's also how the impl started.
though it's a bit more explicit like this, but I might change it back when doing the other methods

}
hecatia-elegua marked this conversation as resolved.
Show resolved Hide resolved
full[0..((<Self as Bitsized>::BITS + 7) / 8)].try_into().unwrap()
}
}
}
Expand Down