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
use core::alloc::Allocator;
use crate::{
node::{self, Header},
AllocateError, Ends, MaybeUninitNode,
};
use super::CursorMut;
impl<T, A> CursorMut<'_, [T], A>
where
A: Allocator,
{
/// Attempts to allocate an uninitialised array node before the current node.
///
/// If the cursor is on the "ghost" element, this will allocate the node at the back of the list.
///
/// # Errors
/// If allocation fails, or an arithmetic overflow occours in [`Layout::array`](core::alloc::Layout::array), this will return an [`AllocateError`].
pub fn try_allocate_uninit_array_before(
&mut self,
length: usize,
) -> Result<MaybeUninitNode<[T], A>, AllocateError> {
let (next, previous) = self.current.map_or_else(
|| (None, self.list.ends.map(|Ends { back, .. }| back)),
|current| {
let header = unsafe { current.header_ptr().as_ref() };
(Some(current), header.previous)
},
);
node::try_new_array(&mut *self.list, length, Header { next, previous })
}
/// Attempts to allocate an uninitialised array node after the current node.
///
/// If the cursor is on the "ghost" element, this will allocate the node at the front of the list.
///
/// # Errors
/// If allocation fails, or an arithmetic overflow occours in [`Layout::array`](core::alloc::Layout::array), this will return an [`AllocateError`].
pub fn try_allocate_uninit_array_after(
&mut self,
length: usize,
) -> Result<MaybeUninitNode<[T], 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))
},
);
node::try_new_array(&mut *self.list, length, Header { next, previous })
}
#[must_use]
/// Allocates an uninitialised array node before the current node.
///
/// If the cursor is on the "ghost" element, this will allocate the node at the back of the list.
pub fn allocate_uninit_array_before(&mut self, length: usize) -> MaybeUninitNode<[T], A> {
AllocateError::unwrap_result(self.try_allocate_uninit_array_before(length))
}
#[must_use]
/// Allocates an uninitialised array node after the current node.
///
/// If the cursor is on the "ghost" element, this will allocate the node at the front of the list.
pub fn allocate_uninit_array_after(&mut self, length: usize) -> MaybeUninitNode<[T], A> {
AllocateError::unwrap_result(self.try_allocate_uninit_array_after(length))
}
/// Attempts to copy the slice `src` and insert it before the current node.
///
/// # Errors
/// If allocation fails, this will return an [`AllocateError`].
pub fn try_insert_copy_slice_before(&mut self, src: &[T]) -> Result<(), AllocateError>
where
T: Copy,
{
let mut node = self.try_allocate_uninit_array_before(src.len())?;
node.copy_from_slice(src);
unsafe { node.insert() };
Ok(())
}
/// Attempts to copy the slice `src` and insert it after the current node.
///
/// # Errors
/// If allocation fails, this will return an [`AllocateError`].
pub fn try_insert_copy_slice_after(&mut self, src: &[T]) -> Result<(), AllocateError>
where
T: Copy,
{
let mut node = self.try_allocate_uninit_array_after(src.len())?;
node.copy_from_slice(src);
unsafe { node.insert() };
Ok(())
}
/// Copies the slice `src` and inserts it before the current node.
pub fn insert_copy_slice_before(&mut self, src: &[T])
where
T: Copy,
{
let mut node =
AllocateError::unwrap_result(self.try_allocate_uninit_array_before(src.len()));
node.copy_from_slice(src);
unsafe { node.insert() };
}
/// Copies the slice `src` and inserts it after the current node.
pub fn insert_copy_slice_after(&mut self, src: &[T])
where
T: Copy,
{
let mut node =
AllocateError::unwrap_result(self.try_allocate_uninit_array_after(src.len()));
node.copy_from_slice(src);
unsafe { node.insert() };
}
/// Attempts to clone the slice `src` and insert it before the current node.
///
/// # Errors
/// If allocation fails, this will return an [`AllocateError`].
pub fn try_insert_clone_slice_before(&mut self, src: &[T]) -> Result<(), AllocateError>
where
T: Clone,
{
let mut node = self.try_allocate_uninit_array_before(src.len())?;
node.clone_from_slice(src);
unsafe { node.insert() };
Ok(())
}
/// Attempts to clone the slice `src` and insert it after the current node.
///
/// # Errors
/// If allocation fails, this will return an [`AllocateError`].
pub fn try_insert_clone_slice_after(&mut self, src: &[T]) -> Result<(), AllocateError>
where
T: Clone,
{
let mut node = self.try_allocate_uninit_array_after(src.len())?;
node.clone_from_slice(src);
unsafe { node.insert() };
Ok(())
}
/// Clones the slice `src` and inserts it before the current node.
pub fn insert_clone_slice_before(&mut self, src: &[T])
where
T: Clone,
{
let mut node = self.allocate_uninit_array_before(src.len());
node.clone_from_slice(src);
unsafe { node.insert() };
}
/// Clones the slice `src` and inserts it after the current node.
pub fn insert_clone_slice_after(&mut self, src: &[T])
where
T: Clone,
{
let mut node = self.allocate_uninit_array_after(src.len());
node.clone_from_slice(src);
unsafe { node.insert() };
}
}