Skip to main content

aranya_runtime/client/
buffers.rs

1//! Caller-supplied buffers for graph operations.
2//!
3//! Mirrors the [`TraversalBuffer`] / [`TraversalBuffers`] pattern from
4//! `crate::storage`: the caller owns the buffer, hands `&mut` to an
5//! operation, and the buffer's `get()` accessors clear on entry so the
6//! caller never touches reset logic.
7
8use crate::{
9    Segment,
10    client::{braiding::strand_heap::StrandHeap, convergence_map::ConvergenceStorage},
11    storage::TraversalBuffers,
12};
13
14/// Reusable storage for one braid call.
15///
16/// Generic over the [`Segment`] type because the strand-heap cache
17/// stores segments inline.
18pub(crate) struct BraidBuffer<S> {
19    pub strands: StrandHeap<S>,
20    pub convergence: ConvergenceStorage,
21}
22
23impl<S: Segment> BraidBuffer<S> {
24    pub(crate) const fn new() -> Self {
25        Self {
26            strands: StrandHeap::new(),
27            convergence: ConvergenceStorage::new(),
28        }
29    }
30}
31
32impl<S: Segment> Default for BraidBuffer<S> {
33    fn default() -> Self {
34        Self::new()
35    }
36}
37
38/// Bundle of buffers used by graph-mutating operations such as
39/// [`add_commands`](crate::ClientState::add_commands).
40///
41/// `traversal` is plural ([`TraversalBuffers`]) so that callers which
42/// also call sync code (`SyncRequester`, `SyncResponder`) can use
43/// `traversal` directly without keeping a separate `TraversalBuffers`
44/// field alongside this struct.
45///
46/// Internal graph-mutation helpers that require a singular
47/// [`TraversalBuffer`](crate::storage::TraversalBuffer) receive
48/// `&mut buffers.traversal.primary`.
49///
50/// Construct once per long-lived component and reuse across calls.
51pub struct RuntimeBuffers<S> {
52    pub traversal: TraversalBuffers,
53    pub(crate) braid: BraidBuffer<S>,
54}
55
56impl<S: Segment> RuntimeBuffers<S> {
57    pub const fn new() -> Self {
58        Self {
59            traversal: TraversalBuffers::new(),
60            braid: BraidBuffer::new(),
61        }
62    }
63}
64
65impl<S: Segment> Default for RuntimeBuffers<S> {
66    fn default() -> Self {
67        Self::new()
68    }
69}