s2n_codec/encoder/
scatter.rs1use crate::{Encoder, EncoderBuffer};
5
6pub struct Buffer<'a> {
7 inner: EncoderBuffer<'a>,
8 #[cfg(feature = "bytes")]
9 extra: Option<bytes::Bytes>,
10}
11
12impl<'a> Buffer<'a> {
13 #[inline]
15 pub fn flatten(&mut self) -> &mut EncoderBuffer<'a> {
16 self.flush();
17 &mut self.inner
18 }
19}
20
21#[cfg(feature = "bytes")]
23impl<'a> Buffer<'a> {
24 #[inline]
26 pub fn new(inner: EncoderBuffer<'a>) -> Self {
27 Self { inner, extra: None }
28 }
29
30 #[inline]
35 pub fn new_with_extra(inner: EncoderBuffer<'a>, extra: Option<bytes::Bytes>) -> Self {
36 Self { inner, extra }
37 }
38
39 #[inline]
44 pub fn into_inner(self) -> (EncoderBuffer<'a>, Option<bytes::Bytes>) {
45 (self.inner, self.extra)
46 }
47
48 #[inline]
50 pub fn inner_mut(&mut self) -> (&mut EncoderBuffer<'a>, &Option<bytes::Bytes>) {
51 (&mut self.inner, &self.extra)
52 }
53
54 #[inline]
56 pub fn clear(&mut self) {
57 self.inner.set_position(0);
58 self.extra = None;
59 }
60
61 #[inline]
62 fn flush(&mut self) {
63 if let Some(extra) = self.extra.take() {
65 self.inner.write_slice(&extra);
66 }
67 }
68}
69
70#[cfg(not(feature = "bytes"))]
72impl<'a> Buffer<'a> {
73 #[inline]
74 pub fn new(inner: EncoderBuffer<'a>) -> Self {
75 Self { inner }
76 }
77
78 #[inline]
79 pub fn new_with_extra(inner: EncoderBuffer<'a>, extra: Option<&'static [u8]>) -> Self {
80 debug_assert!(extra.is_none());
81 Self { inner }
82 }
83
84 #[inline]
85 pub fn into_inner(self) -> (EncoderBuffer<'a>, Option<&'static [u8]>) {
86 (self.inner, None)
87 }
88
89 #[inline]
90 pub fn inner_mut(&mut self) -> (&mut EncoderBuffer<'a>, &Option<&'static [u8]>) {
91 (&mut self.inner, &None)
92 }
93
94 #[inline]
95 pub fn clear(&mut self) {
96 self.inner.set_position(0);
97 }
98
99 #[inline(always)]
100 fn flush(&mut self) {}
101}
102
103impl Encoder for Buffer<'_> {
104 #[cfg(feature = "bytes")]
106 const SPECIALIZES_BYTES: bool = true;
107
108 #[inline]
109 fn write_sized<F: FnOnce(&mut [u8])>(&mut self, len: usize, write: F) {
110 self.flush();
112 self.inner.write_sized(len, write)
113 }
114
115 #[inline]
116 fn write_slice(&mut self, slice: &[u8]) {
117 self.flush();
119 self.inner.write_slice(slice);
120 }
121
122 #[inline]
123 #[cfg(feature = "bytes")]
124 fn write_bytes(&mut self, bytes: bytes::Bytes) {
125 self.flush();
127 self.inner.assert_capacity(bytes.len());
129 self.extra = Some(bytes);
131 }
132
133 #[inline]
134 fn write_zerocopy<
135 T: zerocopy::IntoBytes + zerocopy::FromBytes + zerocopy::Unaligned,
136 F: FnOnce(&mut T),
137 >(
138 &mut self,
139 write: F,
140 ) {
141 self.flush();
143 self.inner.write_zerocopy(write);
144 }
145
146 #[inline]
147 fn write_repeated(&mut self, count: usize, value: u8) {
148 self.flush();
150 self.inner.write_repeated(count, value)
151 }
152
153 #[inline]
154 fn capacity(&self) -> usize {
155 self.inner.capacity()
156 }
157
158 #[inline]
159 #[cfg(feature = "bytes")]
160 fn len(&self) -> usize {
161 let mut len = self.inner.len();
162
163 if let Some(extra) = self.extra.as_ref() {
165 len += extra.len();
166 }
167
168 len
169 }
170
171 #[inline]
172 #[cfg(not(feature = "bytes"))]
173 fn len(&self) -> usize {
174 self.inner.len()
175 }
176}