gpt4 book ai didi

asynchronous - 在oneshot channel : object safety error中使用 `DeserializedOwned`特征

转载 作者:行者123 更新时间:2023-12-03 11:46:31 24 4
gpt4 key购买 nike

如下代码:

trait ClientResponse: DeserializeOwned + Send + Sized {}

struct ClientMsg {
...
resp: oneshot::Sender<Box<dyn ClientResponse>>
}

async fn client_thread(rx: mpsc::Receiver<ClientMsg>, client: reqwest::Client, base_url: Url) -> Result<(), Box<dyn Error>> {
while let Some(msg) = rx.recv().await {
...
let response = client.get(url).send().await?.json().await?;
msg.resp.send(response);
}
}
失败并出现错误:
error[E0038]: the trait `ClientResponse` cannot be made into an object
--> src/main.rs:16:11
|
16 | resp: oneshot::Sender<Box<dyn ClientResponse>>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ClientResponse` cannot be made into an object
|
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> src/main.rs:12:23
|
12 | trait ClientResponse: DeserializeOwned + Send + Sized {}
| -------------- ^^^^^^^^^^^^^^^^ ^^^^^ ...because it requires `Self: Sized`
| | |
| | ...because it requires `Self: Sized`
| this trait cannot be made into an object...
如您所见,在读取编译器错误之后,我尝试将 Sized特征添加为 super 特征,但是它仍然给我同样的错误。我不确定其他方法如何解决该问题,因为我想要一个客户端线程,该线程可以将响应反序列化为发送者确定的类型。

最佳答案

有一个箱子专门用于使Serde特质对象安全,称为erased-serde

关于asynchronous - 在oneshot channel : object safety error中使用 `DeserializedOwned`特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66295235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com