Skip to content

Commit

Permalink
Use safe, public APIs where possible to remove some unsafe blocks. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
anforowicz authored Sep 5, 2023
1 parent 763d69e commit db69fee
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/checked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl From<crate::PodCastError> for CheckedCastError {
pub fn try_from_bytes<T: CheckedBitPattern>(
s: &[u8],
) -> Result<&T, CheckedCastError> {
let pod = unsafe { internal::try_from_bytes(s) }?;
let pod = crate::try_from_bytes(s)?;

if <T as CheckedBitPattern>::is_valid_bit_pattern(pod) {
Ok(unsafe { &*(pod as *const <T as CheckedBitPattern>::Bits as *const T) })
Expand Down Expand Up @@ -281,7 +281,7 @@ pub fn try_from_bytes_mut<T: CheckedBitPattern + NoUninit>(
pub fn try_pod_read_unaligned<T: CheckedBitPattern>(
bytes: &[u8],
) -> Result<T, CheckedCastError> {
let pod = unsafe { internal::try_pod_read_unaligned(bytes) }?;
let pod = crate::try_pod_read_unaligned(bytes)?;

if <T as CheckedBitPattern>::is_valid_bit_pattern(&pod) {
Ok(unsafe { transmute!(pod) })
Expand All @@ -305,7 +305,7 @@ pub fn try_pod_read_unaligned<T: CheckedBitPattern>(
pub fn try_cast<A: NoUninit, B: CheckedBitPattern>(
a: A,
) -> Result<B, CheckedCastError> {
let pod = unsafe { internal::try_cast(a) }?;
let pod = crate::try_cast(a)?;

if <B as CheckedBitPattern>::is_valid_bit_pattern(&pod) {
Ok(unsafe { transmute!(pod) })
Expand All @@ -325,7 +325,7 @@ pub fn try_cast<A: NoUninit, B: CheckedBitPattern>(
pub fn try_cast_ref<A: NoUninit, B: CheckedBitPattern>(
a: &A,
) -> Result<&B, CheckedCastError> {
let pod = unsafe { internal::try_cast_ref(a) }?;
let pod = crate::try_cast_ref(a)?;

if <B as CheckedBitPattern>::is_valid_bit_pattern(pod) {
Ok(unsafe { &*(pod as *const <B as CheckedBitPattern>::Bits as *const B) })
Expand Down Expand Up @@ -374,7 +374,7 @@ pub fn try_cast_mut<
pub fn try_cast_slice<A: NoUninit, B: CheckedBitPattern>(
a: &[A],
) -> Result<&[B], CheckedCastError> {
let pod = unsafe { internal::try_cast_slice(a) }?;
let pod = crate::try_cast_slice(a)?;

if pod.iter().all(|pod| <B as CheckedBitPattern>::is_valid_bit_pattern(pod)) {
Ok(unsafe {
Expand Down

0 comments on commit db69fee

Please sign in to comment.