1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use futures_util::{AsyncWrite, AsyncWriteExt};
use vortex_error::VortexResult;

use crate::messages::{EncoderMessage, MessageEncoder};

pub struct AsyncMessageWriter<W> {
    write: W,
    encoder: MessageEncoder,
}

impl<W: AsyncWrite + Unpin> AsyncMessageWriter<W> {
    pub fn new(write: W) -> Self {
        Self {
            write,
            encoder: MessageEncoder::default(),
        }
    }

    pub async fn write_message(&mut self, message: EncoderMessage<'_>) -> VortexResult<()> {
        for buffer in self.encoder.encode(message) {
            self.write.write_all(&buffer).await?;
        }
        Ok(())
    }

    pub fn inner(&self) -> &W {
        &self.write
    }

    pub fn into_inner(self) -> W {
        self.write
    }
}