Skip to content

Commit

Permalink
[ptr] Relax some method bounds (#902)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlf committed Feb 20, 2024
1 parent ef67713 commit a658f31
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8177,8 +8177,9 @@ mod tests {
macro_rules! assert_impls {
($ty:ty: TryFromBytes) => {
<$ty as TryFromBytesTestable>::with_passing_test_cases(|val| {
let c = Ptr::from_ref(val).forget_aligned();
let c = Ptr::from_ref(val);
let c = c.forget_aligned();

// SAFETY:
// TODO(#899): This is unsound. `$ty` is not necessarily
// `IntoBytes`, but that's the corner we've backed ourselves
Expand Down
17 changes: 11 additions & 6 deletions src/pointer/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,15 +452,17 @@ mod _conversions {
}

/// `Ptr<'a, T>` → `&'a T`
impl<'a, T> Ptr<'a, T, (invariant::Shared, invariant::Aligned, invariant::Valid)>
impl<'a, T, I> Ptr<'a, T, I>
where
T: 'a + ?Sized,
I: Invariants<Alignment = invariant::Aligned, Validity = invariant::Valid>,
I::Aliasing: invariant::at_least::Shared,
{
/// Converts the `Ptr` to a shared reference.
// This consumes `self`, not `&self`, because `self` is, logically, a
// pointer. Since this method is only available for `invariant::Shared`,
// `Self: Copy`, and so this doesn't prevent the caller from still
// using the pointer after calling `as_ref`.
// pointer. For `I::Aliasing = invariant::Shared`, `Self: Copy`, and so
// this doesn't prevent the caller from still using the pointer after
// calling `as_ref`.
#[allow(clippy::wrong_self_convention)]
pub(crate) fn as_ref(self) -> &'a T {
let raw = self.as_non_null();
Expand All @@ -484,7 +486,10 @@ mod _conversions {
// `Valid`.
//
// 4. You must enforce Rust’s aliasing rules. This is ensured by
// contract on `Ptr`, because the `I::Aliasing` is `Shared`.
// contract on `Ptr`, because the `I::Aliasing` is
// `at_least::Shared`. Either it is `Shared` or `Exclusive`. In
// both cases, other references may not mutate the referent
// outside of `UnsafeCell`s.
//
// [1]: https://doc.rust-lang.org/std/ptr/struct.NonNull.html#method.as_ref
// [2]: https://doc.rust-lang.org/std/ptr/index.html#safety
Expand Down Expand Up @@ -745,7 +750,7 @@ mod _transitions {
if T::is_bit_valid(self.reborrow().forget_exclusive().forget_aligned()) {
// SAFETY: If `T::is_bit_valid`, code may assume that `self`
// contains a bit-valid instance of `Self`.
Some(unsafe { self.assume_validity::<invariant::Valid>() })
Some(unsafe { self.assume_valid() })
} else {
None
}
Expand Down

0 comments on commit a658f31

Please sign in to comment.