Aldrin macros
The macros in this crate are not generally meant to be used directly, but through re-exports in other crates.
Procedural macros
Derive macros
- [
Tag] - [
PrimaryTag] - [
RefType] - [
Serialize] - [
Deserialize] - [
Introspectable] - [
KeyTag] - [
PrimaryKeyTag] - [
SerializeKey]
All derive macros are re-exported in both aldrin and aldrin-core.
Attributes
All derive macros support various attributes and some apply to multiple macros.
Container attributes
crate
- Applies to: all derive macros
The attribute #[aldrin(crate = ...) can be used to override the path of the aldrin_core
crate. This is useful when aldrin_core is not a direct dependency, but only reexported
somewhere. The default value depends on from where the macro is invoked, it's either
::aldrin::core or ::aldrin_core.
schema
- Applies to: [
Introspectable]
Deriving Introspectable requires specifying a schema name. It is an error if this attribute is
missing.
# use Introspectable;
ref_type
- Applies to: [
RefType], [Serialize] and [SerializeKey]
Deriving RefType requires specifying an identifier for the type with the
#[aldrin(ref_type = ...)] attribute. The Serialize and SerializeKey derive macro will then
also generate implementations for that type.
It is also possible to specifiy just #[aldrin(ref_type)] without an identifer. In this case, a
default will be chosen by appending Ref to the name of the annotated type.
newtype
- Applies to: [
Serialize], [Deserialize], [KeyTag], [PrimaryKeyTag], [SerializeKey], and [DeserializeKey].
Specifying #[aldrin(newtype)] is possible for structs with exactly 1 field and makes them
behave like that field. E.g., the type will no longer serialize as a struct, but directly as
that field instead.
# use ;
doc
- Applies to: [
Introspectable]
Provides an alternative doc string used only for deriving [Introspectable]. Note that this
attribute can be used anywhere a regular doc comment can be used as well.
If this is not provided then [Introspectable] will fall back to the regular doc comment (if
present).
Using this attribute can be desirable if the regular doc comment is Rust-specific, e.g. due to link conversion or other modifications.
# use ;
/// This doc comment will be rendered by rustdoc.
Field and variant attributes
id
- Applies to: [
Serialize], [Deserialize] and [Introspectable]
Use #[aldrin(id = ...)] to override the automatically defined id for a field or variant.
Default ids start at 0 for the first field or variant and then increment by 1 for each subsequent field or variant.
# use ;
# use ;
optional
- Applies to: [
Serialize], [Deserialize] and [Introspectable]
Use #[aldrin(optional)] to mark fields of a struct as optional. They must be of an Option<T>
type.
Optional fields are not serialized if None and are allowed to be missing when deserializing a
value.
# use ;
Both fields required_field_1 and required_field_2 will always be serialized and
deserialization will fail if either is missing. Serialization of optional_field is skipped if
it is None. If it's missing during deserialization, then it will be set to None.
fallback
- Applies to: [
Serialize], [Deserialize] and [Introspectable]
The last field of a struct and the last variant of an enum can optionally be marked with
#[aldrin(fallback)]. This will enable successful serialization and deserialization of unknown
fields and variants. For structs, the field type must be aldrin_core::UnknownFields. For
enums, the variant must have a single field of type aldrin_core::UnknownVariant.
This attribute cannot be combined with #[aldrin(optional)].
Example of a struct with a fallback field:
# use ;
Example of an enum with a fallback variant:
# use ;
doc
- Applies to: [
Introspectable]
See doc for containers.