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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use core::pin::Pin;
use core::task::{Context, Poll};

use alloc::vec::Vec;

use crate::transaction::{
	AsyncWriteTransaction, Buffered, Unbuffered, WriteTransaction,
};
use crate::{AsyncWrite, Write};

/// Kind of the transaction.
#[derive(Debug)]
pub enum WriteTransactionKind<'a> {
	/// A [`Buffered`] transaction.
	Buffered(&'a mut Vec<u8>),
	/// An [`Unbuffered`] transaction.
	Unbuffered,
}

/// A unified interface for calling code to choose which transaction kind it wants
/// at runtime.
#[derive(Debug)]
pub enum WriteTransactionVariant<'a, W> {
	/// A [`Buffered`] transaction.
	Buffered(Buffered<'a, W>),
	/// An [`Unbuffered`] transaction.
	Unbuffered(Unbuffered<W>),
}

impl<'a, W> WriteTransactionVariant<'a, W> {
	/// Create a [`Buffered`] transaction.
	pub fn buffered(writer: W, buf: &'a mut Vec<u8>) -> Self {
		Self::Buffered(Buffered::new(writer, buf))
	}

	/// Create an [`Unbuffered`] transaction.
	pub fn unbuffered(writer: W) -> Self {
		Self::Unbuffered(Unbuffered::new(writer))
	}

	/// Create a new transaction based on `kind`.
	pub fn new(writer: W, kind: WriteTransactionKind<'a>) -> Self {
		match kind {
			WriteTransactionKind::Buffered(buf) => {
				Self::buffered(writer, buf)
			},
			WriteTransactionKind::Unbuffered => {
				Self::unbuffered(writer)
			},
		}
	}

	/// Get a reference to the underlying writer.
	pub fn writer(&self) -> &W {
		match self {
			Self::Buffered(x) => x.writer(),
			Self::Unbuffered(x) => x.writer(),
		}
	}

	/// Get a mutable reference to the underlying writer.
	pub fn writer_mut(&mut self) -> &mut W {
		match self {
			Self::Buffered(x) => x.writer_mut(),
			Self::Unbuffered(x) => x.writer_mut(),
		}
	}
}

impl<'a, W> Write for WriteTransactionVariant<'a, W>
where
	W: Write,
{
	type Error = W::Error;

	fn write_slice(
		&mut self,
		buf: &[u8],
	) -> Result<usize, Self::Error> {
		match self {
			Self::Buffered(x) => x.write_slice(buf),
			Self::Unbuffered(x) => x.write_slice(buf),
		}
	}

	fn flush_once(&mut self) -> Result<(), Self::Error> {
		match self {
			Self::Buffered(x) => x.flush_once(),
			Self::Unbuffered(x) => x.flush_once(),
		}
	}
}

impl<'a, W> WriteTransaction for WriteTransactionVariant<'a, W>
where
	W: Write,
{
	fn finish(self) -> Result<(), Self::Error> {
		match self {
			Self::Buffered(x) => x.finish(),
			Self::Unbuffered(x) => x.finish(),
		}
	}
}

impl<'a, W> AsyncWrite for WriteTransactionVariant<'a, W>
where
	W: AsyncWrite + Unpin,
{
	type Error = W::Error;

	fn poll_write_slice(
		mut self: Pin<&mut Self>,
		cx: &mut Context,
		buf: &[u8],
	) -> Poll<Result<usize, Self::Error>> {
		match *self {
			Self::Buffered(ref mut x) => {
				Pin::new(x).poll_write_slice(cx, buf)
			},
			Self::Unbuffered(ref mut x) => {
				Pin::new(x).poll_write_slice(cx, buf)
			},
		}
	}

	fn poll_flush_once(
		mut self: Pin<&mut Self>,
		cx: &mut Context,
	) -> Poll<Result<(), Self::Error>> {
		match *self {
			Self::Buffered(ref mut x) => {
				Pin::new(x).poll_flush_once(cx)
			},
			Self::Unbuffered(ref mut x) => {
				Pin::new(x).poll_flush_once(cx)
			},
		}
	}
}

impl<'a, W> AsyncWriteTransaction for WriteTransactionVariant<'a, W>
where
	W: AsyncWrite + Unpin,
{
	fn poll_finish(
		mut self: Pin<&mut Self>,
		cx: &mut Context,
	) -> Poll<Result<(), Self::Error>> {
		match *self {
			Self::Buffered(ref mut x) => Pin::new(x).poll_finish(cx),
			Self::Unbuffered(ref mut x) => {
				Pin::new(x).poll_finish(cx)
			},
		}
	}
}