Trait IDataObjectList

pub trait IDataObjectList: AsAny {
    // Required methods
    fn item_count(&self) -> Result<usize, Error>;
    fn acquire_item(&self, index: usize) -> Result<Weak<DataObject<()>>, Error>;
    fn release_item(&self, index: usize) -> Result<(), Error>;
    fn get_item_template(&self) -> Result<DataObject<()>, Error>;
}
Expand description

Example of a static DataObjectList which only supports insertions. Note that DataObjectList is expected to be used for more complex logic involving RPC, databases or lazy initialization:

use kanzi::DataObjectList;

#[kanzi::class]
#[metaclass(base = kanzi::DataObjectList)]
#[derive(Default)]
pub struct DataObjectStaticList {
    items: Vec<kanzi::DataObject<i32>>,
}

#[kanzi::state]
impl DataObjectStaticList {
    fn new(_domain: &kanzi::Domain, _name: &kanzi::KanziStr) -> kanzi::Result<Self> {
        Ok(Self::default())
    }
}

#[kanzi::methods]
impl DataObjectStaticList {
    pub fn add_item(&self, item: kanzi::DataObject<i32>) {
        self.state_mut().items.push(item)
    }
}

#[kanzi::overrides]
impl kanzi::IDataObjectList for DataObjectStaticList {
    fn item_count(&self) -> kanzi::Result<usize> {
        Ok(self.state_ref().items.len())
    }

    fn acquire_item(&self, index: usize) -> kanzi::Result<kanzi::Weak<kanzi::AbstractDataObject>> {
        let item = &self.state_ref().items[index];
        Ok(kanzi::Weak::downgrade_ref(item.as_abstract()))
    }

    fn release_item(&self, _index: usize) -> kanzi::Result<()> {
        unimplemented!()
    }

    fn get_item_template(&self) -> kanzi::Result<kanzi::AbstractDataObject> {
        unimplemented!()
    }
}

DataObjectStaticList::register(&domain)?;

let list = DataObjectStaticList::create(&domain, "StaticList")?;
assert_eq!(list.item_count()?, 0);
list.add_item(DataObject::<i32>::create(&domain, "Item", 1)?);
list.add_item(DataObject::<i32>::create(&domain, "Item", 2)?);
list.add_item(DataObject::<i32>::create(&domain, "Item", 3)?);
assert_eq!(list.item_count()?, 3);

assert_eq!(
    list.to_string(),
     "DataObject (StaticList): list = [ \
        Weak { object: DataObject (Item): integer = '1' }, \
        Weak { object: DataObject (Item): integer = '2' }, \
        Weak { object: DataObject (Item): integer = '3' } \
     ]"
);

Required Methods§

fn item_count(&self) -> Result<usize, Error>

Returns the count of items on the list.

fn acquire_item(&self, index: usize) -> Result<Weak<DataObject<()>>, Error>

Acquires an item on the list by index.

fn release_item(&self, index: usize) -> Result<(), Error>

Releases an item on the list by index.

fn get_item_template(&self) -> Result<DataObject<()>, Error>

Returns tree structure of the data source for individual list item. Only hierarchy and object types are valid, the data might not be.

Implementors§