pub trait ReplayBuffer<O, A>{
// Required methods
fn push(&mut self, experience: Experience<O, A>);
fn sample(
&self,
batch_size: usize,
rng: &mut impl Rng,
) -> Vec<Experience<O, A>>;
fn len(&self) -> usize;
fn capacity(&self) -> Option<usize>;
// Provided methods
fn is_empty(&self) -> bool { ... }
fn is_full(&self) -> bool { ... }
fn ready_for(&self, batch_size: usize) -> bool { ... }
}Expand description
A buffer that stores past experience for agent training.
Used primarily by off-policy algorithms (DQN, SAC, TD3) to break
temporal correlations between training samples. On-policy algorithms
(PPO, A2C) typically collect fixed-length trajectories instead and
don’t need this trait — they can use a plain Vec<Experience<O, A>>.
§Implementing this trait
The most common implementation is a circular buffer with a fixed capacity that overwrites the oldest experience when full. Concrete implementations live in ember-rl, not here.
§Bounds
O: Clone + Send + Sync and A: Clone + Send + Sync are required
because sampling returns owned Experience values (not references),
and buffers may be accessed across threads during async training.
Required Methods§
Sourcefn push(&mut self, experience: Experience<O, A>)
fn push(&mut self, experience: Experience<O, A>)
Add a new experience to the buffer.
If the buffer is at capacity, implementations should overwrite the oldest experience (FIFO eviction).
Sourcefn sample(&self, batch_size: usize, rng: &mut impl Rng) -> Vec<Experience<O, A>>
fn sample(&self, batch_size: usize, rng: &mut impl Rng) -> Vec<Experience<O, A>>
Sample a random batch of batch_size experiences.
Sampling is done with replacement. The caller supplies the RNG so sampling randomness can be seeded and controlled independently.
§Panics
Implementations may panic if batch_size > self.len().
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.