Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to return a ws::Handler trait object by the factory param of ws::connect? #324

Open
ngugc opened this issue Jun 14, 2020 · 0 comments
Open

Comments

@ngugc
Copy link

ngugc commented Jun 14, 2020

    pub fn connect<U, F, H>(url: U, factory: F) -> Result<()> 
    where
        U: Borrow<str>,
        F: FnMut(Sender) -> H,
        H: Handler, 

This is the signature of ws::connect. A ws::Handler trait object should be returned by F.
I have the following implementations of ws::Handler.

    struct A { ... }
    impl ws::Handler for A {}
    
    struct B { ... }
    impl ws::Handler for B {}
    
    fn new_ws(t: &String) -> Box<dyn ws::Handler> {
        match t {
            "A" => Box::new(A { ... }),
            "B" => Box::new(B { ... }),
            &_ => unimplemented!(),
        }
    }

How can I use new_ws in ws::connect as below?

    ws::connect(URL, |sender| {
        new_ws("A")
    }).unwrap_or_else(|err| {
        error!("websocket error: {:?}", err);
    })

The error:

expected an `Fn<(ws::message::Message,)>` closure, found `dyn ws::handler::Handler

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant