如何转换特征对象来构造?

时间:2020-06-07 16:16:39

标签: rust ws

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

这是ws :: connect的签名。 ws :: Handler特性对象应在F中返回。 我有以下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!(),
    }
}

如何在ws :: connect中使用new_ws,如下所示?

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

错误:

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

0 个答案:

没有答案