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
use ::core::{fmt, ptr::copy_nonoverlapping};
use ::mischief::{In, Slot};
use ::munge::munge;
use ::ptr_meta::Pointee;
use ::rel_core::{Basis, DefaultBasis, Emplace, EmplaceExt, Move, Portable};
use ::situ::{
alloc::RawRegionalAllocator,
fmt::{DebugRaw, DisplayRaw},
ops::{DerefMutRaw, DerefRaw},
str::{from_raw_utf8_unchecked, from_raw_utf8_unchecked_mut},
DropRaw,
Mut,
Ref,
};
use crate::{alloc::RelAllocator, vec, RelVec};
#[derive(DropRaw, Move, Portable)]
#[repr(C)]
pub struct RelString<A: RawRegionalAllocator, B: Basis = DefaultBasis> {
vec: RelVec<u8, A, B>,
}
impl<A: RawRegionalAllocator, B: Basis> RelString<A, B> {
#[inline]
pub fn allocator(this: Ref<'_, Self>) -> Ref<'_, A> {
munge!(let RelString { vec } = this);
RelVec::allocator(vec)
}
#[inline]
pub fn as_bytes(this: Ref<'_, Self>) -> Ref<'_, [u8]> {
munge!(let RelString { vec } = this);
DerefRaw::deref_raw(vec)
}
#[inline]
pub fn as_str(this: Ref<'_, Self>) -> Ref<'_, str> {
unsafe { from_raw_utf8_unchecked(Self::as_bytes(this)) }
}
#[inline]
pub unsafe fn as_mut_vec(this: Mut<'_, Self>) -> Mut<'_, RelVec<u8, A, B>> {
munge!(let RelString { vec } = this);
vec
}
#[inline]
pub fn as_mut_str(this: Mut<'_, Self>) -> Mut<'_, str> {
let vec = unsafe { Self::as_mut_vec(this) };
let bytes = DerefMutRaw::deref_mut_raw(vec);
unsafe { from_raw_utf8_unchecked_mut(bytes) }
}
#[inline]
pub fn capacity(&self) -> usize {
self.vec.capacity()
}
#[inline]
pub fn clear(this: Mut<'_, Self>) {
munge!(let RelString { vec } = this);
RelVec::clear(vec)
}
#[inline]
pub fn len(&self) -> usize {
self.vec.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.vec.is_empty()
}
}
impl<A: RawRegionalAllocator, B: Basis> DerefRaw for RelString<A, B> {
type Target = str;
fn deref_raw(this: Ref<'_, Self>) -> Ref<'_, Self::Target> {
Self::as_str(this)
}
}
pub struct Clone<'a, R>(pub R, pub &'a str);
unsafe impl<A, B, R> Emplace<RelString<A, B>, R::Region> for Clone<'_, R>
where
A: DropRaw + RawRegionalAllocator<Region = R::Region>,
B: Basis,
R: RelAllocator<A>,
{
fn emplaced_meta(&self) -> <RelString<A, B> as Pointee>::Metadata {}
unsafe fn emplace_unsized_unchecked(
self,
out: In<Slot<'_, RelString<A, B>>, A::Region>,
) {
let len = self.1.len();
munge!(let RelString { vec: out_vec } = out);
let mut vec =
In::into_inner(vec::WithCapacity(self.0, len).emplace_mut(out_vec));
unsafe {
copy_nonoverlapping(
self.1.as_ptr(),
RelVec::as_mut_ptr(vec.as_mut()),
len,
);
}
unsafe {
RelVec::set_len(vec, len);
}
}
}
impl<A: RawRegionalAllocator, B: Basis> DebugRaw for RelString<A, B> {
fn fmt_raw(
this: Ref<'_, Self>,
f: &mut fmt::Formatter<'_>,
) -> Result<(), fmt::Error> {
fmt::Debug::fmt(&*Self::as_str(this), f)
}
}
impl<A: RawRegionalAllocator, B: Basis> DisplayRaw for RelString<A, B> {
fn fmt_raw(
this: Ref<'_, Self>,
f: &mut fmt::Formatter<'_>,
) -> Result<(), fmt::Error> {
fmt::Display::fmt(&*Self::as_str(this), f)
}
}