Skip to content
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

Encode infallible alignment errors in types #1718

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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.

src/util/mod.rs Show resolved Hide resolved
src/error.rs Outdated Show resolved Hide resolved
src/error.rs Show resolved Hide resolved
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

src/error.rs Show resolved Hide resolved
src/error.rs Show resolved Hide resolved
@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.

src/error.rs Outdated Show resolved Hide resolved
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

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
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.

Comment on lines +138 to +139
impl<Src, Dst: ?Sized + Unaligned, S, V> From<ConvertError<AlignmentError<Src, Dst>, S, V>>
for ConvertError<Infallible, S, V>
Copy link
Collaborator

@jswrenn jswrenn Sep 21, 2024

Choose a reason for hiding this comment

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

I think we should make this a general From conversion of A, S, and V:

Suggested change
impl<Src, Dst: ?Sized + Unaligned, S, V> From<ConvertError<AlignmentError<Src, Dst>, S, V>>
for ConvertError<Infallible, S, V>
impl<Src, Dst: ?Sized + Unaligned, A, S, V, NewA, NewS, NewV>
From<ConvertError<A, S, V>>
for
ConvertError<NewA, NewS, NewV>
where
NewA: From<A>,
NewS: From<S>,
NewV: From<V>,

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