#[derive(FromContext)]
{
// Attributes available to this derive:
#[context]
#[location]
#[source]
#[from]
}
Expand description
Derive an implementation of dterror::FromContext for an error type.
For a struct, the macro generates:
- a
{Error}Ctx<'ctx>struct holding every field not marked with#[source],#[from], or#[location], derivingClone,Debug, andPartialEq; - a
pub fn new(...)constructor on{Error}Ctxtaking one argument per context field, in declaration order; - an
impl ::dterror::FromContext for {Error}whosefrom_contextmoves the context fields into the error and assignslocationandsourceto the marked fields, when present.
For an enum, the macro generates a {Error}Ctx<'ctx> enum instead: one same-named
variant per constructible variant of the error (a variant with at least one context field
AND a #[source]/#[from] field), plus one public associated constructor on {Error}Ctx per
Ctx variant, named after the variant in snake_case. Unit variants, marker-only
variants, and source-less variants get no Ctx variant and no constructor.
§Attribute helpers
#[source]or#[from]: the field that stores the source of the error. At most one per struct or variant.#[location]: the field that stores the caller’sstd::panic::Location. At most one per struct or variant.#[context(borrow = TargetType)]: store a borrowed reference (&'ctx TargetType) in Ctx instead of an owned value, deferring allocation tofrom_context. This enables zero-cost context construction when the caller already holds a reference — no eager cloning occurs on the Ok path. SeeFromContext::Ctxin thedterrorcrate for details. At most one#[context(...)]attribute per field, and it cannot be combined with#[source]/#[from]or#[location].#[context(constructor = "name")]: variant-level; renames the generated constructor for that variant toname, a quoted string that parses as an identifier. At most one per variant.
Every other field becomes a field of the generated {Error}Ctx. A struct with no context
fields, or an enum with no constructible variant, fails to compile, as do duplicate or
conflicting markers and duplicate constructor names.