channels_io/buf/
chain.rs

1use crate::buf::{Buf, BufMut};
2
3/// The `chain` adapter.
4///
5/// See: [`Buf::chain`] and [`BufMut::chain`].
6#[derive(Debug, Clone, Copy)]
7pub struct Chain<A, B> {
8	first: A,
9	second: B,
10}
11
12impl<A, B> Chain<A, B> {
13	pub(super) fn new(first: A, second: B) -> Self {
14		Self { first, second }
15	}
16
17	/// Get a reference to the first buffer in the chain.
18	#[inline]
19	pub fn first(&self) -> &A {
20		&self.first
21	}
22
23	/// Get a mutable reference to the first buffer in the chain.
24	#[inline]
25	pub fn first_mut(&mut self) -> &mut A {
26		&mut self.first
27	}
28
29	/// Get a reference to the second buffer in the chain.
30	#[inline]
31	pub fn second(&self) -> &B {
32		&self.second
33	}
34
35	/// Get a mutable reference to the second buffer in the chain.
36	#[inline]
37	pub fn second_mut(&mut self) -> &mut B {
38		&mut self.second
39	}
40
41	/// Destruct the adapter and get back the first buffer.
42	#[inline]
43	pub fn into_first(self) -> A {
44		self.first
45	}
46
47	/// Destruct the adapter and get back the second buffer.
48	#[inline]
49	pub fn into_second(self) -> B {
50		self.second
51	}
52
53	/// Destruct the adapter and get back both buffers.
54	#[inline]
55	pub fn into_inner(self) -> (A, B) {
56		(self.first, self.second)
57	}
58}
59
60impl<A, B> Buf for Chain<A, B>
61where
62	A: Buf,
63	B: Buf,
64{
65	fn remaining(&self) -> usize {
66		self.first.remaining() + self.second.remaining()
67	}
68
69	fn chunk(&self) -> &[u8] {
70		if self.first.has_remaining() {
71			self.first.chunk()
72		} else {
73			self.second.chunk()
74		}
75	}
76
77	fn advance(&mut self, n: usize) {
78		let a_rem = self.first.remaining();
79
80		if n > a_rem {
81			self.first.advance(a_rem);
82			self.second.advance(n - a_rem);
83		} else {
84			self.first.advance(n);
85		}
86	}
87}
88
89impl<A, B> BufMut for Chain<A, B>
90where
91	A: BufMut,
92	B: BufMut,
93{
94	fn remaining_mut(&self) -> usize {
95		self.first.remaining_mut() + self.second.remaining_mut()
96	}
97
98	fn chunk_mut(&mut self) -> &mut [u8] {
99		if self.first.has_remaining_mut() {
100			self.first.chunk_mut()
101		} else {
102			self.second.chunk_mut()
103		}
104	}
105
106	fn advance_mut(&mut self, n: usize) {
107		let a_rem = self.first.remaining_mut();
108
109		if n > a_rem {
110			self.first.advance_mut(a_rem);
111			self.second.advance_mut(n - a_rem);
112		} else {
113			self.first.advance_mut(n);
114		}
115	}
116}