How to implement Sink for an struct

Assume I have an struct called MySink

struct MySink‌ {
    some_filed: i32,
}

How can I implement Sink trait for MySink to send some simple Strings ? I‌ can't understand it by reading the documentation, could you please give me a simple code ?

Which Sink trait?

Pardon me. I totally forgot it !
futures::sink::Sink

1 Like

I would look at the Sink impl for Vec to see a basic implementation

https://docs.rs/futures-sink/0.3.4/src/futures_sink/lib.rs.html#166-186

Basically Sink is usually implemented on a proxy to something else, for example Sender implements Sink, and is a proxy for some shared state that it uses to communicate to the Receiver
poll_ready is supposed to asyncronously allocate space, and once it's completed start_send starts sending the value (storing it in the previosly allocated space). poll_flush ensures that all values that were previously sent, are actually stored in the allocated space. poll_close waits for the channel to close.

You can think of any function prefixed by poll as it's own future. If you aren't doing any async work, then you can just use Vec's strategy of returning Poll::Ready from all of these methods.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.