Skip to main content

NonNullPtr

Trait NonNullPtr 

Source
pub unsafe trait NonNullPtr:
    Inv
    + Sized
    + 'static {
    type Target;
    type Ref<'a>: Inv + 'a;
    type Permission: Inv;
    type RefPermission<'a>: Inv;

    const ALIGN_BITS: u32;

    // Required methods
    exec fn into_raw(self) -> (NonNull<Self::Target>, Tracked<Self::Permission>);
    unsafe fn from_raw(
        ptr: NonNull<Self::Target>,
        perm: Tracked<Self::Permission>,
    ) -> Self;
    spec fn ptr_perm_match(ptr: *mut Self::Target, perm: Self::Permission) -> bool;
    spec fn rel_perm(self, perm: Self::Permission) -> bool;
    proof fn lemma_align_bits_properties();
    spec fn ref_perm_view_permission<'a>(
        perm: Self::RefPermission<'a>,
    ) -> Self::Permission;
    spec fn ref_rel_perm<'a>(r: Self::Ref<'a>, perm: Self::RefPermission<'a>) -> bool;
    proof fn tracked_borrow_perm_as_ref_perm<'a>(
        tracked perm: &'a Self::Permission,
    ) -> tracked ret : Self::RefPermission<'a>;
    unsafe fn raw_as_ref<'a>(
        raw: NonNull<Self::Target>,
        perm: Tracked<Self::RefPermission<'a>>,
    ) -> Self::Ref<'a>;
    exec fn ref_as_raw<'a>(
        ptr_ref: Self::Ref<'a>,
    ) -> (NonNull<Self::Target>, Tracked<Self::RefPermission<'a>>);

    // Provided method
    open spec fn align_bits_inv() -> bool { ... }
}
Expand description

A trait that abstracts non-null pointers.

All common smart pointer types such as Box<T>, Arc<T>, and Weak<T> implement this trait as they can be converted to and from the raw pointer type of *const T.

§Safety

This trait must be implemented correctly (according to the doc comments for each method). Types like Rcu rely on this assumption to safely use the raw pointers.

Required Associated Constants§

Source

const ALIGN_BITS: u32

The power of two of the pointer alignment.

Required Associated Types§

Source

type Target

The target type that this pointer refers to.

Source

type Ref<'a>: Inv + 'a

A type that behaves just like a shared reference to the owning pointer. Self: 'a is implied by Self: 'static; omit that redundant GAT where-clause because the current Verus translator rejects it in associated-type impls.

Source

type Permission: Inv

A verification-only permission type that represents the ownership of the memory managed by the pointer.

Source

type RefPermission<'a>: Inv

A verification-only permission type that represents the reading permission of the memory managed by the pointer.

Required Methods§

Source

exec fn into_raw(self) -> (NonNull<Self::Target>, Tracked<Self::Permission>)

requires
self.inv(),
ensures
Self::ptr_perm_match(res_ptr.as_ptr(), perm@),
self.rel_perm(perm@),
perm@.inv(),
res_ptr.as_ptr().addr() % (1usize << Self::ALIGN_BITS) == 0,

Converts to a raw pointer.

Each call to into_raw must be paired with a call to from_raw in order to avoid memory leakage.

The lower Self::ALIGN_BITS of the raw pointer is guaranteed to be zero. In other words, the pointer is guaranteed to be aligned to 1 << Self::ALIGN_BITS. VERUS LIMITATION: the #[verus_spec] attribute does not support with in trait yet.

Source

unsafe exec fn from_raw( ptr: NonNull<Self::Target>, perm: Tracked<Self::Permission>, ) -> ret : Self

requires
Self::ptr_perm_match(ptr.as_ptr(), perm@),
perm@.inv(),
ensures
ret.rel_perm(perm@),
ret.inv(),

Converts back from a raw pointer.

§Safety
  1. The raw pointer must have been previously returned by a call to into_raw.
  2. The raw pointer must not be used after calling from_raw.

Note that the second point is a hard requirement: Even if the resulting value has not (yet) been dropped, the pointer cannot be used because it may break Rust aliasing rules (e.g., Box<T> requires the pointer to be unique and thus never aliased). VERIFICATION DESIGN: It’s easy to verify the second point by consuming the permission produced by into_raw, so we can do nothing with the raw pointer because of the absence of permission. VERUS LIMITATION: the #[verus_spec] attribute does not support with in trait yet.

Source

spec fn ptr_perm_match(ptr: *mut Self::Target, perm: Self::Permission) -> bool

A specification function that constraints the nonnull pointer and the permission returned by into_raw. This design is to support the tagged pointer trick used in Either.

Source

spec fn rel_perm(self, perm: Self::Permission) -> bool

A specification function that relates the original smart pointer and the permission.

Source

proof fn lemma_align_bits_properties()

ensures
Self::ALIGN_BITS < usize::BITS,
Self::align_bits_inv(),

Establishes ALIGN_BITS bounds and implementation-specific facts.

Source

spec fn ref_perm_view_permission<'a>( perm: Self::RefPermission<'a>, ) -> Self::Permission

The RefPermission must be able to be viewed as the owned Permission.

Source

spec fn ref_rel_perm<'a>(r: Self::Ref<'a>, perm: Self::RefPermission<'a>) -> bool

A specification function that relates the Ref type and the RefPermission.

Source

proof fn tracked_borrow_perm_as_ref_perm<'a>( tracked perm: &'a Self::Permission, ) -> tracked ret : Self::RefPermission<'a>

