Attribute Macro state

#[state]
Expand description

Part of the kanzi::class procedural macro suite.

Defines functions that operate on the internal state of the class.

The class state is internally wrapped in a RefCell. To call these functions, a borrow of the state must be performed first. RefCell is used to enforce Rust’s borrowing rules in a C++ interoperability context, where aliasing is common and cannot be checked statically.

For example:

#[kanzi::class]
struct RustDataSource {
    root: kanzi::DataObject,
}
#[kanzi::state]
impl RustDataSource {
    fn new(domain: &kanzi::Domain, this: kanzi::DataSource) -> kanzi::Result<Self> {
        let root = {
            let root = kanzi::DataObject::create_default(domain, "CustomRoot")?;
            let child_1 = kanzi::DataObject::<()>::create_default(&domain, "Child1")?;
            let child_2 = kanzi::DataObject::<()>::create_default(&domain, "Child2")?;
            child_1.add_child(&child_2)?;
            root.add_child(&child_1)?;
            root
        };
        Ok(Self { root, this })
    }
}