Attribute Macro cglue::custom_impl

source ·
#[custom_impl]
Expand description

Add custom wrapping for a trait impl and the C interface.

This is a pretty complex, and fine-grained operation. It allows to control almost every aspect of type wrapping. The logic of argument layout is a declaration and sequence of actions from top to bottom.

In addition, this is the only way to allow using generic parameters within functions - C implementation must have them converted to concrete types beforehand.

Example from cglue-gen.

#[custom_impl(
    // Types within the C interface other than self and additional wrappers.
    {
        f_out: &mut WriteMut,
    },
    // Unwrapped return type
    Result<(), ::core::fmt::Error>,
    // Conversion in trait impl to C arguments (signature names are expected).
    {
        let f_out: WriteBaseMut<::core::fmt::Formatter> = From::from(f);
        let f_out = &mut #crate_path::trait_group::Opaquable::into_opaque(f_out);
    },
    // This is the body of C impl minus the automatic wrapping.
    {
        write!(f_out, #fmt_str, this)
    },
    // This part is processed in the trait impl after the call returns (impl_func_ret,
    // nothing extra needs to happen here).
    {
    },
)]