Rust - Suddenly Can't find split method on WebSocket?

1.7k views Asked by At

I'm a rust newbie and I'm having a lot of trouble getting some simple ws examples to run.

I have a new project with this in the Cargo.toml file:

[package]
name = "ouranos4"
version = "0.1.0"
authors = ["jasongoodwin <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = { version = "1", features = ["full"] }
warp = "0.3"

And then I have the warp ws example found here: https://github.com/seanmonstar/warp/blob/master/examples/websockets.rs

// use futures::{FutureExt, StreamExt};
use warp::Filter;

#[tokio::main]
async fn main() {
    let routes = warp::path("echo")
        // The `ws()` filter will prepare the Websocket handshake.
        .and(warp::ws())
        .map(|ws: warp::ws::Ws| {
            // And then our closure will be called when it completes...
            ws.on_upgrade(|websocket| {
                // Just echo all messages back...
                let (tx, rx) = websocket.split();
                rx.forward(tx).map(|result| {
                    if let Err(e) = result {
                        eprintln!("websocket error: {:?}", e);
                    }
                })
            })
        });

    warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}

And I swear this was compiling but out of the blue I'm getting these errors:

   Compiling ouranos4 v0.1.0 (/Users/jasongoodwin/Development/src/ouranos4)
error[E0599]: no method named `split` found for struct `WebSocket` in the current scope
    --> src/main.rs:13:42
     |
13   |                 let (tx, rx) = websocket.split();
     |                                          ^^^^^ method not found in `WebSocket`
     |
    ::: /Users/jasongoodwin/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.14/src/stream/stream/mod.rs:1359:8
     |
1359 |     fn split<Item>(self) -> (SplitSink<Self, Item>, SplitStream<Self>)
     |        ----- the method is available for `Box<WebSocket>` here
     |
     = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
     |
2    | use futures_util::stream::stream::StreamExt;
     |

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: could not compile `ouranos4`

I've recreated the project, cleaned the project, done everything imaginable. Switched the rust compiler version to nightly and back. I'm really confused as to why I can't split this now. I've tried different examples, different crates, and I keep seeing this error. I'm tempted to throw my laptop away and try on a different computer :P

1

There are 1 answers

0
JasonG On

I needed to add some more imports! Thanks to kmdreko.

use futures::StreamExt;
use futures::FutureExt;