Network services WG
Network services WG
- Async IO
- Library support for network services
Library support
Help us decide what to prioritize the rest of this afternoon!
async/await
Concat two pages
- HTTP GET to URL 1
- HTTP GET to URL 2
- Combine the bodies of the two responses
(Ignore that this is embarrassingly parallel.)
Concat two pages: blocking IO
Client API
impl Client {
fn get(&mut self, url: &Url)
-> io::Result<String> { ... }
}Concat two pages: blocking IO
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)
}Concat two pages: futures
Client API
impl Client {
fn get(&mut self, url: &Url)
-> impl Future<Item = String, Error = io::Error>
{ ... }
}Concat two pages: futures
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
})
}Concat two pages: async/await
Client API
impl Client {
fn get(&mut self, url: &Url)
-> impl Future<Item = String, Error = io::Error>
{ ... }
}(The same API!)
Concat two pages: async/await
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
async fn foo() -> Tis a function that returnsimpl Future<Item = T>- Inside the body, you
return T; your function body compiles to a stackless coroutine
await
- Transforms a
Future<Item = T>intoT - Can only
awaitinside of anasync fn - Yields control of the coroutine until the future resolves
Library support
- Move
Future,IntoFuture, andtaskinto 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>;
}
From here to there
- RFCs: April - May
- Implementation & experimentation: May - September
- Discuss tomorrow morning