Skip to main content

Keys

Trait Keys 

Source
pub trait Keys {
    const KEYS: &'static [&'static str];
    const MAP: &'static KeyMap;
    const REQUIRED: u64 = 0;
}
Expand description

The key schema of a struct, and its compile-time perfect hash.

Shared by every format, because the keys are a property of the type rather than of the encoding. JSON looks a key up out of a quoted run of document bytes and BEVE out of a length-prefixed one, but both land in the same table and yield the same field index.

Required Associated Constants§

Source

const KEYS: &'static [&'static str]

Field keys, in declaration order. Index i here is field i in each format’s read_field and write_fields.

Source

const MAP: &'static KeyMap

The perfect hash over Keys::KEYS, built during const evaluation.

A reference rather than a value, so the table lives in read-only memory and is never copied onto the stack at a call site.

Provided Associated Constants§

Source

const REQUIRED: u64 = 0

Bit i set for each field that a document must supply, in KEYS order.

Where Options::ERROR_ON_MISSING_KEYS is the reader’s answer to “may a member be left out”, this is the type’s, and the two are a union: a field marked here is required under every policy, and RequireKeys requires every field whether or not any is marked. Absence is otherwise no error, so the default is zero and a schema that says nothing about it reads exactly as it did before.

Written by object! from the #[required] markers in a declaration. A hand-written impl may set it directly, and should set no bit past the end of KEYS: such a bit asks for a field that cannot be filled, so no reading under the default policy would ever succeed, while RequireKeys would discard it along with the rest of the mask and accept the same document.

A marked field must be one of the first 64 declared. The mask is a u64, and a field past that has no bit to set. Marking one is a build error naming the limit, reported when the crate is built rather than by cargo check, the mask being a constant of a generic type.

The struct itself may be wider: only the fields that are marked need room here, unlike ERROR_ON_MISSING_KEYS, which needs a bit for every one and so caps the whole struct.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§