typst_macros

Macro cast

source
cast!() { /* proc-macro */ }
Expand description

Implements Reflect, FromValue, and IntoValue for a type.

  • Reflect makes Typst’s runtime aware of the type’s characteristics. It’s important for autocompletion, error messages, etc.
  • FromValue defines how to cast from a value into this type.
  • IntoValue defines how to cast fromthis type into a value.
/// An integer between 0 and 13.
struct CoolInt(u8);

cast! {
    CoolInt,

    // Defines how to turn a `CoolInt` into a value.
    self => self.0.into_value(),

    // Defines "match arms" of types that can be cast into a `CoolInt`.
    // These types needn't be value primitives, they can themselves use
    // `cast!`.
    v: bool => Self(v as u8),
    v: i64 => if matches!(v, 0..=13) {
        Self(v as u8)
    } else {
        bail!("integer is not nice :/")
    },
}