Skip to main content

schema

Macro schema 

Source
macro_rules! schema {
    ($Schema: ident {
    $(
      $Field: ident:
        $(<$($generic_name: tt: $generic_type: tt),+>)?(
          $($arg: ident: $arg_type: ty),*
        ) -> $value: ty$(,)?
    )*
  }) => { ... };
}
Available on crate feature schema only.
Expand description

Create a strongly-typed database schema.

This will define a zero-sized type (ZST) with a statically-defined key structure, helping to effect namespacing for distinct values. The key is not guaranteed to be unique across macro invocations however, it being dependent on the name of the schema and the name of the type. It is guaranteed to be unique within a macro invocation, or across schema definitions with distinct names. Third-party modification of these values within the database has undefined behavior.

The keys and values are converted to bytes using borsh. All borsh serializations are assumed infallible if the underlying writer is, such as when the writer is a [Vec], and this library assumes any serialized value may be successfully deserialized. Violations of these conditions MAY incur undefined behavior.

While the macro supports generic parameters, reading a value written with distinct generic parameters is undefined.

§Arguments

  • Schema: The name of the schema being defined. This is intended to provide domain-separation with other defined schemas and is limited to being 255 bytes long at most.
  • Field: A field within the schema. This will be the name of the generated ZST which is usable to read and write to this field and is limited to being 255 bytes long at most.
  • <_>: Generic type arguments for the arguments and return value. These are not explicitly incorporated into the key and two keys with distinct generic arguments but the same serialization will be considered the same.
  • (_): Runtime arguments to index the key-space with.
  • -> _: The type of the value stored under this key.

§Example

serai_db::schema!(
  MySchema {
    Counter: () -> u64,
    Password: (user: String) -> String,
    UserData: <PersonalData: BorshSerialize + BorshDeserialize>(user: String) -> PersonalData,
  }
);