use crate::Decimal;
impl<const D: u8> Decimal<i32, D> {
pub const BYTES: usize = 4;
#[inline(always)]
pub const fn to_le_bytes(self) -> [u8; 4] {
self.value.to_le_bytes()
}
#[inline(always)]
pub const fn to_be_bytes(self) -> [u8; 4] {
self.value.to_be_bytes()
}
#[inline(always)]
pub const fn to_ne_bytes(self) -> [u8; 4] {
self.value.to_ne_bytes()
}
#[inline(always)]
pub const fn from_le_bytes(bytes: [u8; 4]) -> Self {
Self {
value: i32::from_le_bytes(bytes),
}
}
#[inline(always)]
pub const fn from_be_bytes(bytes: [u8; 4]) -> Self {
Self {
value: i32::from_be_bytes(bytes),
}
}
#[inline(always)]
pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self {
Self {
value: i32::from_ne_bytes(bytes),
}
}
#[inline]
pub fn write_le_bytes(&self, buf: &mut [u8]) {
buf[..4].copy_from_slice(&self.to_le_bytes());
}
#[inline]
pub fn write_be_bytes(&self, buf: &mut [u8]) {
buf[..4].copy_from_slice(&self.to_be_bytes());
}
#[inline]
pub fn read_le_bytes(buf: &[u8]) -> Self {
let bytes: [u8; 4] = buf[..4]
.try_into()
.expect("buf[..4] always yields a 4-byte slice");
Self::from_le_bytes(bytes)
}
#[inline]
pub fn read_be_bytes(buf: &[u8]) -> Self {
let bytes: [u8; 4] = buf[..4]
.try_into()
.expect("buf[..4] always yields a 4-byte slice");
Self::from_be_bytes(bytes)
}
}
impl<const D: u8> Decimal<i64, D> {
pub const BYTES: usize = 8;
#[inline(always)]
pub const fn to_le_bytes(self) -> [u8; 8] {
self.value.to_le_bytes()
}
#[inline(always)]
pub const fn to_be_bytes(self) -> [u8; 8] {
self.value.to_be_bytes()
}
#[inline(always)]
pub const fn to_ne_bytes(self) -> [u8; 8] {
self.value.to_ne_bytes()
}
#[inline(always)]
pub const fn from_le_bytes(bytes: [u8; 8]) -> Self {
Self {
value: i64::from_le_bytes(bytes),
}
}
#[inline(always)]
pub const fn from_be_bytes(bytes: [u8; 8]) -> Self {
Self {
value: i64::from_be_bytes(bytes),
}
}
#[inline(always)]
pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self {
Self {
value: i64::from_ne_bytes(bytes),
}
}
#[inline]
pub fn write_le_bytes(&self, buf: &mut [u8]) {
buf[..8].copy_from_slice(&self.to_le_bytes());
}
#[inline]
pub fn write_be_bytes(&self, buf: &mut [u8]) {
buf[..8].copy_from_slice(&self.to_be_bytes());
}
#[inline]
pub fn read_le_bytes(buf: &[u8]) -> Self {
let bytes: [u8; 8] = buf[..8]
.try_into()
.expect("buf[..8] always yields an 8-byte slice");
Self::from_le_bytes(bytes)
}
#[inline]
pub fn read_be_bytes(buf: &[u8]) -> Self {
let bytes: [u8; 8] = buf[..8]
.try_into()
.expect("buf[..8] always yields an 8-byte slice");
Self::from_be_bytes(bytes)
}
}
impl<const D: u8> Decimal<i128, D> {
pub const BYTES: usize = 16;
#[inline(always)]
pub const fn to_le_bytes(self) -> [u8; 16] {
self.value.to_le_bytes()
}
#[inline(always)]
pub const fn to_be_bytes(self) -> [u8; 16] {
self.value.to_be_bytes()
}
#[inline(always)]
pub const fn to_ne_bytes(self) -> [u8; 16] {
self.value.to_ne_bytes()
}
#[inline(always)]
pub const fn from_le_bytes(bytes: [u8; 16]) -> Self {
Self {
value: i128::from_le_bytes(bytes),
}
}
#[inline(always)]
pub const fn from_be_bytes(bytes: [u8; 16]) -> Self {
Self {
value: i128::from_be_bytes(bytes),
}
}
#[inline(always)]
pub const fn from_ne_bytes(bytes: [u8; 16]) -> Self {
Self {
value: i128::from_ne_bytes(bytes),
}
}
#[inline]
pub fn write_le_bytes(&self, buf: &mut [u8]) {
buf[..16].copy_from_slice(&self.to_le_bytes());
}
#[inline]
pub fn write_be_bytes(&self, buf: &mut [u8]) {
buf[..16].copy_from_slice(&self.to_be_bytes());
}
#[inline]
pub fn read_le_bytes(buf: &[u8]) -> Self {
let bytes: [u8; 16] = buf[..16]
.try_into()
.expect("buf[..16] always yields a 16-byte slice");
Self::from_le_bytes(bytes)
}
#[inline]
pub fn read_be_bytes(buf: &[u8]) -> Self {
let bytes: [u8; 16] = buf[..16]
.try_into()
.expect("buf[..16] always yields a 16-byte slice");
Self::from_be_bytes(bytes)
}
}