requires
perm.inv(),
ensures
Self::ref_perm_view_permission(ret) == *perm,
ret.inv(),

Borrows a reusable reading permission from the owned permission.

Source

unsafe exec fn raw_as_ref<'a>( raw: NonNull<Self::Target>, perm: Tracked<Self::RefPermission<'a>>, ) -> ret : Self::Ref<'a>

requires
Self::ptr_perm_match(raw.as_ptr(), Self::ref_perm_view_permission(perm@)),
perm@.inv(),
ensures
Self::ref_rel_perm(ret, perm@),
ret.inv(),

Obtains a shared reference to the original pointer.

§Safety

The original pointer must outlive the lifetime parameter 'a, and during 'a no mutable references to the pointer will exist.

Source

exec fn ref_as_raw<'a>( ptr_ref: Self::Ref<'a>, ) -> (NonNull<Self::Target>, Tracked<Self::RefPermission<'a>>)

requires
ptr_ref.inv(),
ensures
Self::ref_rel_perm(ptr_ref, perm@),
perm@.inv(),
Self::ptr_perm_match(res_ptr.as_ptr(), Self::ref_perm_view_permission(perm@)),
res_ptr.as_ptr().addr() % (1usize << Self::ALIGN_BITS) == 0,

Converts a shared reference to a raw pointer.

Provided Methods§

Source

open spec fn align_bits_inv() -> bool

{ true }

Optional implementation-specific alignment facts.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: 'static> NonNullPtr for Arc<T>

Source§

open spec fn align_bits_inv() -> bool

{ 1usize << Self::ALIGN_BITS == vstd::layout::align_of::<T>() }
Source§

exec fn into_raw(self) -> (NonNull<Self::Target>, Tracked<Self::Permission>)

Source§

unsafe exec fn from_raw( ptr: NonNull<Self::Target>, Tracked(perm): Tracked<Self::Permission>, ) -> Self

Source§

open spec fn ptr_perm_match(ptr: *mut Self::Target, perm: Self::Permission) -> bool

{ ptr == perm.ptr() }
Source§

open spec fn rel_perm(self, perm: Self::Permission) -> bool

{ perm.view_target() == *self }
Source§

proof fn lemma_align_bits_properties()

Source§

open spec fn ref_perm_view_permission<'a>( perm: Self::RefPermission<'a>, ) -> Self::Permission

{ perm }
Source§

open spec fn ref_rel_perm<'a>(r: Self::Ref<'a>, perm: Self::RefPermission<'a>) -> bool

{ perm.view_target() == *r@ }
Source§

proof fn tracked_borrow_perm_as_ref_perm<'a>( tracked perm: &'a Self::Permission, ) -> tracked Self::RefPermission<'a>

Source§

unsafe exec fn raw_as_ref<'a>( raw: NonNull<Self::Target>, perm: Tracked<Self::RefPermission<'a>>, ) -> Self::Ref<'a>

Source§

exec fn ref_as_raw<'a>( ptr_ref: Self::Ref<'a>, ) -> (NonNull<Self::Target>, Tracked<Self::RefPermission<'a>>)

Source§

const ALIGN_BITS: u32

Source§

type Target = T

Source§

type Permission = ArcPointsTo<T>

Source§

type Ref<'a> = ArcRef<'a, T>

Source§

type RefPermission<'a> = ArcPointsTo<T>

Source§

impl<T: 'static> NonNullPtr for Box<T>

Source§

open spec fn align_bits_inv() -> bool

{ 1usize << Self::ALIGN_BITS == vstd::layout::align_of::<T>() }
Source§

exec fn into_raw(self) -> (NonNull<Self::Target>, Tracked<Self::Permission>)

Source§

unsafe exec fn from_raw( ptr: NonNull<Self::Target>, Tracked(perm): Tracked<Self::Permission>, ) -> Self

Source§

open spec fn ptr_perm_match(ptr: *mut Self::Target, perm: Self::Permission) -> bool

{ ptr == perm.ptr() }
Source§

open spec fn rel_perm(self, perm: Self::Permission) -> bool

{ perm.view_target() == *self }
Source§

proof fn lemma_align_bits_properties()

Source§

open spec fn ref_perm_view_permission<'a>( perm: Self::RefPermission<'a>, ) -> Self::Permission

{ perm@ }
Source§

open spec fn ref_rel_perm<'a>(r: Self::Ref<'a>, perm: Self::RefPermission<'a>) -> bool

{
    &&& r.value() == perm@.value()
    &&& r.ptr() == perm@.ptr()

}
Source§

proof fn tracked_borrow_perm_as_ref_perm<'a>( tracked perm: &'a Self::Permission, ) -> tracked Self::RefPermission<'a>

Source§

unsafe exec fn raw_as_ref<'a>( raw: NonNull<Self::Target>, perm: Tracked<Self::RefPermission<'a>>, ) -> Self::Ref<'a>

Source§

exec fn ref_as_raw<'a>( ptr_ref: Self::Ref<'a>, ) -> (NonNull<Self::Target>, Tracked<Self::RefPermission<'a>>)

Source§

const ALIGN_BITS: u32

Source§

type Target = T

Source§

type Permission = BoxPointsTo<T>

Source§

type Ref<'a> = BoxRef<'a, T>

Source§

type RefPermission<'a> = BoxPointsToRef<'a, T>

Implementors§