Skip to main content

gwr_components/store/
byte_store.rs

1// Copyright (c) 2026 Graphcore Ltd. All rights reserved.
2
3use std::rc::Rc;
4
5use gwr_engine::engine::Engine;
6use gwr_engine::time::clock::Clock;
7use gwr_engine::traits::SimObject;
8use gwr_engine::types::SimError;
9use gwr_track::entity::Entity;
10use gwr_track::tracker::aka::Aka;
11
12use super::Store;
13
14/// Builds stores that support a configurable number of bytes.
15///
16/// Objects must support the [SimObject] trait. The returned [Store] uses
17/// [`total_bytes`](gwr_engine::traits::TotalBytes::total_bytes) to decide
18/// whether there is enough free byte capacity to accept each object.
19pub struct ByteStore<T>(std::marker::PhantomData<T>);
20
21impl<T> ByteStore<T>
22where
23    T: SimObject,
24{
25    /// Basic byte-store constructor.
26    ///
27    /// Returns a `SimError` if `capacity_bytes` is 0.
28    pub fn new_and_register_with_renames(
29        engine: &Engine,
30        clock: &Clock,
31        parent: &Rc<Entity>,
32        name: &str,
33        aka: Option<&Aka>,
34        capacity_bytes: usize,
35    ) -> Result<Rc<Store<T>>, SimError> {
36        let entity = Rc::new(Entity::new(parent, name));
37        let store = Rc::new(Store::new(
38            engine,
39            clock,
40            &entity,
41            aka,
42            capacity_bytes,
43            "bytes",
44            |value: &T| value.total_bytes(),
45        )?);
46        engine.register(store.clone());
47        Ok(store)
48    }
49
50    /// Basic byte-store constructor.
51    ///
52    /// Returns a `SimError` if `capacity_bytes` is 0.
53    pub fn new_and_register(
54        engine: &Engine,
55        clock: &Clock,
56        parent: &Rc<Entity>,
57        name: &str,
58        capacity_bytes: usize,
59    ) -> Result<Rc<Store<T>>, SimError> {
60        Self::new_and_register_with_renames(engine, clock, parent, name, None, capacity_bytes)
61    }
62}