pub struct ObjectStore<T, TID>{ /* private fields */ }
Expand description
A store for objects that are weak referenced by an ID and optional name.
There are two different ways to insert an object.
- Use
insert_new
which takes a closure that receives the ID for the new object - Get an ID with
reserve_id
and thenregister
the object with that ID
To add objects with an associated name, use the corresponding
insert_new_named
and register_named
instead.
§Examples
// create an ObjectStore with a test object
let mut store = ObjectStore::new();
let object_id = store.insert_new_named("test object", |id| Ok(Object { id })).unwrap();
// get the object either by ID or name
let object = store.get(&object_id).unwrap();
let object = store.get_by_name("test object").unwrap();
Implementations§
Source§impl<'s, T, TID> ObjectStore<T, TID>
impl<'s, T, TID> ObjectStore<T, TID>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new ObjectStore with initial capacity
Sourcepub fn reserve_id(&mut self) -> TID
pub fn reserve_id(&mut self) -> TID
Reserve an ID in the ObjectStore. Generally followed with a call to register
using the ID.
Sourcepub fn register(&mut self, object: T) -> Result<TID, IdError<TID>>
pub fn register(&mut self, object: T) -> Result<TID, IdError<TID>>
Registers an object into the ObjectStore
Sourcepub fn register_named<STR>(
&mut self,
name: STR,
object: T,
) -> Result<TID, IdError<TID>>
pub fn register_named<STR>( &mut self, name: STR, object: T, ) -> Result<TID, IdError<TID>>
Registers a named object into the ObjectStore
Sourcepub fn insert_new<CB>(&mut self, cb: CB) -> Result<TID, IdError<TID>>
pub fn insert_new<CB>(&mut self, cb: CB) -> Result<TID, IdError<TID>>
Reserves an ID and registers the object in a single call. The object created must use the ID given to the closure.
Sourcepub fn insert_new_named<CB, STR>(
&mut self,
name: STR,
cb: CB,
) -> Result<TID, IdError<TID>>
pub fn insert_new_named<CB, STR>( &mut self, name: STR, cb: CB, ) -> Result<TID, IdError<TID>>
Reserves an ID and registers the named object in a single call. The object created must use the ID given to the closure.
Sourcepub fn id_from_name(&self, name: &str) -> Option<&TID>
pub fn id_from_name(&self, name: &str) -> Option<&TID>
Get the Object ID from the name
Sourcepub fn name_from_id(&self, id: &TID) -> Option<&str>
pub fn name_from_id(&self, id: &TID) -> Option<&str>
Get the name from the Object ID
Sourcepub fn get_by_name(&self, name: &str) -> Option<&T>
pub fn get_by_name(&self, name: &str) -> Option<&T>
Get an object by its name