docs

你正在阅读 v0.1.0 的文档。Nabi 0.1.0 已不再提供支持。 最新版本:v0.1.3 →

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*).

  • pub const fn dangling() -> Ptr<T>§

    Source

    Well aligned, never dereferenceable.

  • pub fn null() -> Ptr<T> | none§

    Source

    Always none (a Ptr is never null).

  • pub const fn addr(self) -> usize§

    Source

    The address (provenance is lost).

  • pub const fn with_addr(self, addr: usize) -> Ptr<T>§

    Source

    The same pointer at address addr.

  • pub const fn map_addr(self, f: fn(usize) -> usize) -> Ptr<T>§

    Source

    The same pointer at the address f maps the current one to.

  • pub fn offset(self, count: isize) -> Ptr<T>§

    Source

    count elements further (count * size_of::<T>() bytes).

  • pub const fn add(self, count: usize) -> Ptr<T>§

    Source

    count elements further; no overflow check.

  • pub const fn sub(self, count: usize) -> Ptr<T>§

    Source

    count elements back; no overflow check.

  • pub fn byte_offset(self, count: isize) -> Ptr<T>§

    Source

    count bytes further.

  • pub const fn distance(self, origin: Ptr<T>) -> isize§

    Source

    self.addr() - origin.addr() in elements.

  • pub const fn is_aligned(self) -> bool§

    Source

    true when the address is a multiple of align_of::<T>().

  • pub const fn align_offset(self, align: usize) -> usize§

    Source

    Elements to add so the address is a multiple of align, or usize::MAX when no element count gets there.

  • pub const fn cast<U>(self) -> Ptr<U>§

    Source

    The same address as a pointer to U (alignment is the caller's).

  • pub const fn as_const(self) -> ConstPtr<T>§

    Source

    The same address as a read-only pointer.

  • pub const fn as_many(self) -> ManyPtr<T>§

    Source

    The same address as a pointer to a run of Ts.

  • pub unsafe fn read(self) -> T§

    Source

    The T it points at. UB unless it points at a live, initialized T.

  • pub unsafe fn write(self, value: T)§

    Source

    Store value where it points. UB unless that is writable T memory.

  • pub unsafe fn read_volatile(self) -> T§

    Source

    read that the optimizer keeps exactly as written (volatile).

  • pub unsafe fn write_volatile(self, value: T)§

    Source

    write that the optimizer keeps exactly as written (volatile).

Trait implementations 10

ConstPtrSource

pub struct ConstPtr<T>

A non-null pointer to one T that is only read (C const T*).

  • pub const fn dangling() -> ConstPtr<T>§

    Source

    Well aligned, never dereferenceable.

  • pub const fn addr(self) -> usize§

    Source

    The address (provenance is lost).

  • pub const fn with_addr(self, addr: usize) -> ConstPtr<T>§

    Source

    The same pointer at address addr.

  • pub fn offset(self, count: isize) -> ConstPtr<T>§

    Source

    Moved by count elements, either way; no overflow check.

  • pub const fn add(self, count: usize) -> ConstPtr<T>§

    Source

    count elements further; no overflow check.

  • pub fn sub(self, count: usize) -> ConstPtr<T>§

    Source

    Moved back by count elements; no overflow check.

  • pub fn cast<U>(self) -> ConstPtr<U>§

    Source

    The same address as a pointer to U (alignment is the caller's).

  • pub fn as_many(self) -> ManyPtr<T>§

    Source

    The same address as a pointer to a run of Ts.

  • pub unsafe fn read(self) -> T§

    Source

    The T it points at. UB unless it points at a live, initialized T.

  • pub unsafe fn read_volatile(self) -> T§

    Source

    read that the optimizer keeps exactly as written (volatile).

Trait implementations 10
  • impl<T> Debug for ConstPtr<T>
  • impl<T> Clone for ConstPtr<T>
  • impl<T> Copy for ConstPtr<T>
  • impl<T> PartialEq for ConstPtr<T>
  • impl<T> Eq for ConstPtr<T>
  • impl<T> PartialOrd for ConstPtr<T>
  • impl<T> Ord for ConstPtr<T>
  • impl<T> Hash for ConstPtr<T>
  • impl<T> !Send for ConstPtr<T>
  • impl<T> !Sync for ConstPtr<T>

ManyPtrSource

pub struct ManyPtr<T>

A pointer to a run of Ts of unknown length (C T* array).

  • pub const fn addr(self) -> usize§

    Source

    The address (provenance is lost).

  • pub fn with_addr(self, addr: usize) -> ManyPtr<T>§

    Source

    The same pointer at address addr.

  • pub fn offset(self, count: isize) -> ManyPtr<T>§

    Source

    Moved by count elements, either way; no overflow check.

  • pub fn add(self, count: usize) -> ManyPtr<T>§

    Source

    Moved forward by count elements; no overflow check.

  • pub const fn as_one(self) -> Ptr<T>§

    Source

    The first element as a single-element pointer.

  • pub const fn cast<U>(self) -> ManyPtr<U>§

    Source

    The same address as a pointer to Us (alignment is the caller's).

  • pub fn as_const(self) -> ConstPtr<T>§

    Source

    The same address as a read-only pointer to a run of Ts.

  • pub unsafe fn read(self, idx: usize) -> T§

    Source

    Element idx (no bounds: the caller knows the length).

  • pub unsafe fn write(self, idx: usize, value: T)§

    Source

    Store value as element idx (no bounds: the caller knows the length). UB unless that is writable T memory.

  • pub unsafe fn slice(self, len: usize) -> Slice<T>§

    Source

    The len Ts from here as a Slice<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
  • impl<T> Debug for ManyPtr<T>
  • impl<T> Clone for ManyPtr<T>
  • impl<T> Copy for ManyPtr<T>
  • impl<T> PartialEq for ManyPtr<T>
  • impl<T> Eq for ManyPtr<T>
  • impl<T> PartialOrd for ManyPtr<T>
  • impl<T> Ord for ManyPtr<T>
  • impl<T> Hash for ManyPtr<T>
  • impl<T> !Send for ManyPtr<T>
  • impl<T> !Sync for ManyPtr<T>

CPtrSource

pub struct CPtr<T>

A C-compatible nullable pointer (T* that may be NULL).

  • pub const fn null() -> CPtr<T>§

    Source

    The null pointer.

  • pub fn from_ptr(p: Ptr<T>) -> CPtr<T>§

    Source

    The non-null p as a nullable pointer.

  • pub const fn from_const(p: ConstPtr<T>) -> CPtr<T>§

    Source

    The read-only p as a nullable pointer (C const T*).

  • pub const fn from_addr(addr: usize) -> CPtr<T>§

    Source

    A pointer at address addr; 0 is null.

  • pub const fn is_null(self) -> bool§

    Source

    true when the address is 0.

  • pub const fn addr(self) -> usize§

    Source

    The address; 0 when null.

  • pub const fn as_ptr(self) -> Ptr<T> | none§

    Source

    none when null.

  • pub fn as_const(self) -> ConstPtr<T> | none§

    Source

    The same address read-only; none when null.

Trait implementations 10
  • impl<T> Debug for CPtr<T>
  • impl<T> Clone for CPtr<T>
  • impl<T> Copy for CPtr<T>
  • impl<T> PartialEq for CPtr<T>
  • impl<T> Eq for CPtr<T>
  • impl<T> PartialOrd for CPtr<T>
  • impl<T> Ord for CPtr<T>
  • impl<T> Hash for CPtr<T>
  • impl<T> !Send for CPtr<T>
  • impl<T> !Sync for CPtr<T>

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.