Skip to content

Commit 73dd089

Browse files
committed
Create and use from_and_to_bytes
1 parent 985b0bc commit 73dd089

File tree

3 files changed

+35
-44
lines changed

3 files changed

+35
-44
lines changed

packages/std/src/math/conversion.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,35 @@ macro_rules! try_from_int_to_uint {
326326
}
327327
pub(crate) use try_from_int_to_uint;
328328

329+
macro_rules! from_and_to_bytes {
330+
($inner: ty, $byte_size: literal) => {
331+
/// Constructs new value from big endian bytes
332+
#[must_use]
333+
pub const fn from_be_bytes(data: [u8; $byte_size]) -> Self {
334+
Self(<$inner>::from_be_bytes(data))
335+
}
336+
337+
/// Constructs new value from little endian bytes
338+
#[must_use]
339+
pub const fn from_le_bytes(data: [u8; $byte_size]) -> Self {
340+
Self(<$inner>::from_le_bytes(data))
341+
}
342+
343+
/// Returns a copy of the number as big endian bytes.
344+
#[must_use = "this returns the result of the operation, without modifying the original"]
345+
pub const fn to_be_bytes(self) -> [u8; $byte_size] {
346+
self.0.to_be_bytes()
347+
}
348+
349+
/// Returns a copy of the number as little endian bytes.
350+
#[must_use = "this returns the result of the operation, without modifying the original"]
351+
pub const fn to_le_bytes(self) -> [u8; $byte_size] {
352+
self.0.to_le_bytes()
353+
}
354+
};
355+
}
356+
pub(crate) use from_and_to_bytes;
357+
329358
#[cfg(test)]
330359
mod tests {
331360
use super::*;

packages/std/src/math/int128.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use crate::{
1414
};
1515

1616
use super::conversion::{
17-
forward_try_from, primitive_to_wrapped_int, try_from_int_to_int, wrapped_int_to_primitive,
17+
forward_try_from, from_and_to_bytes, primitive_to_wrapped_int, try_from_int_to_int,
18+
wrapped_int_to_primitive,
1819
};
1920
use super::impl_int_serde;
2021
use super::num_consts::NumConsts;
@@ -67,27 +68,7 @@ impl Int128 {
6768
self.0
6869
}
6970

70-
#[must_use]
71-
pub const fn from_be_bytes(data: [u8; 16]) -> Self {
72-
Self(i128::from_be_bytes(data))
73-
}
74-
75-
#[must_use]
76-
pub const fn from_le_bytes(data: [u8; 16]) -> Self {
77-
Self(i128::from_le_bytes(data))
78-
}
79-
80-
/// Returns a copy of the number as big endian bytes.
81-
#[must_use = "this returns the result of the operation, without modifying the original"]
82-
pub const fn to_be_bytes(self) -> [u8; 16] {
83-
self.0.to_be_bytes()
84-
}
85-
86-
/// Returns a copy of the number as little endian bytes.
87-
#[must_use = "this returns the result of the operation, without modifying the original"]
88-
pub const fn to_le_bytes(self) -> [u8; 16] {
89-
self.0.to_le_bytes()
90-
}
71+
from_and_to_bytes!(i128, 16);
9172

9273
#[must_use]
9374
pub const fn is_zero(&self) -> bool {

packages/std/src/math/int64.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use crate::{
1414
};
1515

1616
use super::conversion::{
17-
forward_try_from, primitive_to_wrapped_int, try_from_int_to_int, wrapped_int_to_primitive,
17+
forward_try_from, from_and_to_bytes, primitive_to_wrapped_int, try_from_int_to_int,
18+
wrapped_int_to_primitive,
1819
};
1920
use super::impl_int_serde;
2021
use super::num_consts::NumConsts;
@@ -67,27 +68,7 @@ impl Int64 {
6768
self.0
6869
}
6970

70-
#[must_use]
71-
pub const fn from_be_bytes(data: [u8; 8]) -> Self {
72-
Self(i64::from_be_bytes(data))
73-
}
74-
75-
#[must_use]
76-
pub const fn from_le_bytes(data: [u8; 8]) -> Self {
77-
Self(i64::from_le_bytes(data))
78-
}
79-
80-
/// Returns a copy of the number as big endian bytes.
81-
#[must_use = "this returns the result of the operation, without modifying the original"]
82-
pub const fn to_be_bytes(self) -> [u8; 8] {
83-
self.0.to_be_bytes()
84-
}
85-
86-
/// Returns a copy of the number as little endian bytes.
87-
#[must_use = "this returns the result of the operation, without modifying the original"]
88-
pub const fn to_le_bytes(self) -> [u8; 8] {
89-
self.0.to_le_bytes()
90-
}
71+
from_and_to_bytes!(i64, 8);
9172

9273
#[must_use]
9374
pub const fn is_zero(&self) -> bool {

0 commit comments

Comments
 (0)