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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#[cfg(feature = "alloc")]
use crate::alloc;
use core::{
alloc::Allocator,
fmt,
marker::Unsize,
ptr::{self, Pointee},
};
use crate::{
node::{self, Header},
AllocateError, DynList, Ends, MaybeUninitNode,
};
use super::{super::node::Node, Cursor};
/// A mutable cursor over a [`DynList`].
///
/// Cursors point to an element in the list. There is an extra "ghost" element between the front and the back, making it circular.
pub struct CursorMut<
'a,
U: ?Sized,
#[cfg(feature = "alloc")] A = alloc::Global,
#[cfg(not(feature = "alloc"))] A,
> where
A: Allocator,
{
pub(crate) current: Option<Node<U>>,
pub(crate) list: &'a mut DynList<U, A>,
}
impl<U, A> CursorMut<'_, U, A>
where
U: ?Sized,
A: Allocator,
{
#[must_use]
#[inline]
/// Gets an immutable cursor over the list.
pub fn as_cursor(&self) -> Cursor<'_, U, A> {
Cursor {
current: self.current,
list: self.list,
}
}
/// Moves the cursor to the next element.
///
/// If the cursor is on the "ghost" element, this moves to the front of the list.
/// If the cursor is at the back of the list, this moves to the "ghost" element.
pub fn move_next(&mut self) {
self.current = match self.current {
None => self.list.ends.map(|Ends { front, .. }| front),
// SAFETY:
// As the node is in the list, it's header must be properly initialised.
Some(node) => unsafe { node.header_ptr().as_ref() }.next,
}
}
/// Moves the cursor to the previous element.
///
/// If the cursor is on the "ghost" element, this moves to the back of the list.
/// If the cursor is at the front of the list, this moves to the "ghost" element.
pub fn move_previous(&mut self) {
self.current = match self.current {
None => self.list.ends.map(|Ends { back, .. }| back),
// SAFETY:
// As the node is in the list, it's header must be properly initialised.
Some(node) => unsafe { node.header_ptr().as_ref() }.previous,
}
}
#[must_use]
/// Gets a mutable reference to the current element.
///
/// If the cursor is pointing to the "ghost" element, this returns [`None`].
pub fn current(&mut self) -> Option<&mut U> {
self.current.map(|node| {
// SAFETY:
// As the node is in the list, its metadata must be properly initialised.
let mut ptr = unsafe { node.data_ptr() };
// SAFETY:
// As the node is in the list, its value must be properly initialised.
unsafe { ptr.as_mut() }
})
}
#[must_use]
#[inline]
/// Returns a reference to the underlying list.
pub const fn as_list(&self) -> &DynList<U, A> {
self.list
}
/// Attempts to allocate an uninitialised node before the current node.
///
/// If the cursor is on the "ghost" element, this will allocate the node at the back of the list.
///
/// # Safety
/// The `metadata` must be valid under the safety conditions for [`Layout::for_value_raw`](core::alloc::Layout::for_value_raw).
///
/// # Errors
/// If allocation fails, this will return an [`AllocateError`].
pub unsafe fn try_allocate_uninit_before(
&mut self,
metadata: <U as Pointee>::Metadata,
) -> Result<MaybeUninitNode<U, A>, AllocateError> {
let (next, previous) = self.current.map_or_else(
|| (None, self.list.ends.map(|Ends { back, .. }| back)),
|current| {
// SAFETY:
// As the node is in the list, it's header must be properly initialised.
let header = unsafe { current.header_ptr().as_ref() };
(Some(current), header.previous)
},
);
unsafe { node::try_new(&mut *self.list, metadata, Header { next, previous }) }
}
/// Attempts to allocate an uninitialised node after the current node.
///
/// If the cursor is on the "ghost" element, this will allocate the node at the front of the list.
///
/// # Safety
/// The `metadata` must be valid under the safety conditions for [`Layout::for_value_raw`](core::alloc::Layout::for_value_raw).
///
/// # Errors
/// If allocation fails, this will return an [`AllocateError`].
pub unsafe fn try_allocate_uninit_after(
&mut self,
metadata: <U as Pointee>::Metadata,
) -> Result<MaybeUninitNode<U, A>, AllocateError> {
let (next, previous) = self.current.map_or_else(
|| (self.list.ends.map(|Ends { front, .. }| front), None),
|current| {
let header = unsafe { current.header_ptr().as_ref() };
(header.next, Some(current))
},
);
unsafe { node::try_new(&mut *self.list, metadata, Header { next, previous }) }
}
/// Allocates an uninitialised node before the current node.
///
/// If the cursor is on the "ghost" element, this will allocate the node at the back of the list.
///
/// # Safety
/// The `metadata` must be valid under the safety conditions for [`Layout::for_value_raw`](core::alloc::Layout::for_value_raw).
#[must_use]
pub unsafe fn allocate_uninit_before(
&mut self,
metadata: <U as Pointee>::Metadata,
) -> MaybeUninitNode<U, A> {
AllocateError::unwrap_result(unsafe { self.try_allocate_uninit_before(metadata) })
}
/// Allocates an uninitialised node after the current node.
///
/// If the cursor is on the "ghost" element, this will allocate the node at the front of the list.
///
/// # Safety
/// The `metadata` must be valid under the safety conditions for [`Layout::for_value_raw`](core::alloc::Layout::for_value_raw).
#[must_use]
pub unsafe fn allocate_uninit_after(
&mut self,
metadata: <U as Pointee>::Metadata,
) -> MaybeUninitNode<U, A> {
AllocateError::unwrap_result(unsafe { self.try_allocate_uninit_after(metadata) })
}
/// Attempts to insert `value` before the current node and unsize it to `U`.
///
/// # Errors
/// If allocation fails, this will return an [`AllocateError`].
pub fn try_insert_before_unsize<T>(&mut self, value: T) -> Result<(), AllocateError<T>>
where
T: Unsize<U>,
{
let metadata = ptr::metadata(&value as &U);
let node = match unsafe { self.try_allocate_uninit_before(metadata) } {
Ok(node) => node,
Err(error) => return Err(error.with_value(value)),
};
unsafe { node.value_ptr().cast().write(value) };
unsafe { node.insert() };
Ok(())
}
/// Attempts to insert `value` after the current node and unsize it to `U`.
///
/// # Errors
/// If allocation fails, this will return an [`AllocateError`].
pub fn try_insert_after_unsize<T>(&mut self, value: T) -> Result<(), AllocateError<T>>
where
T: Unsize<U>,
{
let metadata = ptr::metadata(&value as &U);
let node = match unsafe { self.try_allocate_uninit_before(metadata) } {
Ok(node) => node,
Err(error) => return Err(error.with_value(value)),
};
unsafe { node.value_ptr().cast().write(value) };
unsafe { node.insert() };
Ok(())
}
/// Inserts `value` before the current node and unsizes it to `U`.
pub fn insert_before_unsize<T>(&mut self, value: T)
where
T: Unsize<U>,
{
let metadata = ptr::metadata(&value as &U);
let node = unsafe { self.allocate_uninit_before(metadata) };
unsafe { node.value_ptr().cast().write(value) };
unsafe { node.insert() };
}
/// Inserts `value` after the current node and unsizes it to `U`.
pub fn insert_after_unsize<T>(&mut self, value: T)
where
T: Unsize<U>,
{
let metadata = ptr::metadata(&value as &U);
let node = unsafe { self.allocate_uninit_after(metadata) };
unsafe { node.value_ptr().cast().write(value) };
unsafe { node.insert() };
}
#[must_use]
/// Removes the current node and moves to the previous.
///
/// If the cursor is pointing to the "ghost" element, this returns [`None`].
pub fn remove_current_node(&mut self) -> Option<MaybeUninitNode<U, A>> {
let node = self.current?;
let header = unsafe { node.header_ptr().as_ref() };
debug_assert!(self.list.ends.is_some());
let Ends { front, back } = unsafe { self.list.ends.as_mut().unwrap_unchecked() };
if let Some(next) = header.next {
let next_header = unsafe { next.header_ptr().as_mut() };
debug_assert_eq!(next_header.previous, Some(node));
next_header.previous = header.previous;
}
if let Some(previous) = header.previous {
let previous_header = unsafe { previous.header_ptr().as_mut() };
debug_assert_eq!(previous_header.next, Some(node));
previous_header.next = header.next;
}
self.current = header.previous;
match (header.next, header.previous) {
(Some(_next), Some(_previous)) => {}
(None, Some(previous)) => {
debug_assert_eq!(*back, node);
*back = previous;
}
(Some(next), None) => {
debug_assert_eq!(*front, node);
*front = next;
}
(None, None) => {
self.list.ends = None;
}
}
Some(unsafe { dynode::new_maybe_uninit(&mut *self.list, node.into()) })
}
#[inline]
/// Deletes and drops the current node.
///
/// Returns [`true`] if a node was removed and [`false`] if current element is the "ghost".
pub fn delete_current(&mut self) -> bool {
self.remove_current_node()
.map(|mut node| unsafe { node.drop_in_place() })
.is_some()
}
#[cfg(feature = "alloc")]
#[must_use]
#[inline]
/// Attempts to remove the current node and return its value in a [`Box`].
///
/// If the cursor is pointing to the "ghost" element, this returns [`None`].
///
/// # Errors
/// If allocation fails, this will return an [`AllocateError`].
/// The node will not be removed.
pub fn try_remove_current_boxed(&mut self) -> Option<Result<alloc::Box<U, A>, AllocateError>>
where
A: Clone,
{
self.remove_current_node().map(|node| {
unsafe { node.try_take_boxed() }.map_err(|error| {
let (node, error) = error.into_parts();
unsafe { node.insert() };
error
})
})
}
#[cfg(feature = "alloc")]
#[must_use]
#[inline]
/// Removes the current node and returns its value in a [`Box`].
///
/// If the cursor is pointing to the "ghost" element, this returns [`None`].
pub fn remove_current_boxed(&mut self) -> Option<alloc::Box<U, A>>
where
A: Clone,
{
self.try_remove_current_boxed()
.map(AllocateError::unwrap_result)
}
}
unsafe impl<U, A> Send for CursorMut<'_, U, A>
where
U: ?Sized + Send,
A: Allocator + Send,
{
}
unsafe impl<U, A> Sync for CursorMut<'_, U, A>
where
U: ?Sized + Sync,
A: Allocator + Sync,
{
}
impl<U, A> fmt::Debug for CursorMut<'_, U, A>
where
U: ?Sized + fmt::Debug,
A: Allocator,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("CursorMut").field(self).finish()
}
}