Skip to main content

export_handle

Macro export_handle 

Source
macro_rules! export_handle {
    (
        $(#[$handle_meta:meta])*
        handle $handle:ident { $field:ident: $inner:ty },

        $(#[$free_meta:meta])*
        free $free_fn:ident $(,)?
    ) => { ... };
}
Expand description

Declare an opaque document handle and the function that frees it.

The handle is what a C caller holds: a #[repr(C)] newtype around the library’s own document type with one private field. The field name is given rather than invented, because every entry point projects through it ((*doc).inner) and that has to be readable in the consuming source.

#[repr(C)] is emitted because the three shipped libraries declare it. It says nothing useful about a Rust field, and it is kept only so that adopting this macro leaves the declared type identical.

unparser_shared::export_handle! {
    /// Opaque handle to a parsed document.
    handle DemoDoc { inner: Document },

    /// Free a document handle.
    ///
    /// # Safety
    ///
    /// - `doc` must be a pointer a parse function returned, or null.
    /// - After this call the handle is invalid and must not be used.
    free demo_release_document,
}