Skip to content

Encode infallible alignment errors in types #1718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2024
Merged

Conversation

joshlf
Copy link
Member

@joshlf joshlf commented Sep 21, 2024

Permit callers to prove at compile time that alignment errors are unreachable for unaligned destination types. This permits them to infallibly ignore this error condition.

@joshlf
Copy link
Member Author

joshlf commented Sep 21, 2024

Still need to bikeshed naming and how to write the doc comment, but otherwise it's done.

Copy link
Collaborator

@jswrenn jswrenn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really elegant! I particularly like the error-ization of validate_aligned_to.

@joshlf joshlf force-pushed the infallible-alignment-error branch from 1b8c3f7 to e9e640d Compare September 21, 2024 18:00
Copy link
Collaborator

@jswrenn jswrenn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few #[inline(always)] nits, but then I'm happy to merge this.

I wonder if this is sufficiently elegant that we should remove the unaligned methods from Ref!

src/error.rs Outdated
@@ -626,6 +683,17 @@ impl<Src, Dst: ?Sized> CastError<Src, Dst> {
}
}

impl<Src, Dst: ?Sized + Unaligned> Into<SizeError<Src, Dst>> for CastError<Src, Dst> {
fn into(self) -> SizeError<Src, Dst> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slap an #[inline(always)] on this — we want the effects of unreachable_unchecked to propagate up the call chain.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/error.rs Outdated
@@ -255,6 +290,28 @@ impl<Src, Dst: ?Sized> AlignmentError<Src, Dst> {
}
}

impl<Src, Dst: ?Sized + Unaligned> AlignmentError<Src, Dst> {
fn into_infallible(self) -> Infallible {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slap an #[inline(always)] on this — we want the effects of unreachable_unchecked to propagate up the call chain.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/error.rs Outdated
}
}

impl<Src, Dst: ?Sized + Unaligned, S, V> Into<ConvertError<Infallible, S, V>>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we implementing Into here instead of From?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/error.rs Outdated
@@ -255,6 +290,28 @@ impl<Src, Dst: ?Sized> AlignmentError<Src, Dst> {
}
}

impl<Src, Dst: ?Sized + Unaligned> AlignmentError<Src, Dst> {
fn into_infallible(self) -> Infallible {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, should this be an implementation of From? Then we could have a consistent messaging that calling .into() at all levels makes impossible error branches Infallible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@joshlf joshlf force-pushed the infallible-alignment-error branch from e9e640d to df8a716 Compare September 21, 2024 18:44
@codecov-commenter
Copy link

codecov-commenter commented Sep 21, 2024

Codecov Report

Attention: Patch coverage is 53.19149% with 22 lines in your changes missing coverage. Please review.

Project coverage is 88.35%. Comparing base (553996e) to head (761d909).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/error.rs 42.42% 19 Missing ⚠️
src/ref.rs 50.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1718      +/-   ##
==========================================
- Coverage   88.79%   88.35%   -0.45%     
==========================================
  Files          16       16              
  Lines        5846     5889      +43     
==========================================
+ Hits         5191     5203      +12     
- Misses        655      686      +31     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@joshlf joshlf force-pushed the infallible-alignment-error branch from df8a716 to 22e78b9 Compare September 21, 2024 19:00
Permit callers to prove at compile time that alignment errors are
unreachable for unaligned destination types. This permits them to
infallibly ignore this error condition.
@joshlf joshlf force-pushed the infallible-alignment-error branch from 22e78b9 to 761d909 Compare September 21, 2024 19:22
@joshlf
Copy link
Member Author

joshlf commented Sep 21, 2024

I wonder if this is sufficiently elegant that we should remove the unaligned methods from Ref!

Probably, but let's test it in Fuchsia first.

@joshlf joshlf marked this pull request as ready for review September 21, 2024 19:25
@joshlf joshlf enabled auto-merge September 21, 2024 19:25
Comment on lines +141 to +172
/// Infallibly discards the alignment error from this `ConvertError` since
/// `Dst` is unaligned.
///
/// Since [`Dst: Unaligned`], it is impossible to encounter an alignment
/// error. This method permits discarding that alignment error infallibly
/// and replacing it with [`Infallible`].
///
/// [`Dst: Unaligned`]: crate::Unaligned
///
/// # Examples
///
/// ```
/// use core::convert::Infallible;
/// use zerocopy::*;
/// # use zerocopy_derive::*;
///
/// #[derive(TryFromBytes, KnownLayout, Unaligned, Immutable)]
/// #[repr(C, packed)]
/// struct Bools {
/// one: bool,
/// two: bool,
/// many: [bool],
/// }
///
/// impl Bools {
/// fn parse(bytes: &[u8]) -> Result<&Bools, UnalignedTryCastError<&[u8], Bools>> {
/// // Since `Bools: Unaligned`, we can infallibly discard
/// // the alignment error.
/// Bools::try_ref_from_bytes(bytes).map_err(Into::into)
/// }
/// }
/// ```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think users are primed to expect bespoke documentation on trait impls. Let's move this documentation to the error module docs.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, without the generalization recommendation (which is impossible), I'm okay having these docs here.

@joshlf joshlf added this pull request to the merge queue Sep 21, 2024
Merged via the queue into main with commit c6b9554 Sep 21, 2024
86 checks passed
@joshlf joshlf deleted the infallible-alignment-error branch September 21, 2024 20:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants