gpt4 book ai didi

websocket - 如何将Vec 转换为bson::document::Document?

转载 作者:行者123 更新时间:2023-12-03 11:35:23 26 4
gpt4 key购买 nike

我将收到 tungstenite::Message ,它将包含来自客户端的bson文档。我可以将tungstenite::Message转换为Vec<u8>,但是如何在服务器端将其转换回 bson::document::Document
像这样的东西:

if msg.is_binary() {
let bin = msg.into_data();
let doc = mongodb::bson::Document::from_reader(&mut bin); //getting error
}
错误:-
error[E0277]: the trait bound `std::vec::Vec<u8>: std::io::Read` is not satisfied
--> src/main.rs:52:60
|
52 | let doc = mongodb::bson::Document::from_reader(&mut bin);
| ^^^^^^^^ the trait `std::io::Read` is not implemented for `std::vec::Vec<u8>`
|
::: /home/voldimot/.cargo/registry/src/github.com-1ecc6299db9ec823/bson-1.0.0/src/document.rs:530:27
|
530 | pub fn from_reader<R: Read + ?Sized>(reader: &mut R) -> crate::de::Result<Document> {
| ---- required by this bound in `bson::document::Document::from_reader`

最佳答案

您可以使用 std::io::Cursor :

if msg.is_binary() {
let mut bin = std:::io::Cursor::new(msg.into_data());
let doc = mongodb::bson::Document::from_reader(&mut bin);
}

关于websocket - 如何将Vec <u8>转换为bson::document::Document?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63074618/

26 4 0