-
Notifications
You must be signed in to change notification settings - Fork 55
[WIP/proof-of-concept] Convoluted little-endian support #1555
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,7 @@ pub trait FixedSize: Sized { | |
|
|
||
| /// we hide this trait; it isn't part of the public API, and this clarifies | ||
| /// the guarantee that it is only implemented for [u8; N] | ||
| mod sealed { | ||
| pub(super) mod sealed { | ||
| /// A trait representing any fixed-size big-endian byte array. | ||
| /// | ||
| /// This is only used in `Scalar`, as a way of expressing the condition that the | ||
|
|
@@ -68,6 +68,21 @@ mod sealed { | |
| } | ||
| } | ||
|
|
||
| /// A trait for types that contain a byte slice which can be decoded to a scalar value. | ||
| pub trait BytesWrapper: Sized { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we keep this I would prefer to call this something like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other thing that this type encodes is that the underlying byte slice doesn't have the same alignment requirements as the scalar value it decodes to. Not sure how important that is to mention, though. |
||
| type Inner: Sized; | ||
| /// Attempt to construct a new raw value from this slice. | ||
| /// | ||
| /// This will fail if `slice.len() != T::RAW_BYTE_LEN`. | ||
| fn from_slice(slice: &[u8]) -> Option<Self>; | ||
|
|
||
| /// Get the scalar value of the bytes contained in this byte wrapper. | ||
| fn get(&self) -> Self::Inner; | ||
|
|
||
| /// Set the value, overwriting the bytes. | ||
| fn set(&mut self, value: Self::Inner); | ||
| } | ||
|
|
||
| /// A wrapper around raw big-endian bytes for some type. | ||
| #[derive(Clone, Copy, PartialEq, Eq, Hash)] | ||
| #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
|
|
@@ -83,29 +98,32 @@ unsafe impl<T> bytemuck::Zeroable for BigEndian<T> where T: Scalar + Copy {} | |
| #[cfg(feature = "bytemuck")] | ||
| unsafe impl<T> bytemuck::AnyBitPattern for BigEndian<T> where T: Scalar + Copy + 'static {} | ||
|
|
||
| impl<T: Scalar> BigEndian<T> { | ||
| /// construct a new `BigEndian<T>` from raw bytes | ||
| pub fn new(raw: T::Raw) -> BigEndian<T> { | ||
| BigEndian(raw) | ||
| } | ||
|
|
||
| impl<T: Scalar> BytesWrapper for BigEndian<T> { | ||
| type Inner = T; | ||
| /// Attempt to construct a new raw value from this slice. | ||
| /// | ||
| /// This will fail if `slice.len() != T::RAW_BYTE_LEN`. | ||
| pub fn from_slice(slice: &[u8]) -> Option<Self> { | ||
| fn from_slice(slice: &[u8]) -> Option<Self> { | ||
| sealed::BeByteArray::from_slice(slice).map(Self) | ||
| } | ||
|
|
||
| /// Convert this raw type to its native representation. | ||
| #[inline(always)] | ||
| pub fn get(&self) -> T { | ||
| fn get(&self) -> T { | ||
| T::from_raw(self.0) | ||
| } | ||
|
|
||
| /// Set the value, overwriting the bytes. | ||
| pub fn set(&mut self, value: T) { | ||
| fn set(&mut self, value: T) { | ||
| self.0 = value.to_raw(); | ||
| } | ||
| } | ||
|
|
||
| impl<T: Scalar> BigEndian<T> { | ||
| /// construct a new `BigEndian<T>` from raw bytes | ||
| pub fn new(raw: T::Raw) -> BigEndian<T> { | ||
| BigEndian(raw) | ||
| } | ||
|
|
||
| /// Get the raw big-endian bytes. | ||
| pub fn be_bytes(&self) -> &[u8] { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we keep this I think it's fine to rename the trait to just
ByteArray?