channels_io/transaction/
write_transaction.rs

1use core::future::Future;
2use core::pin::Pin;
3use core::task::{Context, Poll};
4
5use crate::{AsyncWrite, Write};
6
7/// This trait provides the methods that are useful for write transactions that
8/// work synchronously.
9pub trait WriteTransaction: Write {
10	/// Finish the transaction.
11	fn finish(self) -> Result<(), Self::Error>
12	where
13		Self: Sized;
14}
15
16/// This trait provides the methods that are useful for write transactions that
17/// work asynchronously.
18pub trait AsyncWriteTransaction: AsyncWrite {
19	/// Poll the future that finishes the transaction.
20	fn poll_finish(
21		self: Pin<&mut Self>,
22		cx: &mut Context,
23	) -> Poll<Result<(), Self::Error>>;
24
25	/// Finish the transaction.
26	fn finish(self) -> Finish<Self>
27	where
28		Self: Sized + Unpin,
29	{
30		Finish::new(self)
31	}
32}
33
34#[allow(missing_debug_implementations)]
35#[must_use = "futures do nothing unless you `.await` them"]
36pub struct Finish<T>
37where
38	T: AsyncWriteTransaction + Unpin,
39{
40	transaction: T,
41}
42
43impl<T> Finish<T>
44where
45	T: AsyncWriteTransaction + Unpin,
46{
47	pub fn new(transaction: T) -> Self {
48		Self { transaction }
49	}
50}
51
52impl<T> Future for Finish<T>
53where
54	T: AsyncWriteTransaction + Unpin,
55{
56	type Output = Result<(), T::Error>;
57
58	fn poll(
59		mut self: Pin<&mut Self>,
60		cx: &mut Context<'_>,
61	) -> Poll<Self::Output> {
62		Pin::new(&mut self.transaction).poll_finish(cx)
63	}
64}