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`