Skip to content

Commit

Permalink
docs(util): fix doc reference in the zerocopy module
Browse files Browse the repository at this point in the history
  • Loading branch information
aminfa committed Dec 15, 2024
1 parent 9336794 commit ef4f550
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
21 changes: 11 additions & 10 deletions util/src/zerocopy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
//!
//! It offers the following primary abstractions and traits:
//!
//! - [`RefMaker`]: A helper structure for safely creating
//! [`Ref`](zerocopy::Ref) references from byte slices.
//! - [`ZerocopyEmancipateExt`]: A trait to convert `Ref<B, T>` into a borrowed
//! `Ref<&[u8], T>`.
//! - [`ZerocopyEmancipateMutExt`]: A trait to convert `Ref<B, T>` into a
//! borrowed mutable `Ref<&mut [u8], T>`.
//! - [`ZerocopySliceExt`]: Extension methods for parsing byte slices into
//! zero-copy references.
//! - [`ZerocopyMutSliceExt`]: Extension methods for parsing and zeroizing byte
//! slices into zero-copy references.
//! - [`RefMaker`](crate::zerocopy::RefMaker): A helper structure for safely
//! creating `zerocopy::Ref` references from byte slices.
//! - [`ZerocopyEmancipateExt`](crate::zerocopy::ZerocopyEmancipateExt):
//! A trait to convert `Ref<B, T>` into a borrowed `Ref<&[u8], T>`.
//! - [`ZerocopyEmancipateMutExt`](crate::zerocopy::ZerocopyEmancipateMutExt):
//! A trait to convert `Ref<B, T>` into a borrowed mutable `Ref<&mut [u8], T>`.
//! - [`ZerocopySliceExt`](crate::zerocopy::ZerocopySliceExt): Extension methods
//! for parsing byte slices into zero-copy references.
//! - [`ZerocopyMutSliceExt`](crate::zerocopy::ZerocopyMutSliceExt):
//! Extension methods for parsing and zeroizing byte slices into zero-copy
//! references.
mod ref_maker;
mod zerocopy_ref_ext;
Expand Down
7 changes: 3 additions & 4 deletions util/src/zerocopy/ref_maker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<B, T> RefMaker<B, T> {
}

/// Returns the size in bytes required by the target type `T`.
/// This is currently defined as [std::mem::size_of] of `T`.
/// This is currently defined as [`std::mem::size_of::<T>`] of `T`.
pub const fn target_size() -> usize {
std::mem::size_of::<T>()
}
Expand Down Expand Up @@ -109,15 +109,14 @@ impl<B: ByteSlice, T> RefMaker<B, T> {
/// let bytes: &[u8] = &[0x01, 0x02, 0x03];
/// let parse_error = RefMaker::<_, Data>::new(bytes).parse()
/// .expect_err("Should error");
/// assert_eq!(format!("{:?}", parse_error),
/// assert_eq!(parse_error.to_string(),
/// "Buffer is undersized at 3 bytes (need 4 bytes)!");
///
/// // errors if the byte buffer is misaligned
/// let bytes = [1u8, 2, 3, 4, 5, 6, 7, 8];
/// let parse_error = RefMaker::<_, Data>::new(&bytes[1..5]).parse()
/// .expect_err("Should error");
/// assert_eq!(format!("{:?}", parse_error),
/// "Parser error!");
/// assert_eq!(parse_error.to_string(), "Parser error!");
/// ```
pub fn parse(self) -> anyhow::Result<Ref<B, T>> {
self.ensure_fit()?;
Expand Down
5 changes: 2 additions & 3 deletions util/src/zerocopy/zerocopy_ref_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use zerocopy::{ByteSlice, ByteSliceMut, Ref};
/// This can be useful when you need a reference that is tied to a slice rather
/// than the original buffer type `B`.
///
/// Note: This trait is implemented to [Ref](zerocopy::Ref) of byte slices
/// (`&[u8]`).
/// Note: This trait is implemented to [`Ref`] of byte slices (`&[u8]`).
pub trait ZerocopyEmancipateExt<B, T> {
/// Converts this reference into a reference backed by a plain byte slice.
///
Expand All @@ -36,7 +35,7 @@ pub trait ZerocopyEmancipateExt<B, T> {
///
/// Similar to [`ZerocopyEmancipateExt`], but for mutable references.
///
/// Note: this trait is implemented to [Ref](zerocopy::Ref) of mutable byte
/// Note: this trait is implemented to [`Ref`] of mutable byte
/// slices (`&mut [u8]`).
pub trait ZerocopyEmancipateMutExt<B, T> {
/// Converts this reference into a mutable reference backed by a plain
Expand Down
6 changes: 3 additions & 3 deletions util/src/zerocopy/zerocopy_slice_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::RefMaker;

/// Extension trait for performing zero-copy parsing operations on byte slices.
///
/// This trait adds methods for creating [`Ref`](zerocopy::Ref) references from
/// This trait adds methods for creating [`Ref`] references from
/// slices by using the [`RefMaker`] type internally.
pub trait ZerocopySliceExt: Sized + ByteSlice {
/// Creates a new `RefMaker` for the given slice.
Expand Down Expand Up @@ -58,7 +58,7 @@ pub trait ZerocopySliceExt: Sized + ByteSlice {

/// Parses a prefix of the slice into a zero-copy reference.
///
/// Uses only the first [std::mem::size_of::<T>()] bytes of `T`.
/// Uses only the first [`std::mem::size_of::<T>()`] bytes of `T`.
///
/// # Errors
///
Expand Down Expand Up @@ -86,7 +86,7 @@ pub trait ZerocopySliceExt: Sized + ByteSlice {

/// Parses a suffix of the slice into a zero-copy reference.
///
/// Uses only the last [std::mem::size_of::<T>()] bytes of `T`.
/// Uses only the last [`std::mem::size_of::<T>()`] bytes of `T`.
///
/// # Errors
///
Expand Down

0 comments on commit ef4f550

Please sign in to comment.