1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use std::io::Result;

use super::VarInt;

use futures::{io::AsyncWrite, io::AsyncWriteExt};

/// Like VarIntWriter, but asynchronous.
#[async_trait::async_trait]
pub trait VarIntAsyncWriter {
    /// Write a VarInt integer to an asynchronous writer.
    async fn write_varint_async<VI: VarInt + Send>(&mut self, n: VI) -> Result<usize>;
}

#[async_trait::async_trait]
impl<AW: AsyncWrite + Send + Unpin> VarIntAsyncWriter for AW {
    async fn write_varint_async<VI: VarInt + Send>(&mut self, n: VI) -> Result<usize> {
        let mut buf = [0_u8; 10];
        let b = n.encode_var(&mut buf);
        self.write_all(&buf[0..b]).await?;
        Ok(b)
    }
}