Skip to main content

CustomDataType

Derive Macro CustomDataType 

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

Derives the CustomDataType trait and an impl From<T> for Value conversion for a user-defined enum or struct.

The type must also derive Encode (for binary serialization) and implement Display (for the cached display string in [CustomValue]).

§Required attribute

  • #[type_tag = "..."]: A unique string identifier for this custom data type.

§What the macro generates

Given a type like:

#[derive(Encode, CustomDataType)]
#[type_tag = "status"]
enum Status { Active, Inactive }

The macro expands into:

impl CustomDataType for Status {
    const TYPE_TAG: &'static str = "status";
}

impl From<Status> for Value {
    fn from(val: Status) -> Value {
        Value::Custom(CustomValue {
            type_tag: "status".to_string(),
            encoded: Encode::encode(&val).into_owned(),
            display: val.to_string(),
        })
    }
}

§Note

The user must also provide Display, Default, and DataType implementations for the type. This macro only bridges the custom type to the Value system.