From 17bc59bc30aaf353bea8097683861ce8726694d8 Mon Sep 17 00:00:00 2001 From: Monica Moniot Date: Tue, 26 Mar 2024 12:34:12 -0400 Subject: [PATCH] removed magic size constants --- src/p384/address.rs | 26 ++++++++++++++------------ src/p384/short_address.rs | 16 +++++++++------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/p384/address.rs b/src/p384/address.rs index 875b40d..3797ade 100644 --- a/src/p384/address.rs +++ b/src/p384/address.rs @@ -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::()); - 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::()); + 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::()); - 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::()); + 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 { + fn try_from(value: [u8; Address::SIZE]) -> Result { let a = Self(unsafe { transmute(value) }); if a.is_valid() { Ok(a) @@ -87,7 +87,7 @@ impl TryFrom<[u8; 48]> for Address { } } -impl From
for [u8; 48] { +impl From
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)) } } } diff --git a/src/p384/short_address.rs b/src/p384/short_address.rs index cd2842d..5504b49 100644 --- a/src/p384/short_address.rs +++ b/src/p384/short_address.rs @@ -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 { + fn try_from(value: [u8; ShortAddress::SIZE]) -> Result { let a = Self(unsafe { transmute(value) }); if a.is_valid() { Ok(a) @@ -60,7 +60,7 @@ impl TryFrom<[u8; 16]> for ShortAddress { } } -impl From for [u8; 16] { +impl From 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)) } } }