Closed IoIxD closed 1 month ago
Ah, no, you're not supposed to use bincode
on top of this. This already calls bincode
internally. Instead, you should create an AsyncBincodeReader
the way you are, and then utilize the fact that AsyncBincodeReader
implements Stream
to grab out each deserialized item using .next()
.
Ok so I found code from other people who have used the crate and I think this is right:
use async_bincode::tokio::AsyncBincodeReader;
use tokio::{io::BufReader, net::TcpStream};
use futures::stream::StreamExt;
pub async fn deserialize<'a, T>(
bytes: BufReader<&mut TcpStream>,
) -> Result<T, Box<dyn std::error::Error>>
where
T: for<'de> serde::de::Deserialize<'de>,
{
let mut buf = AsyncBincodeReader::from(bytes);
Ok(buf.next().await.unwrap().unwrap()) // using unwrap to verify that the error I get is here and nowhere else.
}
But as the comment in the code would elude to, I'm getting an error:
called `Result::unwrap()` on an `Err` value: Io(Kind(BrokenPipe))
Is this not the correct usage either?
EDIT: I just checked the code and realized that BrokenPipe implies bytes is empty, stemming from the same error I had earlier. So there's something else I'm doing wrong, clearly.
BrokenPipe
generally implies that the other side of the TcpStream
has closed the connection
The print call returns
[]
, and bincode fails.What am I doing wrong? How am I supposed to use this?