pub struct KvStream<'a, E> { /* private fields */ }Expand description
An async stream of key-value pairs.
This is a consuming cons-list: awaiting the stream yields either None
(exhausted) or one KvPair together with the stream carrying the remainder.
Consuming rather than borrowing means a backend can keep a live cursor, a
page buffer, or a transaction read handle inside the future without any of
it leaking into the caller’s borrow graph.
The continuation shape enables lazy backends, but does not force a fetching
strategy. A backend may prefetch pages or materialize the range up front;
take stops polling continuations after its limit, but cannot undo such
backend work. The in-memory backend, for example, materializes its
snapshot up front.
§Cancellation
This stream and its async methods are consuming. Dropping a returned future,
as can happen when it loses a select or timeout race, loses both the
in-flight page and the continuation. To resume, reissue
KvStore::range from the last processed key; the
caller owns deduplication at that seam.
use std::ops::Bound;
use fasm_storage::KvStore;
async fn demo<S: KvStore>(store: &S) -> Result<(), S::Error> {
let mut cursor = store.range(&[], Bound::Unbounded, Bound::Unbounded, false);
while let Some((pair, rest)) = cursor.next().await? {
println!("key has {} bytes", pair.key.len());
cursor = rest;
}
Ok(())
}Implementations§
Source§impl<'a, E> KvStream<'a, E>
impl<'a, E> KvStream<'a, E>
Sourcepub fn new<F>(fut: F) -> Self
pub fn new<F>(fut: F) -> Self
Create a new stream from a future producing the head pair and the tail.
On native targets the future must be Send so scans can cross task
boundaries. Browser futures may retain thread-local Web API handles. It
is boxed in either case, so backends may return async blocks of any
shape.
Sourcepub fn failed(err: E) -> Selfwhere
E: MaybeSend + 'a,
pub fn failed(err: E) -> Selfwhere
E: MaybeSend + 'a,
Create a stream that fails on its first poll.
Useful for backends whose range signature cannot return a Result but
which can fail while setting up the scan (acquiring a snapshot,
mapping bounds); the failure is deferred into the stream instead of
being swallowed.
Sourcepub async fn next(self) -> Result<Option<(KvPair, KvStream<'a, E>)>, E>
pub async fn next(self) -> Result<Option<(KvPair, KvStream<'a, E>)>, E>
Get the next item in the stream.
Returns Ok(Some((pair, next))) if there is another item, or
Ok(None) if the stream is exhausted.
This consumes the stream. Dropping the returned future loses the in-flight page and continuation; resume by reissuing the range from the last processed key and deduplicate at the seam.
Sourcepub async fn collect(self) -> Result<Vec<KvPair>, E>
pub async fn collect(self) -> Result<Vec<KvPair>, E>
Collect all remaining items into a vector.
Warning: this loads the entire remaining range into memory. Prefer
take or for_each for unbounded scans.
This consumes the stream. Dropping the returned future loses the in-flight page and continuation; resume by reissuing the range from the last processed key and deduplicate at the seam.
Sourcepub async fn take(self, limit: usize) -> Result<Vec<KvPair>, E>
pub async fn take(self, limit: usize) -> Result<Vec<KvPair>, E>
Collect up to limit items into a vector.
Stops polling continuations as soon as limit items have been produced.
A backend may already have prefetched or materialized additional items.
This consumes the stream. Dropping the returned future loses the in-flight page and continuation; resume by reissuing the range from the last processed key and deduplicate at the seam.
Sourcepub async fn for_each<F>(self, f: F) -> Result<(), E>
pub async fn for_each<F>(self, f: F) -> Result<(), E>
Apply a function to each item, stopping on the first stream error.
The visitor is infallible and synchronous, so it can neither stop the
walk early nor report a failure of its own. A consumer that needs either
— a parse that can fail, a search that ends at the first match — should
drive next directly rather than reaching for this.
This consumes the stream. Dropping the returned future loses the in-flight page and continuation; resume by reissuing the range from the last processed key and deduplicate at the seam.