Skip to content

Commit

Permalink
chore: improve trait implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Nov 21, 2023
1 parent 6dd83eb commit c92a82c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,9 @@ pub fn decode_to_array<T: AsRef<[u8]>, const N: usize>(input: T) -> Result<[u8;
let mut output = impl_core::uninit_array();
// SAFETY: The entire array is never read from.
let output_slice = unsafe { impl_core::slice_assume_init_mut(&mut output) };
decode_to_slice_inner(input, output_slice).map(|()| unsafe {
// SAFETY: All elements are initialized.
impl_core::array_assume_init(output)
})
// SAFETY: All elements are initialized.
decode_to_slice_inner(input, output_slice)
.map(|()| unsafe { impl_core::array_assume_init(output) })
}

decode_to_array_inner(input.as_ref())
Expand Down
21 changes: 9 additions & 12 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<U: FromHex> FromHex for Box<U> {

#[inline]
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
Ok(Box::new(FromHex::from_hex(hex.as_ref())?))
FromHex::from_hex(hex.as_ref()).map(Box::new)
}
}

Expand All @@ -133,7 +133,7 @@ where

#[inline]
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
Ok(Cow::Owned(FromHex::from_hex(hex.as_ref())?))
FromHex::from_hex(hex.as_ref()).map(Cow::Owned)
}
}

Expand All @@ -143,7 +143,7 @@ impl<U: FromHex> FromHex for Rc<U> {

#[inline]
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
Ok(Rc::new(FromHex::from_hex(hex.as_ref())?))
FromHex::from_hex(hex.as_ref()).map(Rc::new)
}
}

Expand All @@ -153,7 +153,7 @@ impl<U: FromHex> FromHex for Arc<U> {

#[inline]
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
Ok(Arc::new(FromHex::from_hex(hex.as_ref())?))
FromHex::from_hex(hex.as_ref()).map(Arc::new)
}
}

Expand All @@ -173,9 +173,9 @@ impl FromHex for Vec<i8> {

#[inline]
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
let vec = crate::decode(hex.as_ref())?;
// SAFETY: transmuting `u8` to `i8` is safe.
Ok(unsafe { core::mem::transmute::<Vec<u8>, Vec<i8>>(vec) })
crate::decode(hex.as_ref())
.map(|vec| unsafe { core::mem::transmute::<Vec<u8>, Vec<i8>>(vec) })
}
}

Expand Down Expand Up @@ -204,9 +204,7 @@ impl<const N: usize> FromHex for [u8; N] {

#[inline]
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
let mut buf = [0u8; N];
crate::decode_to_slice(hex.as_ref(), &mut buf)?;
Ok(buf)
crate::decode_to_array(hex.as_ref())
}
}

Expand All @@ -215,9 +213,8 @@ impl<const N: usize> FromHex for [i8; N] {

#[inline]
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
let mut buf = [0u8; N];
crate::decode_to_slice(hex.as_ref(), &mut buf)?;
// SAFETY: casting `[u8]` to `[i8]` is safe.
Ok(unsafe { *(&buf as *const [u8; N] as *const [i8; N]) })
crate::decode_to_array(hex.as_ref())
.map(|buf| unsafe { *(&buf as *const [u8; N] as *const [i8; N]) })
}
}

0 comments on commit c92a82c

Please sign in to comment.