-
Notifications
You must be signed in to change notification settings - Fork 105
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
Conversation
There was a problem hiding this 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
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()), | ||
} |
There was a problem hiding this comment.
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.
25819dd
to
0e1bf9a
Compare
@joshlf Need to write a commit message, but otherwise this is ready for review. This PR intentionally omits prefix/suffix-returning versions of |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// returns a reference to those bytes interpreted as `Self`. If | |
/// returns a reference to those bytes reinterpreted as a `Self`. If |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// 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
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 |
There was a problem hiding this comment.
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?
Makes progress towards #5
No description provided.