tokio-rs / tokio-core

I/O primitives and event loop for async I/O in Rust
Apache License 2.0
634 stars 116 forks source link

FAQ collected from Gitter #137

Open carllerche opened 7 years ago

carllerche commented 7 years ago

Tracking questions that should probably be answered in the documentation.

what's the general approach for resource cleanup?

One cannot perform asynchronous operations in a drop function. One strategy would be to create a "cleanup" task. On drop of a resource (like a transport), send the socket + state to the cleanup task and perform the cleanup there.

How to do a hanshake

Roughly:


type MyTransport<T> = Framed<T, MyCodec>;

fn connect<T: Io>(io: T) -> Box<Future<MyTransport>> {
    let transport = io.framed(MyCodec::new());

    Box::new(transport.send(SomeMessage)
        .and_then(|transport| {
            // Get the next frame
            transport.into_future()
        })
        .and_then(|(frame, transport)| {
            // complete handshake
            transport
        }))
}
bluejekyll commented 7 years ago

Is there a reason you have the Box around only the first and_then() and not the second? Or is that a typo?

carllerche commented 7 years ago

Box is around the entire statement.. I think I am missing a closing ) though. (Edit: you are correct, I updated the snippet) Also, the box is mostly for simplifying the example and not a strict requirement.

bluejekyll commented 7 years ago

Yeah, I have my code littered with Boxes at the moment, I think I'm just going to wait for impl returns to start removing them.