rokdocs
v0.1

rok::core::fmt

rok::core::fmt - Formatter, Display / Debug, FormatSpec and the f-string lowering targets (stdlib 2.10, refs-lang 1.7.3).

Every rendering writes into a Formatter: the runtime's growing text plus the spec of the value being written. HIR lowers f"..." to one such text: literal pieces and primitive holes go to the runtime directly, any other hole through Display::fmt / Debug::fmt (fstr_* below), and rok_fmt_finish hands the text over as the str without a copy.

Structs

FmtError

pub struct FmtError

A failed write (2.10.12). Writing to memory never fails.

Trait implementations 2
  • impl Display for FmtError
  • impl Debug for FmtError

FormatSpec

pub struct FormatSpec

An f-string spec, [[fill]align][sign][#][0][width][.precision][type] (refs-lang 1.7.3). An absent ty is '\0'; the Display letters s d g read as absent.

  • pub fill: char
  • pub align: Alignment | none
  • pub sign: Sign | none
  • pub alt: bool
  • pub zero: bool
  • pub width: usize | none
  • pub precision: usize | none
  • pub ty: char
  • pub const fn new() -> FormatSpec

    The empty spec.

  • pub const fn parse(text: str) -> FormatSpec

    text parsed by the runtime with the parser the compiler checks static specs with; panics on a malformed spec.

Formatter

pub struct Formatter

Where a value's text goes, with the spec it is written by (2.10.2): the runtime's growing text (rok_fmt_*) and the spec packed in two words - flags: fill (bits 0..32), align, sign, type (a byte each from bit 32), # (bit 56), 0 (bit 57); counts: width, precision (32 bits each, all ones when absent).

  • pub const fn new() -> Formatter
  • pub const fn with_capacity(cap: usize) -> Formatter

    Empty, with room for cap bytes.

  • pub const fn with_spec(spec: FormatSpec) -> Formatter

    Empty, writing values per spec.

  • pub const fn finish(self) -> str

    The text written, as a str (no copy); the formatter is spent.

  • pub const fn write_str(ref self, s: str) throws (FmtError)
  • pub const fn write_char(ref self, c: char) throws (FmtError)
  • pub const fn pad(ref self, s: str) throws (FmtError)

    s cut to precision chars and padded to width (text aligns left by default).

  • pub const fn pad_integral(ref self, is_nonnegative: bool, prefix: str, buf: str) throws (FmtError)

    An integer: the sign, prefix ("0x" ..) under #, the digits buf, padded to width - right by default; 0 and = fill between the prefix and the digits.

  • pub const fn fill(self) -> char
  • pub const fn align(self) -> Alignment | none

    none when the spec names no alignment: numbers then align right, other values left.

  • pub const fn sign(self) -> Sign | none
  • pub const fn alternate(self) -> bool
  • pub const fn pad_zero(self) -> bool
  • pub const fn width(self) -> usize | none
  • pub const fn precision(self) -> usize | none

Enums

Alignment

pub enum Alignment

The spec's align.

  • Left
  • Right
  • Center
  • Equal

Sign

pub enum Sign

The spec's sign.

  • Plus
  • Minus
  • Space

Traits

Display

pub trait Display

What f"{x}" writes.

  • fn fmt(self, ref f: Formatter) throws (FmtError)

Debug

pub trait Debug

What f"{x:?}" writes.

  • fn fmt(self, ref f: Formatter) throws (FmtError)

Functions

fstr_display

pub const fn fstr_display<T: Display>(t: str, v: T) -> str

{v}.

fstr_debug

pub const fn fstr_debug<T: Debug>(t: str, v: T) -> str

{v:?}.

fstr_num

pub fn fstr_num<T: Display>(t: str, v: T, flags: u64, counts: u64) -> str

A number under a spec (flags, counts).

fstr_pack

pub const fn fstr_pack(spec: str, text_only: bool) -> (u64, u64)

A dynamic spec's packed words; panics when malformed, or when text_only and it has a number's type letter.

fstr_text_any

pub const fn fstr_text_any<T: Display + Debug>(t: str, v: T, spec: str) -> str

A dynamic spec on a value with both: ? picks the Debug; then cut and padded as text.