Skip to main content

KvStream

Struct KvStream 

Source
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>

Source

pub fn new<F>(fut: F) -> Self
where F: Future<Output = Result<Option<(KvPair, KvStream<'a, E>)>, E>> + MaybeSend + 'a,

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.

Source

pub fn empty() -> Self
where E: 'a,

Create an empty stream (no items).

Source

pub fn failed(err: E) -> Self
where 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.

Source

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.

Source

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.

Source

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.

Source

pub async fn for_each<F>(self, f: F) -> Result<(), E>
where F: FnMut(KvPair),

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.

Trait Implementations§

Source§

impl<E> Debug for KvStream<'_, E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, E> !RefUnwindSafe for KvStream<'a, E>

§

impl<'a, E> !Sync for KvStream<'a, E>

§

impl<'a, E> !UnwindSafe for KvStream<'a, E>

§

impl<'a, E> Freeze for KvStream<'a, E>
where Pin<Box<dyn Future<Output = Result<Option<(KvPair, KvStream<'a, E>)>, E>> + Send + 'a>>: Freeze,

§

impl<'a, E> Send for KvStream<'a, E>
where Pin<Box<dyn Future<Output = Result<Option<(KvPair, KvStream<'a, E>)>, E>> + Send + 'a>>: Send,

§

impl<'a, E> Unpin for KvStream<'a, E>
where Pin<Box<dyn Future<Output = Result<Option<(KvPair, KvStream<'a, E>)>, E>> + Send + 'a>>: Unpin,

§

impl<'a, E> UnsafeUnpin for KvStream<'a, E>
where Pin<Box<dyn Future<Output = Result<Option<(KvPair, KvStream<'a, E>)>, E>> + Send + 'a>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeSend for T
where T: Send,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.