#[derive(Type)]
{
// Attributes available to this derive:
#[zlink]
}
Expand description
Derives Type for structs and enums, generating appropriate Type::Object or Type::Enum
representation.
Requires the introspection feature to be enabled.
§Structs
For structs, this macro supports named fields and unit structs. It will generate a
Type implementation that creates a Type::Object containing all the fields with their
names and types. Tuple structs are not supported as Varlink does not support unnamed fields.
§Enums
For enums, this macro only supports unit variants (variants without associated data). It will
generate a Type implementation that creates a Type::Enum containing all the variant
names.
§Supported Attributes
The following attributes can be used to customize the behavior of this derive macro:
#[zlink(crate = "path")]- Specifies the crate path to use for zlink types. Defaults to::zlink.
§Limitations
The following types are not supported by this macro:
- Tuple structs: Varlink does not support unnamed fields
- Enums with data: Only unit enums (variants without associated data) are supported
- Unions: Not supported by Varlink
ⓘ
#[derive(Type)] // This will fail to compile
struct Point(f32, f32, f32);ⓘ
#[derive(Type)] // This will fail to compile
enum Status {
Active(String), // Variants with data are not supported
Inactive,
}§Examples
§Named Structs
use zlink::introspect::Type;
use zlink::idl;
#[derive(Type)]
struct Person {
name: String,
age: i32,
active: bool,
}
// Access the generated type information
match Person::TYPE {
idl::Type::Object(fields) => {
let field_vec: Vec<_> = fields.iter().collect();
assert_eq!(field_vec.len(), 3);
assert_eq!(field_vec[0].name(), "name");
assert_eq!(field_vec[0].ty(), &idl::Type::String);
assert_eq!(field_vec[1].name(), "age");
assert_eq!(field_vec[1].ty(), &idl::Type::Int);
assert_eq!(field_vec[2].name(), "active");
assert_eq!(field_vec[2].ty(), &idl::Type::Bool);
}
_ => panic!("Expected struct type"),
}§Unit Structs
#[derive(Type)]
struct Unit;
// Unit structs generate empty field lists
match Unit::TYPE {
idl::Type::Object(fields) => {
assert_eq!(fields.len(), 0);
}
_ => panic!("Expected struct type"),
}§Complex Types
#[derive(Type)]
struct Complex {
id: u64,
description: Option<String>,
tags: Vec<String>,
}
// The macro handles nested types like Option<T> and Vec<T>
match Complex::TYPE {
idl::Type::Object(fields) => {
let field_vec: Vec<_> = fields.iter().collect();
// Optional field becomes Type::Optional
match field_vec[1].ty() {
idl::Type::Optional(inner) => assert_eq!(inner.inner(), &idl::Type::String),
_ => panic!("Expected optional type"),
}
// Vec field becomes Type::Array
match field_vec[2].ty() {
idl::Type::Array(inner) => assert_eq!(inner.inner(), &idl::Type::String),
_ => panic!("Expected array type"),
}
}
_ => panic!("Expected struct type"),
}§Unit Enums
#[derive(Type)]
enum Status {
Active,
Inactive,
Pending,
}
// Unit enums generate variant lists
match Status::TYPE {
idl::Type::Enum(variants) => {
let variant_vec: Vec<_> = variants.iter().collect();
assert_eq!(variant_vec.len(), 3);
assert_eq!(variant_vec[0].name(), "Active");
assert_eq!(variant_vec[1].name(), "Inactive");
assert_eq!(variant_vec[2].name(), "Pending");
}
_ => panic!("Expected enum type"),
}