We can reduce entire mio, async_std::net, and tokio::net down to just a single type! Here's how... 1/12
-
Show this thread
-
Consider this type explosion: each of mio/mio-uds, async-std, and tokio contains types TcpStream, TcpListener, UdpStream, UnixListener, UnixDatagram, and UnixStream. That's 3 * 6 = 18 different types just for async networking! Wouldn't it be nice if 1 type sufficed? 2/12pic.twitter.com/JLLdwOEAwG
1 reply 2 retweets 13 likesShow this thread -
Those 3 libraries are replacing networking types from the standard library with their async equivalents. But what if instead of *replacing* the standard library with new types, we *complement* it with a single new type? 3/12
1 reply 1 retweet 9 likesShow this thread -
Let's introduce a new type Async<T> that "asyncifies" synchronous I/O types. For example, imagine you take a standard TcpStream and simply wrap it with Async to get an Async<TcpStream>! 4/12
2 replies 1 retweet 18 likesShow this thread -
You can think of Async<T> as a smart wrapper around a synchronous I/O type T. If T implements Read/Write, then Async<T> implements AsyncRead/AsyncWrite. 5/12pic.twitter.com/7HPfWSBj5U
1 reply 0 retweets 16 likesShow this thread -
Async<T> is not restricted to the standard networking types - it also seamlessly integrates with types from other libraries like socket2 or filedescriptor! 6/12
1 reply 0 retweets 7 likesShow this thread -
Async<T> also works with arbitrary file descriptors because its constructor accepts anything that implements AsRawFd (on Unix) or AsRawSocket (on Windows). 7/12pic.twitter.com/XlH0wSY8N6
1 reply 0 retweets 8 likesShow this thread -
Since TcpStream::connect(addr) is a blocking function, we'll need it in an async flavor. Thanks to socket2 created by
@alex_crichton, async connect is easy to do: create a Socket, put it into non-blocking mode, call connect(addr), and wait until the socket becomes ready. 8/12pic.twitter.com/GSxUfLGHsP
1 reply 0 retweets 11 likesShow this thread -
How do we "asyncify" methods like UdpSocket::send(buf)? There's an elegant solution invented by
@yoshuawuyts - methods Async::write_with(f) and Async::read_with(f) turn synchronous operation f into an asynchronous operation. 9/12pic.twitter.com/KwXmP0X0s8
1 reply 0 retweets 15 likesShow this thread -
Async<T> is based on research done by
@yoshuawuyts, who managed to simplify mio and reactors down to a really small API with a straightforward implementation. 10/121 reply 0 retweets 20 likesShow this thread
This is what an async TCP echo server looks like. (I'll explain the .unwrap().forget() part later) 11/12pic.twitter.com/OsBIrIGz0j
-
-
And this is a simple HTTP GET request. 12/12pic.twitter.com/BlLI0Hozz0
2 replies 0 retweets 30 likesShow this threadThanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.