Skip to main content

AgentStore

Trait AgentStore 

Source
pub trait AgentStore<A: Agent> {
    // Required methods
    fn get(&self, id: AgentId) -> Option<Ref<'_, A>>;
    fn get_mut(&self, id: AgentId) -> Option<RefMut<'_, A>>;
    fn insert(&mut self, agent: A);
    fn remove(&mut self, id: AgentId) -> Option<A>;
    fn iter_ids(&self) -> Vec<AgentId> ;
    fn len(&self) -> usize;

    // Provided methods
    fn contains(&self, id: AgentId) -> bool { ... }
    fn iter_ids_into(&self, buf: &mut Vec<AgentId>) { ... }
    fn is_empty(&self) -> bool { ... }
}
Expand description

Trait for agent containers.

Provides CRUD operations and ID iteration. All access methods take &self (not &mut self) for reads, using RefCell internally to support concurrent borrows of different agents.

Required Methods§

Source

fn get(&self, id: AgentId) -> Option<Ref<'_, A>>

Borrow an agent immutably by ID.

Source

fn get_mut(&self, id: AgentId) -> Option<RefMut<'_, A>>

Borrow an agent mutably by ID.

§Panics

Panics at runtime if the agent is already borrowed (see RefCell rules).

Source

fn insert(&mut self, agent: A)

Insert an agent into the store.

If an agent with the same ID already exists, it is silently replaced.

Source

fn remove(&mut self, id: AgentId) -> Option<A>

Remove and return an agent by ID, or None if not found.

Source

fn iter_ids(&self) -> Vec<AgentId>

Collect all agent IDs into a new Vec.

Prefer iter_ids_into when a reusable buffer is available.

Source

fn len(&self) -> usize

Number of agents in the store.

Provided Methods§

Source

fn contains(&self, id: AgentId) -> bool

Check whether an agent with the given ID exists.

Source

fn iter_ids_into(&self, buf: &mut Vec<AgentId>)

Append all agent IDs into buf without clearing it first.

This avoids allocation when buf is reused across calls.

Source

fn is_empty(&self) -> bool

Returns true if the store contains no agents.

Implementors§