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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//! Contains the trait the client uses to store slices of memory and basic implementations.
#[cfg(feature = "alloc")]
pub use alloc::AllocBuffer;
#[cfg(feature = "bump")]
pub use bump::{BumpBuffer, InsufficientSpace};
use crate::bytes::Bytes;
/// A trait to describe anything that can allocate memory.
///
/// Returned memory can be borrowed or owned. Either way, it is bound by the `'a`
/// lifetime - usually just the lifetime of the underlying buffer.
///
/// The client does not store any references to memory returned by this provider.
pub trait BufferProvider<'a> {
/// The type returned from a successful buffer provision.
/// Must implement [`AsMut`] so that it can be borrowed mutably right after allocation for
/// initialization and [`Into`] for storing as [`Bytes`].
type Buffer: AsMut<[u8]> + Into<Bytes<'a>>;
/// The error type returned from a failed buffer provision.
#[cfg(not(feature = "defmt"))]
type ProvisionError: core::fmt::Debug;
/// The error type returned from a failed buffer provision.
#[cfg(feature = "defmt")]
type ProvisionError: core::fmt::Debug + defmt::Format;
/// If successful, returns contiguous memory with a size in bytes of the `len` argument.
///
/// # Errors
///
/// Returns a value of its associated error type if the buffer provision fails.
fn provide_buffer(&mut self, len: usize) -> Result<Self::Buffer, Self::ProvisionError>;
}
#[cfg(feature = "bump")]
mod bump {
use core::{marker::PhantomData, slice};
use crate::buffer::BufferProvider;
/// Error returned when the [`BumpBuffer`]'s underlying buffer does not have enough unallocated space.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct InsufficientSpace;
/// Allocates memory from an underlying buffer by bumping up a pointer by the requested length.
///
/// Can be reset when no references to buffer contents exist.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct BumpBuffer<'a> {
ptr: *mut u8,
len: usize,
index: usize,
_phantom_data: PhantomData<&'a mut [u8]>,
}
impl<'a> BufferProvider<'a> for BumpBuffer<'a> {
type Buffer = &'a mut [u8];
type ProvisionError = InsufficientSpace;
/// Return the next `len` bytes from the buffer, advancing the internal tracking
/// index. Returns [`InsufficientSpace`] if there isn't enough room.
fn provide_buffer(&mut self, len: usize) -> Result<Self::Buffer, Self::ProvisionError> {
if self.remaining_len() < len {
Err(InsufficientSpace)
} else {
let start = self.index;
// Safety: we checked the bounds above meaning the resulting pointer
// is in the backing slice's range. This means the pointer arithmetic
// does not overflow.
// The pointer originates from the backing slice owned by this struct with the same lifetime.
let ptr = unsafe { self.ptr.add(start) };
self.index += len;
// Safety: the slice starts at the self.index offset which is not part of any previous reservation.
// Everything after this offset is not allocated and referenced.
// The lifetime is correct as the returned slice has the same lifetime as `Self` which is
// in turn has the lifetime of the backing slice.
let slice = unsafe { slice::from_raw_parts_mut(ptr, len) };
Ok(slice)
}
}
}
impl<'a> BumpBuffer<'a> {
/// Creates a new [`BumpBuffer`] with the provided slice as underlying buffer.
#[must_use]
pub fn new(slice: &'a mut [u8]) -> Self {
Self {
ptr: slice.as_mut_ptr(),
len: slice.len(),
index: 0,
_phantom_data: PhantomData,
}
}
/// Returns the remaining amount of unallocated bytes in the underlying buffer.
#[inline]
#[must_use]
pub fn remaining_len(&self) -> usize {
self.len - self.index
}
/// Invalidates all previous allocations by resetting the [`BumpBuffer`]'s internal tracking index into the underlying
/// buffer, allowing the underlying buffer to be reallocated down the line. After this, the bump buffer will allocate
/// starting with the first byte of the backing buffer again.
///
/// # Safety
///
/// This method is safe to call when no references to previously allocated slices or underlying buffer content exist.
/// The caller must ensure no more such references exist. In the context of the client, this is true when no more values
/// that have a lifetime tied to the used [`BumpBuffer`] instance exist.
///
/// # Example
///
/// ## Sound
///
/// ```rust,ignore
/// use rust_mqtt::buffer::BumpBuffer;
/// use rust_mqtt::client::Client;
/// use rust_mqtt::client::info::ConnectInfo;
/// use rust_mqtt::client::options::ConnectOptions;
/// use tokio::net::TcpStream;
/// use embedded_io_adapters::tokio_1::FromTokio;
///
/// let mut buffer = [0; 1024];
/// let mut buffer = BumpBuffer::new(&mut buffer);
/// let mut client: Client<'_, FromTokio<TcpStream>, _, 0, 1, 0, 0> = Client::new(&mut buffer);
///
/// {
/// // client_identifier lives inside buffer's backing buffer, so it prevents a reset call.
/// let ConnectInfo { client_identifier, .. } = client.connect(todo!(), &ConnectOptions::new(), None).await.unwrap();
///
/// } // client_identifier is dropped here, now we can reset the buffer
///
/// // Safety: client_identifier and all other previously returned values living in buffer's backing
/// // buffer don't exist anymore. No aliasing possible.
/// unsafe { client.buffer_mut().reset() };
///
/// // The next allocation can happen safely here.
/// client.poll().await.unwrap();
/// ```
///
/// ## Unsound
///
/// ```rust,ignore
/// use rust_mqtt::buffer::BumpBuffer;
/// use rust_mqtt::client::Client;
/// use rust_mqtt::client::info::ConnectInfo;
/// use rust_mqtt::client::options::ConnectOptions;
/// use tokio::net::TcpStream;
/// use embedded_io_adapters::tokio_1::FromTokio;
///
/// let mut buffer = [0; 1024];
/// let mut buffer = BumpBuffer::new(&mut buffer);
/// let mut client: Client<'_, FromTokio<TcpStream>, _, 0, 1, 0, 0> = Client::new(&mut buffer);
///
/// // client_identifier lives inside buffer's backing buffer, so it prevents a reset call.
/// let ConnectInfo { client_identifier, .. } = client.connect(todo!(), &ConnectOptions::new(), None).await.unwrap();
///
/// // (No) Safety: client_identifier still lives.
/// unsafe { client.buffer_mut().reset() };
///
/// // The next allocation can happen here and cause an alias to client_identifier.
/// client.poll().await.unwrap();
///
/// // client_identifier is still alive. It might have a different or even non-UTF-8 value now, scary...
/// println!("{:?}", client_identifier);
/// ```
#[inline]
pub unsafe fn reset(&mut self) {
self.index = 0;
}
}
fn _assert_covariant<'a, 'b: 'a>(x: BumpBuffer<'b>) -> BumpBuffer<'a> {
x
}
#[cfg(test)]
mod unit {
use tokio_test::{assert_err, assert_ok};
use super::*;
#[test]
fn provide_buffer_and_remaining_len() {
let mut backing = [0; 10];
{
let mut buf = BumpBuffer::new(&mut backing);
assert_eq!(buf.remaining_len(), 10);
let s1 = assert_ok!(buf.provide_buffer(4));
assert_eq!(s1.len(), 4);
s1.copy_from_slice(&[1, 2, 3, 4]);
assert_eq!(buf.remaining_len(), 6);
// take remaining 6 bytes
let s2 = assert_ok!(buf.provide_buffer(6));
assert_eq!(s2.len(), 6);
s2.copy_from_slice(&[5, 6, 7, 8, 9, 10]);
assert_eq!(buf.remaining_len(), 0);
assert_eq!(s1, [1, 2, 3, 4]);
assert_eq!(s2, [5, 6, 7, 8, 9, 10]);
let err = assert_err!(buf.provide_buffer(1));
assert_eq!(err, InsufficientSpace);
}
assert_eq!(backing, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}
#[test]
fn reset_allows_reuse() {
let mut backing = [0; 6];
{
let mut buf = BumpBuffer::new(&mut backing);
let s1 = {
let s1 = assert_ok!(buf.provide_buffer(3));
s1.copy_from_slice(&[11, 12, 13]);
s1.as_ptr()
};
// reset and take again from start
unsafe { buf.reset() }
let s2 = assert_ok!(buf.provide_buffer(3));
// Checking the slices for equality is UB because we have not upheld the rules of
// `BumpBuffer::reset` and it subsequently causes aliasing.
// assert_eq!(s1, s2);
assert_eq!(s1, s2.as_ptr());
}
assert_eq!(backing, [11, 12, 13, 0, 0, 0]);
}
}
}
#[cfg(feature = "alloc")]
mod alloc {
use alloc::{boxed::Box, vec};
use core::convert::Infallible;
use crate::buffer::BufferProvider;
/// Allocates memory using the global allocator.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AllocBuffer;
impl<'a> BufferProvider<'a> for AllocBuffer {
type Buffer = Box<[u8]>;
type ProvisionError = Infallible;
/// Allocates `len` bytes on the heap
fn provide_buffer(&mut self, len: usize) -> Result<Self::Buffer, Self::ProvisionError> {
let buffer = vec![0; len].into_boxed_slice();
Ok(buffer)
}
}
#[cfg(test)]
mod unit {
use tokio_test::assert_ok;
use crate::buffer::{BufferProvider, alloc::AllocBuffer};
#[test]
fn provide_buffer() {
let mut alloc = AllocBuffer;
let buffer = alloc.provide_buffer(10);
let buffer = assert_ok!(buffer);
assert_eq!(10, buffer.len());
}
}
}