channels_io/transaction/
unbuffered.rs

1use core::pin::Pin;
2use core::task::{Context, Poll};
3
4use pin_project::pin_project;
5
6use crate::transaction::{AsyncWriteTransaction, WriteTransaction};
7use crate::{AsyncWrite, Write};
8
9/// An unbuffered transaction that does no buffering of data.
10///
11/// [`Unbuffered`] transactions proxy IO calls directly to the underlying writer
12/// in a "1-1" fashion.
13#[derive(Debug)]
14#[pin_project]
15pub struct Unbuffered<W> {
16	#[pin]
17	writer: W,
18}
19
20impl<W> Unbuffered<W> {
21	/// Create a new [`Unbuffered`] transaction.
22	pub fn new(writer: W) -> Self {
23		Self { writer }
24	}
25
26	/// Get a reference to the underlying writer.
27	#[must_use]
28	pub fn writer(&self) -> &W {
29		&self.writer
30	}
31
32	/// Get a mutable reference to the underlying writer.
33	#[must_use]
34	pub fn writer_mut(&mut self) -> &mut W {
35		&mut self.writer
36	}
37
38	/// Get a pinned mutable reference to the underlying writer.
39	#[must_use]
40	pub fn writer_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W> {
41		self.project().writer
42	}
43}
44
45impl<W> Write for Unbuffered<W>
46where
47	W: Write,
48{
49	type Error = W::Error;
50
51	fn write_slice(
52		&mut self,
53		buf: &[u8],
54	) -> Result<usize, Self::Error> {
55		self.writer.write_slice(buf)
56	}
57
58	fn flush_once(&mut self) -> Result<(), Self::Error> {
59		self.writer.flush_once()
60	}
61}
62
63impl<W> WriteTransaction for Unbuffered<W>
64where
65	W: Write,
66{
67	fn finish(self) -> Result<(), Self::Error> {
68		Ok(())
69	}
70}
71
72impl<W> AsyncWrite for Unbuffered<W>
73where
74	W: AsyncWrite,
75{
76	type Error = W::Error;
77
78	fn poll_write_slice(
79		self: Pin<&mut Self>,
80		cx: &mut Context,
81		buf: &[u8],
82	) -> Poll<Result<usize, Self::Error>> {
83		let this = self.project();
84		this.writer.poll_write_slice(cx, buf)
85	}
86
87	fn poll_flush_once(
88		self: Pin<&mut Self>,
89		cx: &mut Context,
90	) -> Poll<Result<(), Self::Error>> {
91		let this = self.project();
92		this.writer.poll_flush_once(cx)
93	}
94}
95
96impl<W> AsyncWriteTransaction for Unbuffered<W>
97where
98	W: AsyncWrite,
99{
100	fn poll_finish(
101		self: Pin<&mut Self>,
102		_: &mut Context,
103	) -> Poll<Result<(), Self::Error>> {
104		Poll::Ready(Ok(()))
105	}
106}