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

Make from_bytes integer constructors const #712

Merged
merged 1 commit into from
Aug 1, 2024
Merged
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: 12 additions & 12 deletions src/endian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ mod aligned {

impl<E: Endian> U16<E> {
/// Construct a new value given bytes that already have the required endianness.
pub fn from_bytes(n: [u8; 2]) -> Self {
pub const fn from_bytes(n: [u8; 2]) -> Self {
Self(u16::from_ne_bytes(n), PhantomData)
}

Expand All @@ -430,7 +430,7 @@ mod aligned {

impl<E: Endian> U32<E> {
/// Construct a new value given bytes that already have the required endianness.
pub fn from_bytes(n: [u8; 4]) -> Self {
pub const fn from_bytes(n: [u8; 4]) -> Self {
Self(u32::from_ne_bytes(n), PhantomData)
}

Expand All @@ -455,7 +455,7 @@ mod aligned {

impl<E: Endian> U64<E> {
/// Construct a new value given bytes that already have the required endianness.
pub fn from_bytes(n: [u8; 8]) -> Self {
pub const fn from_bytes(n: [u8; 8]) -> Self {
Self(u64::from_ne_bytes(n), PhantomData)
}

Expand All @@ -480,7 +480,7 @@ mod aligned {

impl<E: Endian> I16<E> {
/// Construct a new value given bytes that already have the required endianness.
pub fn from_bytes(n: [u8; 2]) -> Self {
pub const fn from_bytes(n: [u8; 2]) -> Self {
Self(i16::from_ne_bytes(n), PhantomData)
}

Expand All @@ -505,7 +505,7 @@ mod aligned {

impl<E: Endian> I32<E> {
/// Construct a new value given bytes that already have the required endianness.
pub fn from_bytes(n: [u8; 4]) -> Self {
pub const fn from_bytes(n: [u8; 4]) -> Self {
Self(i32::from_ne_bytes(n), PhantomData)
}

Expand All @@ -530,7 +530,7 @@ mod aligned {

impl<E: Endian> I64<E> {
/// Construct a new value given bytes that already have the required endianness.
pub fn from_bytes(n: [u8; 8]) -> Self {
pub const fn from_bytes(n: [u8; 8]) -> Self {
Self(i64::from_ne_bytes(n), PhantomData)
}

Expand Down Expand Up @@ -621,7 +621,7 @@ pub struct U16Bytes<E: Endian>([u8; 2], PhantomData<E>);

impl<E: Endian> U16Bytes<E> {
/// Construct a new value given bytes that already have the required endianness.
pub fn from_bytes(n: [u8; 2]) -> Self {
pub const fn from_bytes(n: [u8; 2]) -> Self {
Self(n, PhantomData)
}

Expand All @@ -648,7 +648,7 @@ pub struct U32Bytes<E: Endian>([u8; 4], PhantomData<E>);

impl<E: Endian> U32Bytes<E> {
/// Construct a new value given bytes that already have the required endianness.
pub fn from_bytes(n: [u8; 4]) -> Self {
pub const fn from_bytes(n: [u8; 4]) -> Self {
Self(n, PhantomData)
}

Expand All @@ -675,7 +675,7 @@ pub struct U64Bytes<E: Endian>([u8; 8], PhantomData<E>);

impl<E: Endian> U64Bytes<E> {
/// Construct a new value given bytes that already have the required endianness.
pub fn from_bytes(n: [u8; 8]) -> Self {
pub const fn from_bytes(n: [u8; 8]) -> Self {
Self(n, PhantomData)
}

Expand All @@ -702,7 +702,7 @@ pub struct I16Bytes<E: Endian>([u8; 2], PhantomData<E>);

impl<E: Endian> I16Bytes<E> {
/// Construct a new value given bytes that already have the required endianness.
pub fn from_bytes(n: [u8; 2]) -> Self {
pub const fn from_bytes(n: [u8; 2]) -> Self {
Self(n, PhantomData)
}

Expand All @@ -729,7 +729,7 @@ pub struct I32Bytes<E: Endian>([u8; 4], PhantomData<E>);

impl<E: Endian> I32Bytes<E> {
/// Construct a new value given bytes that already have the required endianness.
pub fn from_bytes(n: [u8; 4]) -> Self {
pub const fn from_bytes(n: [u8; 4]) -> Self {
Self(n, PhantomData)
}

Expand All @@ -756,7 +756,7 @@ pub struct I64Bytes<E: Endian>([u8; 8], PhantomData<E>);

impl<E: Endian> I64Bytes<E> {
/// Construct a new value given bytes that already have the required endianness.
pub fn from_bytes(n: [u8; 8]) -> Self {
pub const fn from_bytes(n: [u8; 8]) -> Self {
Self(n, PhantomData)
}

Expand Down
Loading