From 2f16fbe5c9f744b404e780a8cfa0de94663916b7 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Wed, 31 Jul 2024 14:37:55 +0000 Subject: [PATCH] Make `from_bytes` integer constructors const --- src/endian.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/endian.rs b/src/endian.rs index e4a36ba1..9081879c 100644 --- a/src/endian.rs +++ b/src/endian.rs @@ -403,7 +403,7 @@ mod aligned { impl U16 { /// 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) } @@ -430,7 +430,7 @@ mod aligned { impl U32 { /// 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) } @@ -455,7 +455,7 @@ mod aligned { impl U64 { /// 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) } @@ -480,7 +480,7 @@ mod aligned { impl I16 { /// 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) } @@ -505,7 +505,7 @@ mod aligned { impl I32 { /// 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) } @@ -530,7 +530,7 @@ mod aligned { impl I64 { /// 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) } @@ -621,7 +621,7 @@ pub struct U16Bytes([u8; 2], PhantomData); impl U16Bytes { /// 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) } @@ -648,7 +648,7 @@ pub struct U32Bytes([u8; 4], PhantomData); impl U32Bytes { /// 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) } @@ -675,7 +675,7 @@ pub struct U64Bytes([u8; 8], PhantomData); impl U64Bytes { /// 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) } @@ -702,7 +702,7 @@ pub struct I16Bytes([u8; 2], PhantomData); impl I16Bytes { /// 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) } @@ -729,7 +729,7 @@ pub struct I32Bytes([u8; 4], PhantomData); impl I32Bytes { /// 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) } @@ -756,7 +756,7 @@ pub struct I64Bytes([u8; 8], PhantomData); impl I64Bytes { /// 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) }