Skip to main content

ObjectRegistry

Trait ObjectRegistry 

Source
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§

Source

fn register(&mut self, object: T) -> ObjectHandle

Register a new object and return its handle

This allocates a new ID for the object and returns a handle that can be used to reference it later.

§Arguments
  • object - The object to register
§Returns

A handle that can be used to access the object

Source

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 successful
  • Err(ObjectError) - If the handle is invalid or not found
Source

fn get(&self, handle: ObjectHandle) -> Result<&T, ObjectError>

Get a reference to an object by its handle

§Arguments
  • handle - The handle of the object to retrieve
§Returns
  • Ok(&T) - A reference to the object if found and valid
  • Err(ObjectError) - If the handle is invalid or not found
Source

fn get_mut(&mut self, handle: ObjectHandle) -> Result<&mut T, ObjectError>

Get a mutable reference to an object by its handle

§Arguments
  • handle - The handle of the object to retrieve
§Returns
  • Ok(&mut T) - A mutable reference to the object if found and valid
  • Err(ObjectError) - If the handle is invalid or not found
Source

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 found
  • None - If no object with this ID exists
Source

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

Source

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
Source

fn current_generation(&self, id: u64) -> u32

Get the current generation for an object ID

§Arguments
  • id - The object ID
§Returns

The current generation counter for this ID

Source

fn all_ids(&self) -> Vec<u64>

Get all registered object IDs

§Returns

A vector of all currently registered object IDs

Source

fn len(&self) -> usize

Get the number of registered objects

§Returns

The count of objects currently in the registry

Source

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 handle
  • Ok(None) - The object has no parent (e.g., Window is top-level)
  • Err(ObjectError) - If the handle is invalid or not found
Source

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

Provided Methods§

Source

fn is_valid(&self, handle: ObjectHandle) -> bool

Check if a handle is still valid

§Arguments
  • handle - The handle to validate
§Returns

true if the handle references a valid object with the correct generation

Source

fn is_empty(&self) -> bool

Check if the registry is empty

§Returns

true if no objects are registered

Implementors§