vortex_ipc/messages/
writer_async.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use futures::{AsyncWrite, AsyncWriteExt};
5use vortex_error::VortexResult;
6
7use crate::messages::{EncoderMessage, MessageEncoder};
8
9pub struct AsyncMessageWriter<W> {
10    write: W,
11    encoder: MessageEncoder,
12}
13
14impl<W: AsyncWrite + Unpin> AsyncMessageWriter<W> {
15    pub fn new(write: W) -> Self {
16        Self {
17            write,
18            encoder: MessageEncoder::default(),
19        }
20    }
21
22    pub async fn write_message(&mut self, message: EncoderMessage<'_>) -> VortexResult<()> {
23        for buffer in self.encoder.encode(message) {
24            self.write.write_all(&buffer).await?;
25        }
26        Ok(())
27    }
28
29    pub fn inner(&self) -> &W {
30        &self.write
31    }
32
33    pub fn into_inner(self) -> W {
34        self.write
35    }
36}