Skip to content

Commit

Permalink
[docs] Make infallible align errors discoverable (#1737)
Browse files Browse the repository at this point in the history
Wherever alignment errors are possible, mention that it is possible to
discard alignment errors when `Dst: Unaligned`.

Closes #1721
  • Loading branch information
joshlf authored Sep 23, 2024
1 parent 13bff62 commit fcc037a
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 50 deletions.
29 changes: 20 additions & 9 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
//!
//! ## Single failure mode errors
//!
//! Generally speaking, zerocopy's conversions may fail for one of up to three reasons:
//! Generally speaking, zerocopy's conversions may fail for one of up to three
//! reasons:
//! - [`AlignmentError`]: the conversion source was improperly aligned
//! - [`SizeError`]: the conversion source was of incorrect size
//! - [`ValidityError`]: the conversion source contained invalid data
Expand All @@ -28,6 +29,15 @@
//! - [`TryCastError`]: the error type of fallible reference conversions
//! - [`TryReadError`]: the error type of fallible read conversions
//!
//! ## [`Unaligned`] destination types
//!
//! For [`Unaligned`] destination types, alignment errors are impossible. All
//! compound error types support infallibly discarding the alignment error via
//! [`From`] so long as `Dst: Unaligned`. For example, see [`<SizeError as
//! From<ConvertError>>::from`][size-error-from].
//!
//! [size-error-from]: struct.SizeError.html#method.from-1
//!
//! ## Accessing the conversion source
//!
//! All error types provide an `into_src` method that converts the error into
Expand Down Expand Up @@ -63,8 +73,8 @@
//! ## `Send`, `Sync`, and `'static`
//!
//! Our error types are `Send`, `Sync`, and `'static` when their `Src` parameter
//! is `Send`, `Sync`, or `'static`, respectively. This can cause issues when
//! an error is sent or synchronized across threads; e.g.:
//! is `Send`, `Sync`, or `'static`, respectively. This can cause issues when an
//! error is sent or synchronized across threads; e.g.:
//!
//! ```compile_fail,E0515
//! use zerocopy::*;
Expand Down Expand Up @@ -164,7 +174,7 @@ impl<Src, Dst: ?Sized + Unaligned, S, V> From<ConvertError<AlignmentError<Src, D
/// }
///
/// impl Bools {
/// fn parse(bytes: &[u8]) -> Result<&Bools, UnalignedTryCastError<&[u8], Bools>> {
/// fn parse(bytes: &[u8]) -> Result<&Bools, AlignedTryCastError<&[u8], Bools>> {
/// // Since `Bools: Unaligned`, we can infallibly discard
/// // the alignment error.
/// Bools::try_ref_from_bytes(bytes).map_err(Into::into)
Expand Down Expand Up @@ -881,10 +891,11 @@ impl<Src, Dst: ?Sized + TryFromBytes> TryReadError<Src, Dst> {
}
}

/// The error type of fallible casts to unaligned types.
/// The error type of well-aligned, fallible casts.
///
/// This is like [`TryCastError`], but for unaligned types. It is identical to
/// `TryCastError`, except that its alignment error is [`Infallible`].
/// This is like [`TryCastError`], but for casts that are always well-aligned.
/// It is identical to `TryCastError`, except that its alignment error is
/// [`Infallible`].
///
/// As of this writing, none of zerocopy's API produces this error directly.
/// However, it is useful since it permits users to infallibly discard alignment
Expand All @@ -906,15 +917,15 @@ impl<Src, Dst: ?Sized + TryFromBytes> TryReadError<Src, Dst> {
/// }
///
/// impl Bools {
/// fn parse(bytes: &[u8]) -> Result<&Bools, UnalignedTryCastError<&[u8], Bools>> {
/// fn parse(bytes: &[u8]) -> Result<&Bools, AlignedTryCastError<&[u8], Bools>> {
/// // Since `Bools: Unaligned`, we can infallibly discard
/// // the alignment error.
/// Bools::try_ref_from_bytes(bytes).map_err(Into::into)
/// }
/// }
/// ```
#[allow(type_alias_bounds)]
pub type UnalignedTryCastError<Src, Dst: ?Sized + TryFromBytes> =
pub type AlignedTryCastError<Src, Dst: ?Sized + TryFromBytes> =
ConvertError<Infallible, SizeError<Src, Dst>, ValidityError<Src, Dst>>;

/// The error type of a failed allocation.
Expand Down
Loading

0 comments on commit fcc037a

Please sign in to comment.