Struct

core::str::Str

source
forall 'k.
struct Str { ... }

Text: len bytes at ptr, in domain 'k, read and never written through.

Branded and restricted: a Str names bytes in domain 'k, and only this module can build one or see its pointer. Reading the bytes needs a capability that may read the domain (core::slice::ReadBytes): the Exclusive!['k] itself, a node of its shared-read tree, or a context that lends one (an arena's alloc::arena::Domain!, a program's own context), so a Str cannot be read once its domain's Exclusive!['k] is gone; a Str['static] is read through StaticRead. marker: Brand['k] costs nothing at runtime (Brand is Copy and zero-size) and does not make Str linear.

A literal is a Str['static], and enters a domain through Str::from_static; text over memory the caller just filled uses unsafe Str::from_raw_parts. Both take the domain's capability, so text is planted only in a domain its maker holds. from_raw_parts_branded takes a brand instead, for a module whose own object holds it (an arena), and is unsafe for that reason: a brand is Copy and proves nothing. A writable Slice[u8, 'k] is seen as text by as_str.

The methods that read take the capability by place, &cx. Generic code over any readable byte view goes through ReadBytes too (core::slice's byte_or_zero, find_bytes, bytes_eq_within, ...).

Methods

method from_raw_partssource

forall C, R where Access[C -> 'k, R].
unsafe fn from_raw_parts(&cx: C, ptr: *u8, len: usize) -> Str['k] { ... }

The len bytes at ptr as text of domain 'k.

Caller: ptr[0 .. len) is initialized and stays readable for as long as the domain 'k's capability exists. Only that capability's holder may change the bytes, and only through a writable view of them.

method from_raw_parts_brandedsource

unsafe fn from_raw_parts_branded(ptr: *u8, len: usize, marker: Brand['k]) -> Str['k] { ... }

The len bytes at ptr as text, branded with marker.

The same, for a module whose own object already carries the domain's brand (an arena's marker, set once from Exclusive!::fresh), and for string literals, which the compiler builds with the 'static brand. Caller: as for from_raw_parts, and marker is a witness that object really holds: text planted in someone else's domain reads their bytes. With the 'static brand the bytes stay unchanged for the rest of the process, as StaticRead assumes.

method emptysource

forall C, R where Access[C -> 'k, R].
fn empty(&cx: C) -> Str['k] { ... }

No bytes at all; nothing to read.

method from_staticsource

forall C, R where Access[C -> 'k, R].
fn from_static(&cx: C, text: Str['static]) -> Str['k] { ... }

A literal as text of domain 'k. Static bytes outlive every domain, and nothing writes through a Str, so reading them under 'k's capability is always valid.

method into_partssource

fn into_parts(self) -> (*u8, usize) { ... }

The pointer and length back; rebuilding text from them needs from_raw_parts again. The brand stays inside: text may be made from the capability alone, so it must not hand out a witness.

method lensource

fn len(self) -> usize { ... }

The number of bytes.

method is_emptysource

fn is_empty(self) -> bool { ... }

Whether there are no bytes.

method addresssource

fn address(self) -> usize { ... }

Where the text starts, as a number. Like Ref::address, it hands out nothing to read through; it lets text be matched against a known buffer (a diagnostic finding the line a name came from).

method takesource

fn take(self, count: usize) -> Str['k] { ... }

The first count bytes, or all of them if there are fewer.

method skipsource

fn skip(self, count: usize) -> Str['k] { ... }

Everything after the first count bytes, or empty text at the end.

method slicesource

fn slice(self, start: usize, end: usize) -> Str['k] { ... }

Bytes start to end, each clamped to the text (empty text when start is past end).

method subsource

fn sub(self, start: usize, count: usize) -> Option[Str['k]] { ... }

count bytes starting start in, or None past the end. Checked by subtraction so a huge count cannot wrap into range.

method getsource

forall C where ReadBytes[C, Str['k]].
fn get(self, &cx: C, i: usize) -> Option[u8] { ... }

Byte i, or None past the end.

method atsource

forall C where ReadBytes[C, Str['k]].
fn at(self, &cx: C, i: usize) -> u8 { ... }

Byte i; stops the program (core::process::trap) past the end. get is the form for an index that may miss.

method byte_or_zerosource

forall C where ReadBytes[C, Str['k]].
fn byte_or_zero(self, &cx: C, i: usize) -> u8 { ... }

Byte i, or 0u8 past the end: a sentinel for scanners whose character-class tests already treat 0u8 as matching nothing (core::slice::byte_or_zero).

method trim_startsource

forall C where ReadBytes[C, Str['k]].
fn trim_start(self, &cx: C) -> Str['k] { ... }

Without the ASCII whitespace at its start (core::ascii::is_space).

method trim_endsource

forall C where ReadBytes[C, Str['k]].
fn trim_end(self, &cx: C) -> Str['k] { ... }

Without the ASCII whitespace at its end.

method trimsource

forall C where ReadBytes[C, Str['k]].
fn trim(self, &cx: C) -> Str['k] { ... }

Without the ASCII whitespace at either end.

method eqsource

forall C where ReadBytes[C, Str['k]].
fn eq(self, &cx: C, other: Str['k]) -> bool { ... }

Byte equality with other text of the domain.

method issource

forall C where ReadBytes[C, Str['k]].
fn is(self, &cx: C, literal: Str['static]) -> bool { ... }

Byte equality with a literal.

method cmpsource

forall C where ReadBytes[C, Str['k]].
fn cmp(self, &cx: C, other: Str['k]) -> Order { ... }

Byte order, as unsigned values, a shorter prefix first (core::slice::bytes_cmp).

method starts_withsource

forall C where ReadBytes[C, Str['k]].
fn starts_with(self, &cx: C, prefix: Str['k]) -> bool { ... }

Whether the bytes start with prefix's.

method ends_withsource

forall C where ReadBytes[C, Str['k]].
fn ends_with(self, &cx: C, suffix: Str['k]) -> bool { ... }

Whether the bytes end with suffix's.

method findsource

forall C where ReadBytes[C, Str['k]].
fn find(self, &cx: C, needle: Str['k], start: usize) -> Option[usize] { ... }

The first index at or after start where needle's bytes appear (core::slice::find_bytes).

method containssource

forall C where ReadBytes[C, Str['k]].
fn contains(self, &cx: C, needle: Str['k]) -> bool { ... }

Whether needle's bytes appear in the text.

method find_bytesource

forall C where ReadBytes[C, Str['k]].
fn find_byte(self, &cx: C, byte: u8, start: usize) -> Option[usize] { ... }

The first index at or after start holding byte (core::slice::find_byte).

method rfind_bytesource

forall C where ReadBytes[C, Str['k]].
fn rfind_byte(self, &cx: C, byte: u8) -> Option[usize] { ... }

The last index holding byte (core::slice::rfind_byte).

Instances

instance Borrow[Str['k], C, Readable[C, Str['k]]]source

forall C, 'k where ReadBytes[C, Str['k]].
instance Borrow[Str['k], C, Readable[C, Str['k]]] { ... }

In core::borrow.

Text borrows as a Readable through any capability that can read it.

instance Printable[Str['static], W]source

forall W where Writer[W].
instance Printable[Str['static], W] { ... }

In core::fmt.

'static text, such as a literal, prints as its bytes.

instance Format[Str['r], ReadingWriter![W, C]]source

forall W, C, 'r where Writer[W], ReadBytes[C, Str['r]].
instance Format[Str['r], ReadingWriter![W, C]] { ... }

In core::fmt.

Text formats as its bytes, read through the writer's context: f"{name}".write(&cx, out) with name: Str['k]. A Str['static] in an f-string is written directly, as literal text is.

instance ReadBytes[Exclusive!['r], Str['r]]source

forall 'r.
instance ReadBytes[Exclusive!['r], Str['r]] { ... }

In core::str.

Text is read through its domain's Exclusive!.

instance ReadBytes[Shared!['r, 'q], Str['r]]source

forall 'r, 'q.
instance ReadBytes[Shared!['r, 'q], Str['r]] { ... }

In core::str.

Text is read through any node of its domain's shared-read tree.

instance ReadBytes[StaticRead, Str['static]]source

instance ReadBytes[StaticRead, Str['static]] { ... }

In core::str.

'static text is read through StaticRead.

instance HashIn[Str['t], C]source

forall C, 't where ReadBytes[C, Str['t]].
instance HashIn[Str['t], C] { ... }

In core::str.

Text hashes and compares by its bytes, read through the capability C.

instance ReadBytes[Domain!['k], Str['k]]source

forall 'k.
instance ReadBytes[Domain!['k], Str['k]] { ... }

In alloc::arena.

Text of a domain is read through the domain itself, which lends its capability to read it.