Derive Macro Schema

Source
#[derive(Schema)]
{
    // Attributes available to this derive:
    #[postcard]
}
Expand description

Derive Schema for a struct or enum

§Examples

use postcard_schema::Schema;

#[derive(Schema)]
struct Point {
    x: i32,
    y: i32,
}

§Attributes

§#[serde(rename = "name")]

The names of fields, containers, and variants in derived schemas respect #[serde(rename = "name")] attributes. Note that this does not include variants like #[serde(rename(serialize = "ser_name"))] or #[serde(rename_all = "...")].

§#[postcard(crate = ...)]

The #[postcard(crate = ...)] attribute can be used to specify a path to the postcard_schema crate instance to use when referring to Schema and schema types from generated code. This is normally only applicable when invoking re-exported derives from a different crate.

use postcard_schema as reexported_postcard_schema;

#[derive(Schema)]
#[postcard(crate = reexported_postcard_schema)]
struct Point {
    x: i32,
    y: i32,
}

§#[postcard(bound = ...)]

The #[postcard(bound = ...)] attribute can be used to overwrite the default bounds when deriving Schema. The default bounds are T: Schema for each type parameter T.

#[derive(Schema)]
#[postcard(bound = "")]
struct Foo<F: Bar, T: Schema>(F::Wrap<T>);

trait Bar {
    type Wrap<T: Schema>: Schema;
}

struct NoSchema;
impl Bar for NoSchema {
    type Wrap<T: Schema> = Option<T>;
}

Foo::<NoSchema, u8>::SCHEMA;

Derive the postcard_schema::Schema trait for a struct or enum.