async-rs / async-std

Async version of the Rust standard library

Home Page:https://async.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: Async version of the std::iter traits.

ckaran opened this issue · comments

Summary: Please include async versions of all traits in std::iter.

I'm working on yet another key-value store, this time with the twist that not only is it async, but portions of the data structure can be transparently written to storage (not swap space, it's actually serialized out on a separate thread and fully persisted). Because of how it works, implementing an iterator with async versions of the std::iter would make a lot of sense; if a task is blocked waiting for data to be loaded from memory, a different task that can move forwards can do its own thing. I know of the async-iterator crate, but it is extremely new and isn't yet as fleshed out as much of async-std tends to be. Is it possible for you to add support for async iteration?

The Stream trait acts as an asynchronous form of iteration.

@notgull I think I wasn't being clear enough in my original request, because you're right, the Stream trait, and more importantly, the StreamExt trait is the async analog of Iterator. The issue is that as far as I can tell there is no equivalent to the other traits in std::iter. In particular, are there async equivalents of the following traits?

The reason for my original request is because the data structure that I'm putting together looks a lot like a std::collections::BTreeMap; it's just that parts of the tree might not be in memory currently, so iterating across the tree may require loading those portions into memory (and possibly spilling other portions of the tree). If you look at the traits implemented by std::collections::btree_map::Iter, you can see which traits are implemented; I want analogs of those traits in async-std.

You can use the stream::from_iter combinator to convert a regular iterator into a stream. You can pass the result of calling rev() on the iterator to this function to asynchronously iterate backwards.

You should probably raise this issue with the futures crate, since they define the Stream and FusedStream types.

@notgull I will raise the issue with the futures crate, thank you.