如何将 Vec<u8> 转换为 bson::d ocument::D ocument?



我将收到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); 
}

最新更新