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
use core::alloc::Allocator;
use crate::{
node::{self, Header},
AllocateError, Ends, MaybeUninitNode,
};
use super::CursorMut;
impl<A> CursorMut<'_, str, A>
where
A: Allocator,
{
/// Attempts to allocate an uninitialised string 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_string_before(
&mut self,
length: usize,
) -> Result<MaybeUninitNode<str, 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_string(&mut *self.list, length, Header { next, previous })
}
/// Attempts to allocate an uninitialised string 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_string_after(
&mut self,
length: usize,
) -> Result<MaybeUninitNode<str, 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_string(&mut *self.list, length, Header { next, previous })
}
#[must_use]
/// Allocates an uninitialised string 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_string_before(&mut self, length: usize) -> MaybeUninitNode<str, A> {
AllocateError::unwrap_result(self.try_allocate_uninit_string_before(length))
}
#[must_use]
/// Allocates an uninitialised string 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_string_after(&mut self, length: usize) -> MaybeUninitNode<str, A> {
AllocateError::unwrap_result(self.try_allocate_uninit_string_after(length))
}
/// Attempts to copy the string slice `src` and insert it before the current node.
///
/// # Errors
/// If allocation fails, this will return an [`AllocateError`].
pub fn try_insert_copy_str_before(&mut self, src: &str) -> Result<(), AllocateError> {
let mut node = self.try_allocate_uninit_string_before(src.len())?;
node.copy_from_str(src);
unsafe { node.insert() };
Ok(())
}
/// Attempts to copy the string slice `src` and insert it after the current node.
///
/// # Errors
/// If allocation fails, this will return an [`AllocateError`].
pub fn try_insert_copy_str_after(&mut self, src: &str) -> Result<(), AllocateError> {
let mut node = self.try_allocate_uninit_string_after(src.len())?;
node.copy_from_str(src);
unsafe { node.insert() };
Ok(())
}
/// Copies the string slice `src` and inserts it before the current node.
pub fn insert_copy_str_before(&mut self, src: &str) {
let mut node = self.allocate_uninit_string_before(src.len());
node.copy_from_str(src);
unsafe { node.insert() };
}
/// Copies the string slice `src` and inserts it after the current node.
pub fn insert_copy_str_after(&mut self, src: &str) {
let mut node = self.allocate_uninit_string_after(src.len());
node.copy_from_str(src);
unsafe { node.insert() };
}
}