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
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 24 additions & 0 deletions bilge-impl/src/bitsize_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,29 @@ fn generate_common(ir: ItemIr, arb_int: &TokenStream) -> TokenStream {
const BITS: usize = <Self::ArbitraryInt as Bitsized>::BITS;
const MAX: Self::ArbitraryInt = <Self::ArbitraryInt as Bitsized>::MAX;
}
impl #name {
pub fn to_ne_bytes(&self) -> [u8; (<Self as Bitsized>::BITS + 7) / 8] {
// 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")]
{
// = 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

}
}
}
}
}
23 changes: 21 additions & 2 deletions examples/nested_tuples_and_arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// you can use the "Expand glob import" command on
// use bilge::prelude::*;
// but still need to add Bitsized, Number yourself
use bilge::prelude::{bitsize, u1, u18, u2, u39, Bitsized, DebugBits, DefaultBits, FromBits, Number, TryFromBits};
use bilge::prelude::{bitsize, u1, u18, u2, u39, BinaryBits, Bitsized, DebugBits, DefaultBits, FromBits, Number, TryFromBits};

// This file basically just informs you that yes, combinations of different nestings work.
// also see `tests/struct.rs`

#[bitsize(39)]
#[derive(FromBits, DebugBits, PartialEq)]
#[derive(FromBits, DebugBits, PartialEq, BinaryBits)]
struct Mess {
field1: (u1, (u2, u8), u1),
array: [[InnerTupleStruct; 2]; 2],
Expand Down Expand Up @@ -123,4 +123,23 @@ fn main() {

let default = UnfilledEnumMess::default();
println!("{default:?}");

println!("{:b}", mess);
println!("{:?}", mess.to_ne_bytes());
println!(
"{:?} == {:?} == {:?}",
core::mem::size_of::<Mess>(),
core::mem::size_of::<[u8; (<Mess as Bitsized>::BITS + 7) / 8]>(),
core::mem::size_of::<[u8; (<<Mess as Bitsized>::ArbitraryInt as Number>::UnderlyingType::BITS as usize + 7) / 8]>()
);
// 1u8.to_ne_bytes() -> unsafe { mem::transmute(self) }
const SIZE: usize = core::mem::size_of::<Mess>();
let mut full = unsafe { core::mem::transmute_copy::<Mess, [u8; SIZE]>(&mess) };
println!("{:?}", full);
full.rotate_left(3);
println!("{:?}", full);
let part: [u8; (<Mess as Bitsized>::BITS + 7) / 8] = full[0..((<Mess as Bitsized>::BITS + 7) / 8)].try_into().unwrap();
println!("{:?}", part);

println!("{:?}", default.to_ne_bytes());
}
3 changes: 3 additions & 0 deletions examples/readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct InterruptSetEnables([bool; 32]);

#[bitsize(32)]
#[derive(FromBits, Debug, PartialEq)]
#[repr(u32)]
enum Subclass {
Mouse,
Keyboard,
Expand Down Expand Up @@ -84,6 +85,8 @@ fn main() {
assert_eq!(3, num);
assert_ne!(42, num);

println!("{:?}", Subclass::from(42).to_ne_bytes());

assert_eq!(Subclass2::Reserved(3), Subclass2::from(3));
assert_eq!(Subclass2::Reserved(42), Subclass2::from(42));
let num = u32::from(Subclass2::from(42));
Expand Down