NanoId structv0.1.0

Source
A Nano ID of the standard form: twenty-one characters from NanoIdAlphabet.
pub struct NanoId {
    // private fields
}

Copy, holding its text inline and owning nothing. Equality and ordering compare the text byte by byte.

Associated functions

Generate

pub func Generate() -> NanoId ! EntropyError

A new identifier from the operating system's generator. Costs twenty-one bytes of entropy and allocates nothing. Reports what the system reported when it could not supply random bytes, and produces no identifier in that case.

Returns: the identifier, or why none could be made

Source

Parse

pub func Parse(text: char8[..]) -> NanoId ! NanoIdParseError

Reads a standard identifier from its text. Reports WrongLength unless the text is exactly twenty-one bytes, and InvalidCharacter at the first byte outside the default alphabet. A refused parse produces no value. Runs in constant time for a text of the right length and allocates nothing.

NameTypeDescription
textchar8[..]the text to read

Returns: the identifier, or why the text is not one

Source

Methods

Text

pub func Text(self: &NanoId) -> char8[..]

The identifier's twenty-one characters. Borrows the value: the slice reads the value's own storage and must not outlive it. Cannot fail and allocates nothing.

Returns: the text, always NanoIdLength bytes of the default alphabet

Source

Equals

pub func Equals(self: &NanoId, other: &NanoId) -> bool

Whether two identifiers are the same. Compares every byte whatever it finds, so the time taken does not reveal how much of a guessed identifier was right. Cannot fail and allocates nothing.

NameTypeDescription
other&NanoIdthe identifier to compare against

Returns: whether the two have the same text

Source

Compare

pub func Compare(self: &NanoId, other: &NanoId) -> Ordering

The order of two identifiers' texts, byte by byte. Identifiers are random, so this order means nothing beyond being total and stable, which is what a sorted container needs. Cannot fail and allocates nothing.

NameTypeDescription
other&NanoIdthe identifier to compare against

Returns: how this identifier orders against other

Source

Implements Display

NanoId conforms to Display.

WriteDisplay

pub func WriteDisplay(
    self: &NanoId,
    writer: &var TextWriter,
    spec: FormatSpec
) -> ! FormatError

Writes the identifier's twenty-one characters. No style is understood. Reports whatever the writer reported, and UnsupportedRequest for a style.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole identifier was accepted

Source

Implements Debug

NanoId conforms to Debug.

WriteDebug

pub func WriteDebug(
    self: &NanoId,
    writer: &var TextWriter,
    spec: FormatSpec
) -> ! FormatError

The same text as Display: an identifier has one reading. The failures WriteDisplay reports.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole identifier was accepted

Source

NanoIdParseError

pub variant NanoIdParseError {
    WrongLength(uint),
    InvalidCharacter(uint)
}

Why text could not be read as a standard Nano ID. Failure-only, carrying the zero-based byte offset where the problem was found. Copy, owning nothing.

Source

Cases

NameDescription
WrongLength(uint)The text is not twenty-one bytes long. The offset is the text's length when it is too short, and the first byte past the form when it is too long.
InvalidCharacter(uint)A character that is not in the default alphabet. The offset is that character.

Implements ParseFailure

NanoIdParseError conforms to ParseFailure.

Offset

pub func Offset(self: &NanoIdParseError) -> uint

Where in the input the failure was found. Borrows the error and leaves it untouched. Cannot fail; runs in constant time and allocates nothing.

Returns: the zero-based UTF-8 byte offset, which is the text's length at end of input

Source