forall T, 'k.
struct Slice { ... }
A checked view of len contiguous Ts in domain 'k.
As with core::mem::Ref, Slice stores *T, never T inline, so it is Copy whatever T is.
Restricted because of len: a Ref has no field besides its pointer to lie about, and fabricating a different pointer needs an unsafe cast, but Slice's len is independent of the pointer's honesty. If the literal were open, safe code holding a live Ref[u8,'k] and its Exclusive!['k] could copy the honest pointer and brand into a Slice with an enormous len, then read and write far past the allocation through the safe get_ref/sub, which trust len unconditionally. No module but this one can write the struct literal or destructure a Slice; from_raw_parts is the sole, unsafe way in, so every construction site names its obligation.
Methods
method from_raw_partssource
unsafe fn from_raw_parts(ptr: *T, len: usize, marker: Brand['k]) -> Slice[T, 'k] { ... }
A view of the len elements at ptr, in the domain marker names.
The only way to build a Slice from outside this module. Caller: ptr[0 .. len) is live and readable and writable under marker's brand, and ptr is not null, even when len is 0 or T is zero-size and nothing is read through it. get_ref/sub are safe methods that trust exactly this (a Ref from get_ref is never null), so an honest ptr and len here are the whole of Slice's soundness boundary. Typical callers already hold a Ref of the same brand and assert that more elements are live starting there (Vec!::as_slice, Buffer!::open). String literals are not slices: they are read-only text, core::str::Str['static].
method into_partssource
fn into_parts(self) -> (*T, usize, Brand['k]) { ... }
The pointer, length and brand of this slice.
The safe inverse of from_raw_parts: the (ptr, len, marker) the slice was built from, for code that must read through it directly or see it as another view (Slice::as_str). Safe because it grants no authority: rebuilding a Slice from the result still goes through the unsafe from_raw_parts, and the raw pointer can only be dereferenced under unsafe.
method get_refsource
fn get_ref(self, i: usize) -> Option[Ref[T, 'k]] { ... }
A Ref of the same brand to element i, or None when i is out of range. Reads and writes then go through the Ref, under the capability its brand asks for: Ref::with and copy under the Exclusive! a context lends, Ref::read under a Shared! node. Mirrors Vec!::get_ref. Pure address projection with the same brand, like sub, so it needs no capability. The bounds check is what makes this sound: the brand only proves the allocation is live, not that i is inside it.
Option, not an abort and not a sentinel: this is a fallible lookup, the same shape as core::raw::raw_get (Option[T]) and the map lookups (HashMap!::get, Option[Ref[V,'k]]).
method getsource
forall C, R where Copy[T], Access[C -> 'k, R], Sized[T].
fn get(self, &cx: C, i: usize) -> Option[T] { ... }
A copy of element i, read through the capability cx lends, or None when i is out of range.
method atsource
forall C, R where Copy[T], Access[C -> 'k, R], Sized[T].
fn at(self, &cx: C, i: usize) -> T { ... }
A copy of element i; stops the program (core::process::trap) when i is out of range. get is the form for an index that may miss.
method lensource
fn len(self) -> usize { ... }
The number of elements.
method is_emptysource
fn is_empty(self) -> bool { ... }
Whether the slice has no elements.
method subsource
fn sub(self, start: usize, count: usize) -> Option[Slice[T, 'k]] { ... }
A sub-view starting start elements in, count elements long, or None if that range does not fit. Pure address projection with the same brand, so no capability is needed. There is no way back from a sub-view to a destroyable buffer: alloc::buffer::BufferRestore! exists so a sub-sliced Slice can never be used to reconstruct one.
method takesource
fn take(self, count: usize) -> Slice[T, 'k] { ... }
The first count elements, or the whole slice if it is shorter.
method skipsource
fn skip(self, count: usize) -> Slice[T, 'k] { ... }
Everything after the first count elements, or an empty slice at the end.
method slicesource
fn slice(self, start: usize, end: usize) -> Slice[T, 'k] { ... }
Elements start to end, each clamped to the slice (an empty slice when start is past end).
method itersource
forall C, R where Access[C -> 'k, R].
fn iter(self, cx: C) -> SliceIter![T, 'k, C, R] { ... }
The elements in order, read under the capability cx lends, which the iterator's finish hands back.
method as_strsource
fn as_str(self) -> Str['k] { ... }
Defined in core::str.
The bytes the slice views, as text of the same domain. Safe: the slice's construction already vouched that its bytes are live and readable under its brand, which is all text asks, and text only reads them.
Instances
instance ReadBytes[Exclusive!['r], Slice[u8, 'r]]source
forall 'r.
instance ReadBytes[Exclusive!['r], Slice[u8, 'r]] { ... }
In core::slice.
A byte slice is read through its domain's Exclusive!.