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

Add TryFromBytes prefix/suffix conversion methods #1072

Merged
merged 1 commit into from
May 9, 2024
Merged

Add TryFromBytes prefix/suffix conversion methods #1072

merged 1 commit into from
May 9, 2024

Conversation

jswrenn
Copy link
Collaborator

@jswrenn jswrenn commented Mar 25, 2024

No description provided.

Copy link
Member

@joshlf joshlf left a comment

Choose a reason for hiding this comment

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

Rebase on top of #1161

Comment on lines +1178 to +1194
match Ptr::from_ref(candidate).try_cast_into_no_leftover::<Self>() {
Ok(candidate) => {
// This call may panic. If that happens, it doesn't cause any soundness
// issues, as we have not generated any invalid state which we need to
// fix before returning.
//
// Note that one panic or post-monomorphization error condition is
// calling `try_into_valid` (and thus `is_bit_valid`) with a shared
// pointer when `Self: !Immutable`. Since `Self: Immutable`, this panic
// condition will not happen.
match candidate.try_into_valid() {
Ok(valid) => Ok(valid.as_ref()),
Err(e) => Err(e.map_src(|src| src.as_bytes().as_ref()).into()),
}
}
Err(e) => Err(e.map_src(Ptr::as_ref).into()),
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Rewritten in this manner for consistency with our other from_ref methods, and because doing so allows us to name try_ref_from's argument "candidate" without thinking of new, weird identifier names.

@jswrenn jswrenn force-pushed the split-at branch 2 times, most recently from 25819dd to 0e1bf9a Compare May 7, 2024 22:11
@jswrenn
Copy link
Collaborator Author

jswrenn commented May 7, 2024

@joshlf Need to write a commit message, but otherwise this is ready for review. This PR intentionally omits prefix/suffix-returning versions of try_read_from, since I suspect those will get considerably less verbose once we finish our UnsafeCell-and-Ptr refactor.

src/lib.rs Outdated
/// [here][TryFromBytes#what-is-a-valid-instance] for a discussion of how
/// these cases are handled.
/// If the bytes of `candidate` are a valid instance of `Self`, this method
/// returns a reference to those bytes interpreted as `Self`. If
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// returns a reference to those bytes interpreted as `Self`. If
/// returns a reference to those bytes reinterpreted as a `Self`. If

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

src/lib.rs Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated
///
/// If the first `size_of::<Self>()` bytes of `candidate` are a valid
/// instance of `Self`, this method returns both a reference to those bytes
/// interpreted as `Self`, and a reference to the remaining bytes. If
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// interpreted as `Self`, and a reference to the remaining bytes. If
/// reinterpreted as a `Self`, and a reference to the remaining bytes. If

src/lib.rs Outdated
///
/// If the last `size_of::<Self>()` bytes of `candidate` are a valid
/// instance of `Self`, this method returns both a reference to those bytes
/// interpreted as `Self`, and a reference to the preceding bytes. If
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// interpreted as `Self`, and a reference to the preceding bytes. If
/// reinterpreted as a `Self`, and a reference to the preceding bytes. If

src/lib.rs Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated
///
/// If the bytes of `candidate` are a valid instance of `Self`, reads those
/// bytes as `Self`. If `candidate.len() < size_of::<Self>()` or `candidate`
/// is not aligned to `align_of::<Self>()` or the bytes are not a valid
Copy link
Member

Choose a reason for hiding this comment

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

Alignment isn't checked, right?

src/lib.rs Outdated Show resolved Hide resolved
@jswrenn jswrenn enabled auto-merge May 9, 2024 19:16
@jswrenn jswrenn added this pull request to the merge queue May 9, 2024
Merged via the queue into main with commit fea84bd May 9, 2024
210 checks passed
@jswrenn jswrenn deleted the split-at branch May 9, 2024 19:30
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.

2 participants