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§
Sourceconst ALIGN_BITS: u32
const ALIGN_BITS: u32
The power of two of the pointer alignment.
Required Associated Types§
Sourcetype Ref<'a>: Inv + 'a
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.
Sourcetype Permission: Inv
type Permission: Inv
A verification-only permission type that represents the ownership of the memory managed by the pointer.
Sourcetype RefPermission<'a>: Inv
type RefPermission<'a>: Inv
A verification-only permission type that represents the reading permission of the memory managed by the pointer.
Required Methods§
Sourceexec fn into_raw(self) -> (NonNull<Self::Target>, Tracked<Self::Permission>)
exec fn into_raw(self) -> (NonNull<Self::Target>, Tracked<Self::Permission>)
self.inv(),ensuresSelf::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.
Sourceunsafe exec fn from_raw(
ptr: NonNull<Self::Target>,
perm: Tracked<Self::Permission>,
) -> ret : Self
unsafe exec fn from_raw( ptr: NonNull<Self::Target>, perm: Tracked<Self::Permission>, ) -> ret : Self
Self::ptr_perm_match(ptr.as_ptr(), perm@),perm@.inv(),ensuresret.rel_perm(perm@),ret.inv(),Converts back from a raw pointer.
§Safety
- The raw pointer must have been previously returned by a call to
into_raw. - 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.
Sourcespec fn ptr_perm_match(ptr: *mut Self::Target, perm: Self::Permission) -> bool
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.
Sourcespec fn rel_perm(self, perm: Self::Permission) -> bool
spec fn rel_perm(self, perm: Self::Permission) -> bool
A specification function that relates the original smart pointer and the permission.
Sourceproof fn lemma_align_bits_properties()
proof fn lemma_align_bits_properties()
Self::ALIGN_BITS < usize::BITS,Self::align_bits_inv(),Establishes ALIGN_BITS bounds and implementation-specific facts.
Sourcespec fn ref_perm_view_permission<'a>(
perm: Self::RefPermission<'a>,
) -> Self::Permission
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.
Sourcespec fn ref_rel_perm<'a>(r: Self::Ref<'a>, perm: Self::RefPermission<'a>) -> bool
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.
Sourceproof fn tracked_borrow_perm_as_ref_perm<'a>(
tracked perm: &'a Self::Permission,
) -> tracked ret : Self::RefPermission<'a>
proof fn tracked_borrow_perm_as_ref_perm<'a>( tracked perm: &'a Self::Permission, ) -> tracked ret : Self::RefPermission<'a>
perm.inv(),ensuresSelf::ref_perm_view_permission(ret) == *perm,ret.inv(),Borrows a reusable reading permission from the owned permission.
Sourceunsafe exec fn raw_as_ref<'a>(
raw: NonNull<Self::Target>,
perm: Tracked<Self::RefPermission<'a>>,
) -> ret : Self::Ref<'a>
unsafe exec fn raw_as_ref<'a>( raw: NonNull<Self::Target>, perm: Tracked<Self::RefPermission<'a>>, ) -> ret : Self::Ref<'a>
Self::ptr_perm_match(raw.as_ptr(), Self::ref_perm_view_permission(perm@)),perm@.inv(),ensuresSelf::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.
Sourceexec fn ref_as_raw<'a>(
ptr_ref: Self::Ref<'a>,
) -> (NonNull<Self::Target>, Tracked<Self::RefPermission<'a>>)
exec fn ref_as_raw<'a>( ptr_ref: Self::Ref<'a>, ) -> (NonNull<Self::Target>, Tracked<Self::RefPermission<'a>>)
ptr_ref.inv(),ensuresSelf::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§
Sourceopen spec fn align_bits_inv() -> bool
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>
impl<T: 'static> NonNullPtr for Arc<T>
Source§open spec fn align_bits_inv() -> bool
open spec fn align_bits_inv() -> bool
{ 1usize << Self::ALIGN_BITS == vstd::layout::align_of::<T>() }Source§unsafe exec fn from_raw(
ptr: NonNull<Self::Target>,
Tracked(perm): Tracked<Self::Permission>,
) -> Self
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
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
open spec fn rel_perm(self, perm: Self::Permission) -> bool
{ perm.view_target() == *self }Source§proof fn lemma_align_bits_properties()
proof fn lemma_align_bits_properties()
Source§open spec fn ref_perm_view_permission<'a>(
perm: Self::RefPermission<'a>,
) -> Self::Permission
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
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>
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>
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>>)
exec fn ref_as_raw<'a>( ptr_ref: Self::Ref<'a>, ) -> (NonNull<Self::Target>, Tracked<Self::RefPermission<'a>>)
const ALIGN_BITS: u32
type Target = T
type Permission = ArcPointsTo<T>
type Ref<'a> = ArcRef<'a, T>
type RefPermission<'a> = ArcPointsTo<T>
Source§impl<T: 'static> NonNullPtr for Box<T>
impl<T: 'static> NonNullPtr for Box<T>
Source§open spec fn align_bits_inv() -> bool
open spec fn align_bits_inv() -> bool
{ 1usize << Self::ALIGN_BITS == vstd::layout::align_of::<T>() }Source§unsafe exec fn from_raw(
ptr: NonNull<Self::Target>,
Tracked(perm): Tracked<Self::Permission>,
) -> Self
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
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
open spec fn rel_perm(self, perm: Self::Permission) -> bool
{ perm.view_target() == *self }Source§proof fn lemma_align_bits_properties()
proof fn lemma_align_bits_properties()
Source§open spec fn ref_perm_view_permission<'a>(
perm: Self::RefPermission<'a>,
) -> Self::Permission
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
open spec fn ref_rel_perm<'a>(r: Self::Ref<'a>, perm: Self::RefPermission<'a>) -> bool
{
&&& r.value() == perm@.value()
&&& r.ptr() == perm@.ptr()
}