Skip to content

Commit 99b49b7

Browse files
committed
[pointer] Rename Any -> Unknown, remove aliasing
Don't implement `Aliasing` for `Unknown` - every `Ptr` always has *some* aliasing that is known at construction.
1 parent 3e67424 commit 99b49b7

File tree

5 files changed

+17
-27
lines changed

5 files changed

+17
-27
lines changed

src/pointer/invariant.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,11 @@ pub trait Validity: Sealed {}
8181
/// Exclusive`.
8282
pub trait Reference: Aliasing + Sealed {}
8383

84-
/// No requirement - any invariant is allowed.
85-
pub enum Any {}
86-
impl Aliasing for Any {
87-
const IS_EXCLUSIVE: bool = false;
84+
/// It is unknown whether any invariant holds.
85+
pub enum Unknown {}
8886

89-
// SAFETY: Since we don't know what aliasing model this is, we have to be
90-
// conservative. Invariance is strictly more restrictive than any other
91-
// variance model, so this can never cause soundness issues.
92-
//
93-
// `fn() -> T` and `fn(T) -> ()` are covariant and contravariant in `T`,
94-
// respectively. [1] Thus, `fn(T) -> T` is invariant in `T`. Thus, `fn(&'a
95-
// T) -> &'a T` is invariant in `'a` and `T`.
96-
//
97-
// [1] https://doc.rust-lang.org/1.81.0/reference/subtyping.html#variance
98-
type Variance<'a, T: 'a + ?Sized> = fn(&'a T) -> &'a T;
99-
}
100-
impl Alignment for Any {}
101-
impl Validity for Any {}
87+
impl Alignment for Unknown {}
88+
impl Validity for Unknown {}
10289

10390
/// The `Ptr<'a, T>` adheres to the aliasing rules of a `&'a T`.
10491
///
@@ -212,7 +199,7 @@ mod sealed {
212199

213200
pub trait Sealed {}
214201

215-
impl Sealed for Any {}
202+
impl Sealed for Unknown {}
216203

217204
impl Sealed for Shared {}
218205
impl Sealed for Exclusive {}

src/pointer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ use crate::Unaligned;
2424
/// to [`TryFromBytes::is_bit_valid`].
2525
///
2626
/// [`TryFromBytes::is_bit_valid`]: crate::TryFromBytes::is_bit_valid
27-
pub type Maybe<'a, T, Aliasing = invariant::Shared, Alignment = invariant::Any> =
27+
pub type Maybe<'a, T, Aliasing = invariant::Shared, Alignment = invariant::Unknown> =
2828
Ptr<'a, T, (Aliasing, Alignment, invariant::Initialized)>;
2929

3030
/// A semi-user-facing wrapper type representing a maybe-aligned reference, for
3131
/// use in [`TryFromBytes::is_bit_valid`].
3232
///
3333
/// [`TryFromBytes::is_bit_valid`]: crate::TryFromBytes::is_bit_valid
34-
pub type MaybeAligned<'a, T, Aliasing = invariant::Shared, Alignment = invariant::Any> =
34+
pub type MaybeAligned<'a, T, Aliasing = invariant::Shared, Alignment = invariant::Unknown> =
3535
Ptr<'a, T, (Aliasing, Alignment, invariant::Valid)>;
3636

3737
// These methods are defined on the type alias, `MaybeAligned`, so as to bring

src/pointer/ptr.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ mod _transitions {
676676
#[doc(hidden)]
677677
#[must_use]
678678
#[inline]
679-
pub const fn forget_aligned(self) -> Ptr<'a, T, I::WithAlignment<Any>> {
679+
pub const fn forget_aligned(self) -> Ptr<'a, T, I::WithAlignment<Unknown>> {
680680
// SAFETY: `Any` is less restrictive than `Aligned`.
681681
unsafe { self.assume_invariants() }
682682
}
@@ -714,7 +714,7 @@ mod _casts {
714714
pub unsafe fn cast_unsized_unchecked<U: 'a + ?Sized, F: FnOnce(*mut T) -> *mut U>(
715715
self,
716716
cast: F,
717-
) -> Ptr<'a, U, (I::Aliasing, Any, Any)> {
717+
) -> Ptr<'a, U, (I::Aliasing, Unknown, Unknown)> {
718718
let ptr = cast(self.as_inner().as_non_null().as_ptr());
719719

720720
// SAFETY: Caller promises that `cast` returns a pointer whose
@@ -784,7 +784,10 @@ mod _casts {
784784
/// - `u` has the same provenance as `p`
785785
#[doc(hidden)]
786786
#[inline]
787-
pub unsafe fn cast_unsized<U, F, R, S>(self, cast: F) -> Ptr<'a, U, (I::Aliasing, Any, Any)>
787+
pub unsafe fn cast_unsized<U, F, R, S>(
788+
self,
789+
cast: F,
790+
) -> Ptr<'a, U, (I::Aliasing, Unknown, Unknown)>
788791
where
789792
T: Read<I::Aliasing, R>,
790793
U: 'a + ?Sized + Read<I::Aliasing, S>,
@@ -1070,7 +1073,7 @@ mod _project {
10701073
pub unsafe fn project<U: 'a + ?Sized>(
10711074
self,
10721075
projector: impl FnOnce(*mut T) -> *mut U,
1073-
) -> Ptr<'a, U, (I::Aliasing, Any, Initialized)> {
1076+
) -> Ptr<'a, U, (I::Aliasing, Unknown, Initialized)> {
10741077
// TODO(#1122): If `cast_unsized` were able to reason that, when
10751078
// casting from an `Initialized` pointer, the result is another
10761079
// `Initialized` pointer, we could remove this method entirely.

src/util/macro_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ pub unsafe fn transmute_mut<'dst, 'src: 'dst, Src: 'src, Dst: 'dst>(
520520
fn try_cast_or_pme<Src, Dst, I, R>(
521521
src: Ptr<'_, Src, I>,
522522
) -> Result<
523-
Ptr<'_, Dst, (I::Aliasing, invariant::Any, invariant::Valid)>,
523+
Ptr<'_, Dst, (I::Aliasing, invariant::Unknown, invariant::Valid)>,
524524
ValidityError<Ptr<'_, Src, I>, Dst>,
525525
>
526526
where

src/util/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ impl<I: invariant::Validity> ValidityVariance<I> for Covariant {
9999
pub enum Invariant {}
100100

101101
impl<I: invariant::Alignment> AlignmentVariance<I> for Invariant {
102-
type Applied = invariant::Any;
102+
type Applied = invariant::Unknown;
103103
}
104104

105105
impl<I: invariant::Validity> ValidityVariance<I> for Invariant {
106-
type Applied = invariant::Any;
106+
type Applied = invariant::Unknown;
107107
}
108108

109109
// SAFETY:

0 commit comments

Comments
 (0)