jonhoo / async-bincode

Asynchronous access to a bincode-encoded item stream.
Apache License 2.0
68 stars 9 forks source link

AsyncBincodeReader::from returns blank buffer (unsure how to properly use crate). #23

Closed IoIxD closed 1 month ago

IoIxD commented 1 month ago
pub async fn deserialize<'a, T>(
    bytes: BufReader<&mut TcpStream>,
) -> Result<T, Box<dyn std::error::Error>>
where
    T: bincode::Decode + std::fmt::Debug,
{
    let buf: AsyncBincodeReader<BufReader<&mut TcpStream>, T> = AsyncBincodeReader::from(bytes);
    println!("{:?}", buf.buffer());
    Ok(bincode::decode_from_slice(&mut buf.buffer(), config::standard())?.0)
}

The print call returns [], and bincode fails.

What am I doing wrong? How am I supposed to use this?

jonhoo commented 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().

IoIxD commented 1 month ago

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.

jonhoo commented 5 days ago

BrokenPipe generally implies that the other side of the TcpStream has closed the connection