- Async IO
- Library support for network services
Help us decide what to prioritize the rest of this afternoon!
- HTTP GET to URL 1
- HTTP GET to URL 2
- Combine the bodies of the two responses
(Ignore that this is embarrassingly parallel.)
impl Client {
fn get(&mut self, url: &Url)
-> io::Result<String> { ... }
}
fn concat_pages(client: &mut Client, url1: &Url, url2: &Url)
-> io::Result<String>
{
let page1 = client.get(url1)?;
let page2 = client.get(url2)?;
Ok(page1 + &page2)
}
impl Client {
fn get(&mut self, url: &Url)
-> impl Future<Item = String, Error = io::Error>
{ ... }
}
fn concat_pages(client: Arc<Mutex<Client>>,
url1: &Url, url2: &Url)
-> io::Result<String>
{
let client2 = client.clone();
let url2 = url2.to_owned();
client.lock().get(url1)
.and_then(move |page1| {
client2.lock().get(&url2).join(Ok(page1))
})
.map(|(page2, page1)| {
page2 + &page1
})
}
impl Client {
fn get(&mut self, url: &Url)
-> impl Future<Item = String, Error = io::Error>
{ ... }
}
(The same API!)
async fn concat_pages(client: &mut Client,
url1: &Url, url2: &Url)
-> io::Result<String>
{
let page1 = await(client.get(url1))?;
let page2 = await(client.get(url2))?;
Ok(page1 + &page2)
}
async fn foo() -> T
is a function that returnsimpl Future<Item = T>
- Inside the body, you
return T
; your function body compiles to a stackless coroutine
- Transforms a
Future<Item = T>
intoT
- Can only
await
inside of anasync fn
- Yields control of the coroutine until the future resolves
- Move
Future
,IntoFuture
, andtask
into libcore - Leave everything else in the ecosystem for now
trait Future {
type Item;
fn poll(self: Pin<Self>,
ctx: &mut task::Context) -> Async<Self::Item>;
}
- RFCs: April - May
- Implementation & experimentation: May - September
- Discuss tomorrow morning