Struct ObjectStore

Source
pub struct ObjectStore<T, TID>
where TID: Eq + Hash,
{ /* 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 then register 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>
where T: ObjectStoreContent + ObjectStoreContent<IdType = TID>, TID: Eq + Hash + Clone,

Source

pub fn new() -> Self

Create a new ObjectStore

Source

pub fn with_capacity(capacity: usize) -> Self

Create a new ObjectStore with initial capacity

Source

pub fn reserve_id(&mut self) -> TID

Reserve an ID in the ObjectStore. Generally followed with a call to register using the ID.

Source

pub fn register(&mut self, object: T) -> Result<TID, IdError<TID>>

Registers an object into the ObjectStore

Source

pub fn register_named<STR>( &mut self, name: STR, object: T, ) -> Result<TID, IdError<TID>>
where STR: Into<Cow<'static, str>>,

Registers a named object into the ObjectStore

Source

pub fn insert_new<CB>(&mut self, cb: CB) -> Result<TID, IdError<TID>>
where CB: FnOnce(TID) -> Result<T, IdError<TID>>,

Reserves an ID and registers the object in a single call. The object created must use the ID given to the closure.

Source

pub fn insert_new_named<CB, STR>( &mut self, name: STR, cb: CB, ) -> Result<TID, IdError<TID>>
where CB: FnOnce(TID) -> Result<T, IdError<TID>>, STR: Into<Cow<'static, str>>,

Reserves an ID and registers the named object in a single call. The object created must use the ID given to the closure.

Source

pub fn id_from_name(&self, name: &str) -> Option<&TID>

Get the Object ID from the name

Source

pub fn name_from_id(&self, id: &TID) -> Option<&str>

Get the name from the Object ID

Source

pub fn get_by_name(&self, name: &str) -> Option<&T>

Get an object by its name

Source

pub fn get(&self, id: &TID) -> Option<&T>

Get an object by its ID

Source

pub fn get_mut(&mut self, id: &TID) -> Option<&mut T>

Get a mutable reference to the object

Source

pub fn iter_names(&self) -> impl Iterator<Item = (&Cow<'static, str>, &TID)>

Trait Implementations§

Source§

impl<T: Debug, TID> Debug for ObjectStore<T, TID>
where TID: Eq + Hash + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, TID> !Freeze for ObjectStore<T, TID>

§

impl<T, TID> RefUnwindSafe for ObjectStore<T, TID>

§

impl<T, TID> Send for ObjectStore<T, TID>
where TID: Send, T: Send,

§

impl<T, TID> Sync for ObjectStore<T, TID>
where TID: Sync, T: Sync,

§

impl<T, TID> Unpin for ObjectStore<T, TID>
where TID: Unpin, T: Unpin,

§

impl<T, TID> UnwindSafe for ObjectStore<T, TID>
where TID: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.