pub trait BufferReader<T: Clone + Send>: Send {
// Required methods
fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<Result<T, DbError>>;
fn try_recv(&mut self) -> Result<T, DbError>;
}Expand description
Reader trait for consuming values from a buffer
This is the object-safe service-provider interface that runtime adapters
implement. It is poll-based — and therefore object-safe and zero-allocation —
rather than async: an async fn on an erased trait forces a
Pin<Box<dyn Future>> heap allocation on every call.
Consumers do not call this directly; they use the Reader<T>
handle returned by Consumer::subscribe, whose recv() is async and wraps
poll_recv via core::future::poll_fn with no
allocation.
Each reader is independent with its own state.
§Error Handling
Ok(value)- Successfully received a valueErr(BufferLagged)- Missed messages (SPMC ring only, can continue)Err(BufferClosed)- Buffer closed (graceful shutdown)
§Contract (poll/try_recv interleaving and cancellation)
Every implementation in this workspace (tokio/embassy/wasm adapters) conforms — this section makes the contract explicit for future implementors.
try_recvMAY be called betweenPendingpoll_recvcalls; implementations MUST NOT lose values or wakeups across the interleaving.- A value claimed internally just before caller cancellation MUST be
delivered on the next
poll_recv/try_recvcall — this is why, e.g., the tokio broadcast/watch readers keep a persistentReusableBoxFutureacross calls rather than dropping and recreating it per call. poll_recvMUST register the latest waker on everyPendingreturn; spurious wakes are permitted (a registered waker firing with no corresponding new value is not a bug — callers must re-poll and tolerate a no-op).- Lag MUST surface as
Err(DbError::BufferLagged)plus a metricsadd_droppedcall on bothpoll_recvandtry_recv.
Required Methods§
Sourcefn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<Result<T, DbError>>
fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<Result<T, DbError>>
Poll for the next value.
Returns Poll::Ready(Ok(value)) when a value is available,
Poll::Ready(Err(..)) on lag/closure, or Poll::Pending after
registering cx.waker() to be woken when the next value arrives.
§Behavior by Buffer Type
- SPMC Ring: Returns next value, or
Lagged(n)if fell behind - SingleLatest: Waits for value change, returns most recent
- Mailbox: Waits for slot value, takes and clears it
Sourcefn try_recv(&mut self) -> Result<T, DbError>
fn try_recv(&mut self) -> Result<T, DbError>
Non-blocking receive — returns immediately.
Returns Err(DbError::BufferEmpty) if no pending values.
§Behavior by Buffer Type
- SPMC Ring: Returns next buffered value, or
BufferEmptyif caught up - SingleLatest: Returns value if changed since last read, or
BufferEmpty - Mailbox: Takes and returns slot value, or
BufferEmptyif empty
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".