Home > database >  Expecting different type to given type
Expecting different type to given type

Time:01-13

I am trying to do some async websocket programming. So far my code looks like

use native_tls::TlsConnector;
use websocket::client::sync::Client;
use websocket::client::ParseError;
use websocket::futures::{Future, Sink, Stream};
use websocket::r#async::client::ClientNew;
use websocket::stream::sync::TcpStream;
use websocket::stream::sync::TlsStream;
use websocket::ClientBuilder;
use websocket::OwnedMessage;
use websocket::WebSocketError;

fn main() {
    let twitch_websocket_url: &str = "lol";
    let tls_connector: native_tls::Result<TlsConnector> = TlsConnector::new();
    if let Err(error) = tls_connector.as_ref() {
        println!("Error: {}", error);
    }

    let client_builder: Result<ClientBuilder, ParseError> =
        ClientBuilder::new(&twitch_websocket_url);
    if let Err(parse_error) = client_builder.as_ref() {
        println!("Error: {}", parse_error);
    }

    // Get future for async connection.
    let future_conn: ClientNew<TlsStream<TcpStream>> = client_builder
        .unwrap()
        .async_connect_secure(Some(tls_connector.unwrap()));
    if let Err(wserror) = future_conn.as_ref() {
        println!("Error: {}", wserror);
    }
}

I am getting the following error:

error[E0308]: mismatched types
   --> src/main.rs:28:53
    |
28  |       let future_conn: ClientNew<TlsStream<TcpStream>> = client_builder.unwrap()
    |  ______________________-------------------------------___^
    | |                      |
    | |                      expected due to this
29  | |         .async_connect_secure(Some(tls_connector.unwrap()));
    | |___________________________________________________________^ expected struct `native_tls::TlsStream`, found struct `websocket::client::r#async::TlsStream`
    |

but this is strange because ClientNew<TlsStream<TcpStream>> is not a native_tls::TlsStream as seen here https://docs.rs/websocket/latest/websocket/client/async/type.ClientNew.html

I thought maybe there was mix up with names so I tried removing some use statements but to no avail.

CodePudding user response:

You're calling websocket::ClientBuilder::async_connect_secure which returns ClientNew<TlsStream<TcpStream>>. If you click the actual TlsStream in that return type in the doc page, it takes you to websocket::client::async::TlsStream. If you compare that TlsStream to your use websocket::stream::sync::TlsStream you can see they are clearly different types. In other words, your let future_conn: ClientNew<TlsStream<TcpStream>> is declared with the wrong TlsStream. If you aren't using this other TlsStream anywhere then it's just a matter of fixing your use statements to point to the right one.

As for the compiler error message, it may be helpful to know that websocket::stream::sync::TlsStream turns out to just be a re-export (pub use) of native_tls::TlsStream.

  • Related