author | title | date | theme |
---|---|---|---|
Bhargav Voleti & Matthew Wraith |
Futures in Haskell and Rust |
March 27th, 2019 |
solarized |
Goals of the languages:
- Rust, zero cost abstractions and memory safety
- Haskell, correctness and expressivity
Main differences:
- Rust cares a lot about performance, so the type system tells you where the values live in the machine (stack vs heap)
- Haskell doesn't necessarily care about performance
- Concisely talk about some future values
- More often than not, you're streaming stuff
- futures is an abstraction, tokio is backend to futures
- async uses STM for coordination
- execution model
futures are an abstraction over things that need to be polled, tokio is a backend async is more all inclusive, based on STM
Look at this section: A closer look at futures
executors in rust STM in Haskell
futures-rs uses macros, and async relies on Haskell's STM runtime.
- API comparison
- Reading file pipe to stdout
- Concurrent HTTP client to single file
- Chat server
- Streams
- combinators over futures, streams
- and_then
- concurrency
Async a
instance Functor Async
async :: IO a -> IO (Async a)
wait :: Async a -> IO a
poll :: Async a -> IO (Maybe (Either SomeException a))
-- TODO io-streams interface
Use await!
macro to wait on Future
.
pub enum Poll<T> {
Ready(T),
Pending,
}
pub trait Future {
type Item;
type Error;
fn poll(&mut self) -> Result<Self::Item, Self::Error>;
}
map concurrently:
mapConcurrently :: Traversible t
=> (a -> IO b) -> t a -> IO (t b)
main = mapConcurrently get urls >>= concat >>= print
TODO full example
High level differences