Skip to main content

WtfEncoding

Trait WtfEncoding 

Source
pub trait WtfEncoding {
    type Unit: Copy + Ord + Hash + Debug;

    const NUL: Self::Unit;

    // Required methods
    fn encode_str(s: &str) -> Vec<Self::Unit>;
    fn decode(units: &[Self::Unit]) -> Option<String>;
    fn decode_lossy(units: &[Self::Unit]) -> String;

    // Provided methods
    fn eq_str(units: &[Self::Unit], s: &str) -> bool { ... }
    fn debug_fmt(units: &[Self::Unit], f: &mut Formatter<'_>) -> Result { ... }
}
Expand description

A code-unit encoding for a WtfString / WtfStr.

This trait is the storage-width seam: the API common to every width is written against E: WtfEncoding, while width-specific API (such as the *const u16 FFI surface) lives in inherent impls on the concrete instantiations. The two shipped encodings are Wtf16 (u16 units) and Wtf8 (u8 units); each defines its own encode/decode/comparison/formatting semantics over a crate-owned Vec<Unit> with the same always-terminated model.

Required Associated Constants§

Source

const NUL: Self::Unit

The NUL code unit (U+0000) used as the always-present buffer terminator.

Changing this value is a breaking change to the storage format.

Required Associated Types§

Source

type Unit: Copy + Ord + Hash + Debug

The code unit this encoding stores (u16 for Wtf16).

Required Methods§

Source

fn encode_str(s: &str) -> Vec<Self::Unit>

Encode a UTF-8 str into this encoding’s code units.

Source

fn decode(units: &[Self::Unit]) -> Option<String>

Decode content code units to a String if they are well-formed for this encoding, or None if they are ill-formed (e.g. an unpaired surrogate), which a strict String cannot represent.

Source

fn decode_lossy(units: &[Self::Unit]) -> String

Decode content code units to a String, replacing any ill-formed sequence with the Unicode replacement character (U+FFFD).

Provided Methods§

Source

fn eq_str(units: &[Self::Unit], s: &str) -> bool

Whether content code units equal the UTF-8 str s under this encoding.

The default encodes s and compares slices; an encoding can override with an allocation-free lazy comparison (as Wtf16 does).

Source

fn debug_fmt(units: &[Self::Unit], f: &mut Formatter<'_>) -> Result

Write the escaped debug form of units, like OsStr: quoted, with control and non-printable characters escaped.

The default decodes lossily and escapes; an encoding can override to also escape ill-formed sequences losslessly (as Wtf16 does for a lone surrogate), so distinct ill-formed inputs remain distinguishable.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§