0
votes

I wanted to implement the futures::Sink trait in my struct and kept getting yelled at by the compiler telling me to import and implement the trait. So I came up with this example which surprisingly (to me at least) still doesn't work:

use futures::Sink;
use std::pin::Pin;
use std::task::{Context, Poll};

struct Dummy;
impl Sink<tungstenite::Message> for Dummy {
    type Error = tungstenite::Error;

    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        todo!()
    }

    fn start_send(self: Pin<&mut Self>, item: tungstenite::Message) -> Result<(), Self::Error> {
        todo!()
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        todo!()
    }

    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        todo!()
    }
}

#[tokio::main]
async fn main() {
    let mut dummy = Dummy;
    dummy.start_send(tungstenite::Message::Text("".to_owned()));
}

I get:

error[E0599]: no method named `start_send` found for struct `Dummy` in the current scope
  --> src/main.rs:29:11
   |
5  | struct Dummy;
   | ------------- method `start_send` not found for this
...
29 |     dummy.start_send(tungstenite::Message::Text("".to_owned()));
   |           ^^^^^^^^^^ method not found in `Dummy`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `start_send`, perhaps you need to implement it:
           candidate #1: `futures::Sink`

error: aborting due to previous error

I'm not making a generic struct, there are no bounds issues, no lifetimes. It's just a direct trait impl which the compiler refuses to recognize.

cargo tree | grep futures says all versions are v0.3.13, and here's the docs

I even tried Pin::new(&mut dummy).start_send because of the signature, but still nothing<. I have no idea what's going on here, or what kind of silly mistake did I make...

(By the way, I had a go at the similar impl Stream before this one and had no issues whatsoever)

1

1 Answers

1
votes

I even tried Pin::new(&mut dummy).start_send because of the signature, but still nothing<. I have no idea what's going on here, or what kind of silly mistake did I make...

I also tried wrapping it in a Pin, since the start_send() takes in a Pin<&mut Self> as self, and this does seem to work for me.

use futures::sink::Sink;
use std::pin::Pin;
use std::task::{Context, Poll};

struct Message {}

struct Dummy;
impl Sink<Message> for Dummy {
    type Error = ();

    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        todo!()
    }

    fn start_send(self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error> {
        todo!()
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        todo!()
    }

    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        todo!()
    }
}

#[tokio::main]
async fn main() {
    let mut dummy = Dummy;
    let pin_dummy = Pin::new(&mut dummy);
    pin_dummy.start_send(Message {});
}

Rust Playground