pub trait ObjectRegistry<T> {
Show 14 methods
// Required methods
fn register(&mut self, object: T) -> ObjectHandle;
fn unregister(&mut self, handle: ObjectHandle) -> Result<T, ObjectError>;
fn get(&self, handle: ObjectHandle) -> Result<&T, ObjectError>;
fn get_mut(&mut self, handle: ObjectHandle) -> Result<&mut T, ObjectError>;
fn get_by_id(&self, id: u64) -> Option<&T>;
fn next_id(&mut self) -> u64;
fn increment_generation(&mut self, id: u64);
fn current_generation(&self, id: u64) -> u32;
fn all_ids(&self) -> Vec<u64>;
fn len(&self) -> usize;
fn get_parent(
&self,
handle: &ObjectHandle,
) -> Result<Option<ObjectHandle>, ObjectError>;
fn get_children(
&self,
handle: &ObjectHandle,
) -> Result<Vec<ObjectHandle>, ObjectError>;
// Provided methods
fn is_valid(&self, handle: ObjectHandle) -> bool { ... }
fn is_empty(&self) -> bool { ... }
}Expand description
Trait for managing object lifecycle in a registry
Implementations of this trait provide storage and retrieval of objects by their handles, with support for generation-based invalidation.
§Type Parameters
T- The type of object being managed
§Examples
// Example implementation (not compiled in doctests)
struct MyRegistry {
objects: HashMap<u64, RegistryEntry<MyObject>>,
next_id: u64,
generations: HashMap<u64, u32>,
}
impl ObjectRegistry<MyObject> for MyRegistry {
fn register(&mut self, object: MyObject) -> ObjectHandle {
let id = self.next_id();
let generation = self.generations.get(&id).copied().unwrap_or(0);
let handle = ObjectHandle::new(ObjectType::Window, id, generation);
self.objects.insert(id, RegistryEntry::new(handle, object));
handle
}
// ... other methods
}Required Methods§
Sourcefn register(&mut self, object: T) -> ObjectHandle
fn register(&mut self, object: T) -> ObjectHandle
Sourcefn unregister(&mut self, handle: ObjectHandle) -> Result<T, ObjectError>
fn unregister(&mut self, handle: ObjectHandle) -> Result<T, ObjectError>
Unregister an object by its handle
This removes the object from the registry and increments the generation counter for its ID, invalidating any existing handles.
§Arguments
handle- The handle of the object to unregister
§Returns
Ok(object)- The removed object if successfulErr(ObjectError)- If the handle is invalid or not found
Sourcefn get(&self, handle: ObjectHandle) -> Result<&T, ObjectError>
fn get(&self, handle: ObjectHandle) -> Result<&T, ObjectError>
Sourcefn get_mut(&mut self, handle: ObjectHandle) -> Result<&mut T, ObjectError>
fn get_mut(&mut self, handle: ObjectHandle) -> Result<&mut T, ObjectError>
Sourcefn get_by_id(&self, id: u64) -> Option<&T>
fn get_by_id(&self, id: u64) -> Option<&T>
Get an object by its ID, regardless of generation
This is useful for debugging or administrative operations where you need to access an object even if the handle might be stale.
§Arguments
id- The object ID
§Returns
Some(&T)- A reference to the object if foundNone- If no object with this ID exists
Sourcefn next_id(&mut self) -> u64
fn next_id(&mut self) -> u64
Get the next available object ID
This should return a unique ID that hasn’t been used yet, or has been recycled after unregistration.
§Returns
A unique object ID
Sourcefn increment_generation(&mut self, id: u64)
fn increment_generation(&mut self, id: u64)
Increment the generation counter for an object ID
This is called when an object is unregistered to invalidate existing handles. The next object to use this ID will have a higher generation.
§Arguments
id- The object ID whose generation should be incremented
Sourcefn current_generation(&self, id: u64) -> u32
fn current_generation(&self, id: u64) -> u32
Sourcefn get_parent(
&self,
handle: &ObjectHandle,
) -> Result<Option<ObjectHandle>, ObjectError>
fn get_parent( &self, handle: &ObjectHandle, ) -> Result<Option<ObjectHandle>, ObjectError>
Get the parent handle of an object
This enables navigation up the object hierarchy (e.g., Pane -> Tab -> Window).
§Arguments
handle- The handle of the object whose parent to retrieve
§Returns
Ok(Some(handle))- The parent object’s handleOk(None)- The object has no parent (e.g., Window is top-level)Err(ObjectError)- If the handle is invalid or not found
Sourcefn get_children(
&self,
handle: &ObjectHandle,
) -> Result<Vec<ObjectHandle>, ObjectError>
fn get_children( &self, handle: &ObjectHandle, ) -> Result<Vec<ObjectHandle>, ObjectError>
Get the child handles of an object
This enables navigation down the object hierarchy (e.g., Window -> Tabs -> Panes).
§Arguments
handle- The handle of the object whose children to retrieve
§Returns
Ok(Vec<ObjectHandle>)- A vector of child object handles (may be empty)Err(ObjectError)- If the handle is invalid or not found