我看一下生锈的帖子:https://github.com/cyderize/rust-websocket/ 在源文件https://github.com/cyderize/rust-websocket/blob/master/src/client/response.rs中 我见过方法begin():
pub fn begin(self) -> Client<DataFrame, Sender<W>, Receiver<R>> {
let (reader, writer) = self.into_inner();
let sender = Sender::new(writer);
let receiver = Receiver::new(reader);
Client::new(sender, receiver)
}
所以......我决定创建一些简单的包装函数:
fn get_transport(url: &str) -> Client<DataFrame, Sender<Write>, Receiver<Read>> {
let url = Url::parse(url).unwrap();
let request = Client::connect(url).unwrap();
let response = request.send().unwrap();
let mut ws = response.begin();
ws
}
但不幸的是我得到了错误结果:
error: the trait `core::marker::Sized` is not implemented for the type `websocket::ws::sender::Sender<std::io::Write>` [E0277]
src/lib.rs:36 fn get_transport(url: &str) -> Client<DataFrame, Sender<Write>, Receiver<Read>> {
有人可以形容我为什么不能以这种方式回报价值。 response.rs
中的begin()方法有何不同?
我应该如何在我的案件中返回价值?
UPD1: 在@ker建议之后,我有下一个代码:
fn get_transport<W: Write, R: Read, S: Sender<W>, RE: Receiver<R>>(url: &str) -> Client<DataFrame, S, RE> {
let url = Url::parse(url).unwrap();
let request = Client::connect(url).unwrap();
let response = request.send().unwrap();
let mut ws = response.begin();
ws
}
但是在下一次编译中收到了下一个错误:
src/lib.rs:45:5: 45:7 error: mismatched types:
expected `websocket::client::Client<websocket::dataframe::DataFrame, S, RE>`,
found `websocket::client::Client<websocket::dataframe::DataFrame, websocket::client::sender::Sender<websocket::stream::WebSocketStream>, websocket::client::receiver::Receiver<websocket::stream::WebSocketStream>>`
(expected type parameter,
found struct `websocket::client::sender::Sender`) [E0308]
src/lib.rs:45 ws
^~
src/lib.rs:45:5: 45:7 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to previous error
似乎我使用了错误的发件人和错误的接收者:\ 在更正使用部分之后,我已经得到了结果代码:
fn get_transport(url: &str) -> Client<DataFrame, Sender<WebSocketStream>, Receiver<WebSocketStream>> {
let url = Url::parse(url).unwrap();
let request = Client::connect(url).unwrap();
let response = request.send().unwrap();
let mut ws = response.begin();
ws
}
不幸的是我无法找出为什么我不能使用泛型作为结果值。像这样:
fn get_transport<R: Read, W: Write>(url: &str) -> Client<DataFrame, Sender<W>, Receiver<R>>
在这种情况下,我收到错误消息:
expected `websocket::client::Client<websocket::dataframe::DataFrame, websocket::client::sender::Sender<W>, websocket::client::receiver::Receiver<R>>`,
found `websocket::client::Client<websocket::dataframe::DataFrame, websocket::client::sender::Sender<websocket::stream::WebSocketStream>, websocket::client::receiver::Receiver<websocket::stream::WebSocketStream>>`
(expected type parameter,
found enum `websocket::stream::WebSocketStream`) [E0308]
src/lib.rs:45 ws
^~
答案 0 :(得分:3)
由于任何类型都可能影响特征,因此通过了解它实现的特征,你永远无法知道对象的大小。你可以做的是,使你的函数分别通用于实现Write和Read的类型。这样您就可以返回对象,而不知道对象的实际类型是什么。
fn get_transport<W: Write, R: Read, S: Sender<W>, RE: Receiver<R>>(url: &str) -> Client<DataFrame, S, RE> {
let url = Url::parse(url).unwrap();
let request = Client::connect(url).unwrap();
let response = request.send().unwrap();
let mut ws = response.begin();
ws
}
添加泛型的原因是因为泛型标签指向实际类型,而不知道除了它们实现的特征之外的任何类型。
答案 1 :(得分:1)
您希望将返回类型中的Write
和Send
替换为具体类型。
尝试将行let mut ws = response.begin();
更改为let mut ws: () = response.begin()
,您将看到一条错误消息,告诉您从begin函数返回的具体类型。