bare::ptr
本頁內容
API 參考只有英文版,由標準函式庫原始碼中的英文文件註解產生。
raw pointers. A pointer is its
address; reading / writing through it is unsafe.
Provenance is not tracked at run time: expose_provenance /
from_exposed_provenance are the address itself, and the latter is
unsafe (it makes a pointer out of a plain integer). Ptr /
ConstPtr / ManyPtr are never null: #[niche(0)] makes
Ptr<T> | none one address, null = none.
Structs
PtrSource
pub struct Ptr<T>A non-null, mutable pointer to one T (C T*).
Well aligned, never dereferenceable.
Always
none(aPtris never null).The address (provenance is lost).
The same pointer at address
addr.The same pointer at the address
fmaps the current one to.countelements further (count * size_of::<T>()bytes).countelements further; no overflow check.countelements back; no overflow check.countbytes further.self.addr() - origin.addr()in elements.truewhen the address is a multiple ofalign_of::<T>().Elements to
addso the address is a multiple ofalign, orusize::MAXwhen no element count gets there.The same address as a pointer to
U(alignment is the caller's).The same address as a read-only pointer.
The same address as a pointer to a run of
Ts.The
Tit points at. UB unless it points at a live, initializedT.Store
valuewhere it points. UB unless that is writableTmemory.readthat the optimizer keeps exactly as written (volatile).writethat the optimizer keeps exactly as written (volatile).
ConstPtrSource
pub struct ConstPtr<T>A non-null pointer to one T that is only read (C const T*).
Well aligned, never dereferenceable.
The address (provenance is lost).
The same pointer at address
addr.Moved by
countelements, either way; no overflow check.countelements further; no overflow check.Moved back by
countelements; no overflow check.The same address as a pointer to
U(alignment is the caller's).The same address as a pointer to a run of
Ts.The
Tit points at. UB unless it points at a live, initializedT.readthat the optimizer keeps exactly as written (volatile).
Trait implementations 10
ManyPtrSource
pub struct ManyPtr<T>A pointer to a run of Ts of unknown length (C T* array).
The address (provenance is lost).
The same pointer at address
addr.Moved by
countelements, either way; no overflow check.Moved forward by
countelements; no overflow check.The first element as a single-element pointer.
The same address as a pointer to
Us (alignment is the caller's).The same address as a read-only pointer to a run of
Ts.Element
idx(no bounds: the caller knows the length).Store
valueas elementidx(no bounds: the caller knows the length). UB unless that is writableTmemory.The
lenTs from here as aSlice<T>; they must stay valid while the view is used, and the GC neither traces nor moves them (elements holding GC references are UB here).
Trait implementations 10
CPtrSource
pub struct CPtr<T>A C-compatible nullable pointer (T* that may be NULL).
The null pointer.
The non-null
pas a nullable pointer.The read-only
pas a nullable pointer (Cconst T*).A pointer at address
addr; 0 is null.truewhen the address is 0.The address; 0 when null.
nonewhen null.The same address read-only;
nonewhen null.
Functions
ptr_castSource
pub const fn ptr_cast<U, T>(p: Ptr<T>) -> Ptr<U> The same address as a pointer to U (ptr_cast::<U>(p);
alignment is the caller's problem).
align_castSource
pub const fn align_cast<T>(p: Ptr<u8>) -> Ptr<T> | none p as a Ptr<T> when its address suits T's alignment.
align_cast_uncheckedSource
pub const fn align_cast_unchecked<T>(p: Ptr<u8>) -> Ptr<T> p as a Ptr<T> with no alignment check, not even in debug builds.
const_castSource
pub const fn const_cast<T>(p: ConstPtr<T>) -> Ptr<T> Drop the const: writing through the result is the caller's
responsibility.
expose_provenanceSource
pub const fn expose_provenance<T>(p: Ptr<T>) -> usize The address of p, marked as escaping (strict provenance).
addr_ofSource
pub fn addr_of<T>(place: T) -> Ptr<T> A pointer to place itself (a local, or a field / array element of
one), valid while that local is in scope: the local lives in memory
and, holding GC references, stays a GC root (whose referents never
move) until its scope ends.
many_addr_ofSource
pub fn many_addr_of<T>(place: T) -> ManyPtr<T> addr_of as the start of a run of Ts (many_addr_of(buf[0])).
Constants
unsafeSource
pub const unsafe fn from_exposed_provenance<T>(addr: usize) -> Ptr<T> A pointer at addr, assumed to carry an exposed provenance.