Skip to content

Commit

Permalink
allow setting field byte order
Browse files Browse the repository at this point in the history
  • Loading branch information
vhdirk committed Nov 30, 2022
1 parent f35317e commit 6ee9290
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
26 changes: 22 additions & 4 deletions impl/src/bitfield/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl BitfieldStruct {
#invalid_bit_pattern

if invalid_bit_pattern {
return ::core::result::Result::Err(::modular_bitfield_msb::error::InvalidBitPattern::new(bytes))
return ::core::result::Result::Err(::modular_bitfield::error::InvalidBitPattern::new(bytes))
}

::core::result::Result::Ok(Self {
Expand Down Expand Up @@ -594,7 +594,7 @@ impl BitfieldStruct {

let bf_read_be = quote_spanned!(span=>
let __bf_read: <#ty as ::modular_bitfield::Specifier>::Bytes = {
::modular_bitfield::private::read_specifier::<#ty>(&self.bytes[..], #offset)
::modular_bitfield::private::read_specifier_be::<#ty>(&self.bytes[..], #offset)
};
);

Expand All @@ -603,7 +603,7 @@ impl BitfieldStruct {
let __bf_base_bits: ::core::primitive::usize = 8usize * ::core::mem::size_of::<<#ty as ::modular_bitfield::Specifier>::Bytes>();
let __bf_spec_bits: ::core::primitive::usize = <#ty as ::modular_bitfield::Specifier>::BITS;
let __bf_read: <#ty as ::modular_bitfield::Specifier>::Bytes = {
::modular_bitfield::private::read_specifier::<#ty>(&self.bytes[..], #offset)
::modular_bitfield::private::read_specifier_le::<#ty>(&self.bytes[..], #offset)
} << if <#ty as ::modular_bitfield::Specifier>::STRUCT { __bf_base_bits - __bf_spec_bits } else { 0 };
__bf_read
};
Expand Down Expand Up @@ -736,6 +736,24 @@ impl BitfieldStruct {
}, None => bf_raw_val_native
};

let write_specifier_be = quote_spanned!(span=> ::modular_bitfield::private::write_specifier_be::<#ty>(&mut self.bytes[..], #offset, __bf_raw_val););
let write_specifier_le = quote_spanned!(span=> ::modular_bitfield::private::write_specifier_le::<#ty>(&mut self.bytes[..], #offset, __bf_raw_val););
let write_specifier_native = quote_spanned!(span =>
#[cfg(target_endian = "big")]
#write_specifier_be

#[cfg(target_endian = "little")]
#write_specifier_le
);

let write_specifier = match &config.endian {
Some(value) => match value.value {
Endian::Big => write_specifier_be,
Endian::Little => write_specifier_le,
Endian::Native => write_specifier_native
}, None => write_specifier_native
};

let setters = quote_spanned!(span=>
#[doc = #with_docs]
#[inline]
Expand Down Expand Up @@ -790,7 +808,7 @@ impl BitfieldStruct {
return ::core::result::Result::Err(::modular_bitfield::error::OutOfBounds)
}

::modular_bitfield::private::write_specifier::<#ty>(&mut self.bytes[..], #offset, __bf_raw_val);
#write_specifier
::core::result::Result::Ok(())
}
);
Expand Down
6 changes: 4 additions & 2 deletions src/private/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ pub mod static_assertions {
pub use self::{
array_bytes_conv::ArrayBytesConversion,
proc::{
read_specifier,
write_specifier,
read_specifier_be,
write_specifier_be,
read_specifier_le,
write_specifier_le,
},
push_pop::{
PopBuffer,
Expand Down
37 changes: 26 additions & 11 deletions src/private/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ where

#[doc(hidden)]
#[inline]
pub fn read_specifier<T>(bytes: &[u8], offset: usize) -> <T as Specifier>::Bytes
pub fn read_specifier_le<T>(bytes: &[u8], offset: usize) -> <T as Specifier>::Bytes
where
T: Specifier,
PushBuffer<T::Bytes>: Default + PushBits,
{
let end = offset + <T as Specifier>::BITS;
// LSB

let ls_byte = offset / 8; // compile-time
let ms_byte = (end - 1) / 8; // compile-time
let lsb_offset = offset % 8; // compile-time
Expand Down Expand Up @@ -59,8 +57,17 @@ where
}
}

// MSB
buffer.into_bytes()
}

#[doc(hidden)]
#[inline]
pub fn read_specifier_be<T>(bytes: &[u8], offset: usize) -> <T as Specifier>::Bytes
where
T: Specifier,
PushBuffer<T::Bytes>: Default + PushBits,
{
let end = offset + <T as Specifier>::BITS;
let ms_byte = offset / 8; // compile-time
let ls_byte = (end - 1) / 8; // compile-time
let msb_offset = offset % 8; // compile-time
Expand Down Expand Up @@ -91,14 +98,12 @@ where
buffer.push_bits(lsb_offset as u32, bytes[ls_byte] >> (8 - lsb_offset));
}
}
// END

buffer.into_bytes()
}

#[doc(hidden)]
#[inline]
pub fn write_specifier<T>(
pub fn write_specifier_le<T>(
bytes: &mut [u8],
offset: usize,
new_val: <T as Specifier>::Bytes,
Expand Down Expand Up @@ -151,7 +156,20 @@ pub fn write_specifier<T>(
}
}
}
// MSB
}


#[doc(hidden)]
#[inline]
pub fn write_specifier_be<T>(
bytes: &mut [u8],
offset: usize,
new_val: <T as Specifier>::Bytes,
) where
T: Specifier,
PopBuffer<T::Bytes>: PopBits,
{
let end = offset + <T as Specifier>::BITS;
let ms_byte = offset / 8; // compile-time
let ls_byte = (end - 1) / 8; // compile-time
let msb_offset = offset % 8; // compile-time
Expand Down Expand Up @@ -197,7 +215,4 @@ pub fn write_specifier<T>(
bytes[ms_byte] = stays_same | overwrite;
}
}

// END

}

0 comments on commit 6ee9290

Please sign in to comment.