Skip to content

Commit

Permalink
Explicitly document that pod_read_unaligned and try_pod_read_unaligne…
Browse files Browse the repository at this point in the history
…d don't panic on unallowed reads (#220)

* Explicitly document that pod_read_unaligned and try_pod_read_unaligned don't panic on unallowed reads

Their names make that reasonably clear already, but it doesn't hurt to
be explicit. Also, when choosing between `*from_bytes(x)` and
`pod_read_unaligned(x)`, it's good to have a clearly documented
criterion.

* Cosmetic doc changes

Add a couple of missing links, add a missing "like" (consistent with
other similar sentences).

* Remove redundant link

The redundant link emits a warning in `cargo doc`.
  • Loading branch information
hniksic authored Jan 25, 2024
1 parent 085a5f5 commit 8dc32b1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/checked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ pub fn pod_read_unaligned<T: CheckedBitPattern>(bytes: &[u8]) -> T {
///
/// ## Panics
///
/// * This is like [`try_cast`](try_cast), but will panic on a size mismatch.
/// * This is like [`try_cast`], but will panic on a size mismatch.
#[inline]
pub fn cast<A: NoUninit, B: CheckedBitPattern>(a: A) -> B {
match try_cast(a) {
Expand Down
18 changes: 13 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub fn bytes_of_mut<T: NoUninit + AnyBitPattern>(t: &mut T) -> &mut [u8] {
///
/// ## Panics
///
/// This is [`try_from_bytes`] but will panic on error.
/// This is like [`try_from_bytes`] but will panic on error.
#[inline]
pub fn from_bytes<T: AnyBitPattern>(s: &[u8]) -> &T {
unsafe { internal::from_bytes(s) }
Expand All @@ -244,14 +244,17 @@ pub fn from_bytes<T: AnyBitPattern>(s: &[u8]) -> &T {
///
/// ## Panics
///
/// This is [`try_from_bytes_mut`] but will panic on error.
/// This is like [`try_from_bytes_mut`] but will panic on error.
#[inline]
pub fn from_bytes_mut<T: NoUninit + AnyBitPattern>(s: &mut [u8]) -> &mut T {
unsafe { internal::from_bytes_mut(s) }
}

/// Reads from the bytes as if they were a `T`.
///
/// Unlike [`from_bytes`], the slice doesn't need to respect alignment of `T`, only sizes
/// must match.
///
/// ## Failure
/// * If the `bytes` length is not equal to `size_of::<T>()`.
#[inline]
Expand All @@ -263,6 +266,9 @@ pub fn try_pod_read_unaligned<T: AnyBitPattern>(

/// Reads the slice into a `T` value.
///
/// Unlike [`from_bytes`], the slice doesn't need to respect alignment of `T`, only sizes
/// must match.
///
/// ## Panics
/// * This is like `try_pod_read_unaligned` but will panic on failure.
#[inline]
Expand Down Expand Up @@ -298,7 +304,7 @@ pub fn try_from_bytes_mut<T: NoUninit + AnyBitPattern>(
///
/// ## Panics
///
/// * This is like [`try_cast`](try_cast), but will panic on a size mismatch.
/// * This is like [`try_cast`], but will panic on a size mismatch.
#[inline]
pub fn cast<A: NoUninit, B: AnyBitPattern>(a: A) -> B {
unsafe { internal::cast(a) }
Expand Down Expand Up @@ -351,15 +357,17 @@ pub fn cast_slice_mut<
unsafe { internal::cast_slice_mut(a) }
}

/// As `align_to`, but safe because of the [`Pod`] bound.
/// As [`align_to`](https://doc.rust-lang.org/std/primitive.slice.html#method.align_to),
/// but safe because of the [`Pod`] bound.
#[inline]
pub fn pod_align_to<T: NoUninit, U: AnyBitPattern>(
vals: &[T],
) -> (&[T], &[U], &[T]) {
unsafe { vals.align_to::<U>() }
}

/// As `align_to_mut`, but safe because of the [`Pod`] bound.
/// As [`align_to_mut`](https://doc.rust-lang.org/std/primitive.slice.html#method.align_to_mut),
/// but safe because of the [`Pod`] bound.
#[inline]
pub fn pod_align_to_mut<
T: NoUninit + AnyBitPattern,
Expand Down

0 comments on commit 8dc32b1

Please sign in to comment.