Skip to main content

AsRust

Derive Macro AsRust 

Source
#[derive(AsRust)]
{
    // Attributes available to this derive:
    #[target_type]
    #[nullable]
    #[as_rust_extra_field]
    #[as_rust_ignore]
    #[target_name]
}
Expand description

Derive AsRust<T> for a struct or unit enum.

Generates a non-consuming conversion that returns a freshly-allocated value of the type named in #[target_type(...)]. C-string fields are decoded as UTF-8 and copied; other pointer fields are borrowed via RawBorrow and then converted with their own AsRust impl; remaining fields go through their own AsRust impl directly.

The derived AsRust reads pointer fields under the same layout assumptions as CReprOf / CDrop; deriving all three together keeps them in sync.

§Struct-level attributes

  • #[target_type(Path)]required.
  • #[as_rust_extra_field(name = expr)] — initialise an extra field on the Rust side that has no C counterpart. The attribute can be repeated; self (the C-compatible value) is in scope inside expr, allowing reconstruction from unrelated C-side fields.

§Field-level attributes

  • #[nullable] — map a null pointer to [None] instead of failing.
  • #[target_name(ident)] — name of the matching field on the Rust side when it differs from the C-side name.

A field annotated with #[c_repr_of_convert(...)] (see CReprOf) is skipped by this derive; pair it with #[as_rust_extra_field] if the Rust struct still has a matching field.

§Enums

Enums are supported only if every variant is a unit variant.