removed magic size constants

This commit is contained in:
Monica Moniot
2024-03-26 12:34:12 -04:00
parent daebb48841
commit 17bc59bc30
2 changed files with 23 additions and 19 deletions
+14 -12
View File
@@ -35,9 +35,9 @@ impl Address {
/// Get this address as a raw byte array.
#[inline(always)]
pub fn as_bytes(&self) -> &[u8; 48] {
debug_assert_eq!(size_of::<[u8; 48]>(), size_of::<Self>());
unsafe { &*self.0.as_ptr().cast::<[u8; 48]>() }
pub fn as_bytes(&self) -> &[u8; Address::SIZE] {
debug_assert_eq!(size_of::<[u8; Address::SIZE]>(), size_of::<Self>());
unsafe { &*self.0.as_ptr().cast::<[u8; Address::SIZE]>() }
}
/// Get this address's 128-bit short prefix.
@@ -49,9 +49,9 @@ impl Address {
/// Get mutable bytes.
/// This is private because it should be impossible for external code to create an invalid address.
#[inline(always)]
pub(crate) fn as_mut_bytes(&mut self) -> &mut [u8; 48] {
debug_assert_eq!(size_of::<[u8; 48]>(), size_of::<Self>());
unsafe { &mut *self.0.as_mut_ptr().cast::<[u8; 48]>() }
pub(crate) fn as_mut_bytes(&mut self) -> &mut [u8; Address::SIZE] {
debug_assert_eq!(size_of::<[u8; Address::SIZE]>(), size_of::<Self>());
unsafe { &mut *self.0.as_mut_ptr().cast::<[u8; Address::SIZE]>() }
}
/// Check address validity, used in deserialization code.
@@ -73,11 +73,11 @@ impl Address {
}
}
impl TryFrom<[u8; 48]> for Address {
impl TryFrom<[u8; Address::SIZE]> for Address {
type Error = InvalidParameterError;
#[inline]
fn try_from(value: [u8; 48]) -> Result<Self, Self::Error> {
fn try_from(value: [u8; Address::SIZE]) -> Result<Self, Self::Error> {
let a = Self(unsafe { transmute(value) });
if a.is_valid() {
Ok(a)
@@ -87,7 +87,7 @@ impl TryFrom<[u8; 48]> for Address {
}
}
impl From<Address> for [u8; 48] {
impl From<Address> for [u8; Address::SIZE] {
#[inline(always)]
fn from(value: Address) -> Self {
unsafe { transmute(value) }
@@ -190,7 +190,7 @@ impl Serialize for Address {
if serializer.is_human_readable() {
self.to_string().serialize(serializer)
} else {
<&Blob<48>>::from(self.as_bytes()).serialize(serializer)
<&Blob<{ Address::SIZE }>>::from(self.as_bytes()).serialize(serializer)
}
}
}
@@ -204,8 +204,10 @@ impl<'de> Deserialize<'de> for Address {
if deserializer.is_human_readable() {
Address::from_str(<&str>::deserialize(deserializer)?).map_err(|_| serde::de::Error::custom(ADDRESS_ERR.0))
} else {
Address::try_from(<[u8; 48]>::from(Blob::<48>::deserialize(deserializer)?))
.map_err(|_| serde::de::Error::custom(ADDRESS_ERR.0))
Address::try_from(<[u8; Address::SIZE]>::from(Blob::<{ Address::SIZE }>::deserialize(
deserializer,
)?))
.map_err(|_| serde::de::Error::custom(ADDRESS_ERR.0))
}
}
}
+9 -7
View File
@@ -46,11 +46,11 @@ impl ShortAddress {
}
}
impl TryFrom<[u8; 16]> for ShortAddress {
impl TryFrom<[u8; ShortAddress::SIZE]> for ShortAddress {
type Error = InvalidParameterError;
#[inline]
fn try_from(value: [u8; 16]) -> Result<Self, Self::Error> {
fn try_from(value: [u8; ShortAddress::SIZE]) -> Result<Self, Self::Error> {
let a = Self(unsafe { transmute(value) });
if a.is_valid() {
Ok(a)
@@ -60,7 +60,7 @@ impl TryFrom<[u8; 16]> for ShortAddress {
}
}
impl From<ShortAddress> for [u8; 16] {
impl From<ShortAddress> for [u8; ShortAddress::SIZE] {
#[inline(always)]
fn from(value: ShortAddress) -> Self {
unsafe { transmute(value) }
@@ -108,7 +108,7 @@ impl FromStr for ShortAddress {
let s = s.trim();
let s = s.strip_prefix(PREFIX_SHORT).unwrap_or(s);
if s.len() == Self::STRING_SIZE_NO_PREFIX {
let mut tmp = [0u8; 16];
let mut tmp = [0u8; ShortAddress::SIZE];
let mut w = &mut tmp[..];
for ss in s.split('.') {
if ss.len() == 7 {
@@ -165,7 +165,7 @@ impl Serialize for ShortAddress {
if serializer.is_human_readable() {
self.to_string().serialize(serializer)
} else {
<&Blob<16>>::from(self.as_bytes()).serialize(serializer)
<&Blob<{ ShortAddress::SIZE }>>::from(self.as_bytes()).serialize(serializer)
}
}
}
@@ -180,8 +180,10 @@ impl<'de> Deserialize<'de> for ShortAddress {
ShortAddress::from_str(<&str>::deserialize(deserializer)?)
.map_err(|_| serde::de::Error::custom(ADDRESS_ERR.0))
} else {
ShortAddress::try_from(<[u8; 16]>::from(Blob::<16>::deserialize(deserializer)?))
.map_err(|_| serde::de::Error::custom(ADDRESS_ERR.0))
ShortAddress::try_from(<[u8; ShortAddress::SIZE]>::from(
Blob::<{ ShortAddress::SIZE }>::deserialize(deserializer)?,
))
.map_err(|_| serde::de::Error::custom(ADDRESS_ERR.0))
}
}
}