FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
arrayvec/src/raw.rs at arrayvec-copy-2 · Simteract/arrayvec · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Simteract
/
arrayvec
Public
forked from
bluss/arrayvec
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
arrayvec
/
src
/
raw.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
319 lines (284 loc) · 8.72 KB
Breadcrumbs
arrayvec
/
src
/
raw.rs
Copy path
File metadata and controls
319 lines (284 loc) · 8.72 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
use
CapacityError
;
use
RangeArgument
;
use
array
::
Array
;
use
std
::
cmp
;
use
std
::
ops
::
{
DerefMut
}
;
use
std
::
ptr
;
use
std
::
slice
;
#
[
cfg
(
feature=
"std"
)
]
use
std
::
io
;
use
array
::
Index
;
pub
trait
RawArrayVec
<
A
:
Array
>
:
DerefMut
<
Target
=
[
A
::
Item
]
>
{
fn
len
(
&
self
)
->
usize
;
fn
capacity
(
&
self
)
->
usize
;
fn
is_full_impl
(
&
self
)
->
bool
{
self
.
len
(
)
==
self
.
capacity
(
)
}
unsafe
fn
set_len
(
&
mut
self
,
length
:
usize
)
;
fn
len_ref
(
&
mut
self
)
->
&
mut
A
::
Index
;
fn
push_impl
(
&
mut
self
,
element
:
A
::
Item
)
->
Result
<
(
)
,
CapacityError
<
A
::
Item
>
>
{
if
self
.
len
(
)
<
A
::
capacity
(
)
{
let
len =
self
.
len
(
)
;
unsafe
{
ptr
::
write
(
self
.
get_unchecked_mut
(
len
)
,
element
)
;
self
.
set_len
(
len +
1
)
;
}
Ok
(
(
)
)
}
else
{
Err
(
CapacityError
::
new
(
element
)
)
}
}
fn
insert_impl
(
&
mut
self
,
index
:
usize
,
element
:
A
::
Item
)
->
Result
<
(
)
,
CapacityError
<
A
::
Item
>
>
{
assert
!
(
index <=
self
.
len
(
)
)
;
if
index ==
self
.
capacity
(
)
{
return
Err
(
CapacityError
::
new
(
element
)
)
;
}
let
ret =
if
self
.
len
(
)
==
self
.
capacity
(
)
{
Err
(
CapacityError
::
new
(
self
.
pop_impl
(
)
.
unwrap
(
)
)
)
}
else
{
Ok
(
(
)
)
}
;
let
len =
self
.
len
(
)
;
// follows is just like Vec<T>
unsafe
{
// infallible
// The spot to put the new value
{
let
p =
self
.
get_unchecked_mut
(
index
)
as
*
mut
_
;
// Shift everything over to make space. (Duplicating the
// `index`th element into two consecutive places.)
ptr
::
copy
(
p
,
p
.
offset
(
1
)
,
len - index
)
;
// Write it in, overwriting the first copy of the `index`th
// element.
ptr
::
write
(
p
,
element
)
;
}
self
.
set_len
(
len +
1
)
;
}
ret
}
fn
pop_impl
(
&
mut
self
)
->
Option
<
A
::
Item
>
{
if
self
.
len
(
)
==
0
{
return
None
}
unsafe
{
let
new_len =
self
.
len
(
)
-
1
;
self
.
set_len
(
new_len
)
;
Some
(
ptr
::
read
(
self
.
get_unchecked_mut
(
new_len
)
)
)
}
}
fn
swap_remove_impl
(
&
mut
self
,
index
:
usize
)
->
Option
<
A
::
Item
>
{
let
len =
self
.
len
(
)
;
if
index >= len
{
return
None
}
self
.
swap
(
index
,
len -
1
)
;
self
.
pop_impl
(
)
}
fn
remove_impl
(
&
mut
self
,
index
:
usize
)
->
Option
<
A
::
Item
>
{
if
index >=
self
.
len
(
)
{
None
}
else
{
self
.
drain_impl
(
index..index +
1
)
.
next
(
)
}
}
fn
clear_impl
(
&
mut
self
)
{
while
let
Some
(
_
)
=
self
.
pop_impl
(
)
{
}
}
fn
retain_impl
<
F
>
(
&
mut
self
,
mut
f
:
F
)
where
F
:
FnMut
(
&
mut
A
::
Item
)
->
bool
{
let
len =
self
.
len
(
)
;
let
mut
del =
0
;
{
let
v =
&
mut
*
*
self
;
for
i
in
0
..len
{
if
!
f
(
&
mut
v
[
i
]
)
{
del +=
1
;
}
else
if
del >
0
{
v
.
swap
(
i - del
,
i
)
;
}
}
}
if
del >
0
{
self
.
drain_impl
(
len - del..
)
;
}
}
fn
extend_impl
<
T
:
IntoIterator
<
Item
=
A
::
Item
>
>
(
&
mut
self
,
iter
:
T
)
{
let
take =
self
.
capacity
(
)
-
self
.
len
(
)
;
for
elt
in
iter
.
into_iter
(
)
.
take
(
take
)
{
let
_ =
self
.
push_impl
(
elt
)
;
}
}
#
[
cfg
(
feature=
"std"
)
]
fn
write_impl
(
&
mut
self
,
data
:
&
[
u8
]
)
-> io
::
Result
<
usize
>
where
A
:
Array
<
Item
=
u8
>
{
use
std
::
io
::
Write
;
unsafe
{
let
len =
self
.
len
(
)
;
let
mut
tail = slice
::
from_raw_parts_mut
(
self
.
get_unchecked_mut
(
len
)
,
A
::
capacity
(
)
- len
)
;
let
result = tail
.
write
(
data
)
;
if
let
Ok
(
written
)
= result
{
self
.
set_len
(
len + written
)
;
}
result
}
}
fn
drain_impl
<
R
:
RangeArgument
>
(
&
mut
self
,
range
:
R
)
->
Drain
<
A
>
{
// Memory safety
//
// When the Drain is first created, it shortens the length of
// the source vector to make sure no uninitalized or moved-from elements
// are accessible at all if the Drain's destructor never gets to run.
//
// Drain will ptr::read out the values to remove.
// When finished, remaining tail of the vec is copied back to cover
// the hole, and the vector length is restored to the new length.
//
let
len =
self
.
len
(
)
;
let
start = range
.
start
(
)
.
unwrap_or
(
0
)
;
let
end = range
.
end
(
)
.
unwrap_or
(
len
)
;
// bounds check happens here
let
range_slice
:
*
const
_
=
&
self
[
start..end
]
;
unsafe
{
// set self.vec length's to start, to be safe in case Drain is leaked
self
.
set_len
(
start
)
;
Drain
{
tail_start
:
end
,
tail_len
:
len - end
,
iter
:
(
*
range_slice
)
.
iter
(
)
,
source_ptr
:
self
.
as_mut_ptr
(
)
,
source_len
:
self
.
len_ref
(
)
as
*
mut
_
}
}
}
fn
clone_from_impl
(
&
mut
self
,
rhs
:
&
Self
)
where
A
::
Item
:
Clone
{
// recursive case for the common prefix
let
prefix = cmp
::
min
(
self
.
len
(
)
,
rhs
.
len
(
)
)
;
{
let
a =
&
mut
self
[
..prefix
]
;
let
b =
&
rhs
[
..prefix
]
;
for
i
in
0
..prefix
{
a
[
i
]
.
clone_from
(
&
b
[
i
]
)
;
}
}
if
prefix <
self
.
len
(
)
{
// rhs was shorter
for
_
in
0
..
self
.
len
(
)
- prefix
{
self
.
pop_impl
(
)
;
}
}
else
{
for
elt
in
&
rhs
[
self
.
len
(
)
..
]
{
let
_ =
self
.
push_impl
(
elt
.
clone
(
)
)
;
}
}
}
}
/*
impl<A: Array> Deref for RawArrayVec<A> {
type Target = [A::Item];
#[inline]
fn deref(&self) -> &[A::Item] {
unsafe {
slice::from_raw_parts(self.xs.as_ptr(), self.len())
}
}
}
impl<A: Array> DerefMut for RawArrayVec<A> {
#[inline]
fn deref_mut(&mut self) -> &mut [A::Item] {
let len = self.len();
unsafe {
slice::from_raw_parts_mut(self.xs.as_mut_ptr(), len)
}
}
}
impl<A: Array> From<A> for RawArrayVec<A> {
fn from(array: A) -> Self {
RawArrayVec {
xs: array,
len: Index::from(A::capacity()),
}
}
}
impl<'a, A: Array> IntoIterator for &'a RawArrayVec<A> {
type Item = &'a A::Item;
type IntoIter = slice::Iter<'a, A::Item>;
fn into_iter(self) -> Self::IntoIter { self.iter() }
}
impl<'a, A: Array> IntoIterator for &'a mut RawArrayVec<A> {
type Item = &'a mut A::Item;
type IntoIter = slice::IterMut<'a, A::Item>;
fn into_iter(self) -> Self::IntoIter { self.iter_mut() }
}
*/
/// A draining iterator for `ArrayVec`.
pub
struct
Drain
<
'
a
,
A
>
where
A
:
Array
,
A
::
Item
:
'
a
,
{
/// Index of tail to preserve
tail_start
:
usize
,
/// Length of tail
tail_len
:
usize
,
/// Current remaining range to remove
iter
:
slice
::
Iter
<
'
a
,
A
::
Item
>
,
source_ptr
:
*
mut
A
::
Item
,
source_len
:
*
mut
A
::
Index
,
}
unsafe
impl
<
'
a
,
A
:
Array
+
Sync
>
Sync
for
Drain
<
'
a
,
A
>
{
}
unsafe
impl
<
'
a
,
A
:
Array
+
Send
>
Send
for
Drain
<
'
a
,
A
>
{
}
impl
<
'
a
,
A
:
Array
>
Iterator
for
Drain
<
'
a
,
A
>
where
A
::
Item
:
'
a
,
{
type
Item
=
A
::
Item
;
#
[
inline
]
fn
next
(
&
mut
self
)
->
Option
<
Self
::
Item
>
{
self
.
iter
.
next
(
)
.
map
(
|elt|
unsafe
{
ptr
::
read
(
elt
as
*
const
_
)
}
)
}
#
[
inline
]
fn
size_hint
(
&
self
)
->
(
usize
,
Option
<
usize
>
)
{
self
.
iter
.
size_hint
(
)
}
}
impl
<
'
a
,
A
:
Array
>
DoubleEndedIterator
for
Drain
<
'
a
,
A
>
where
A
::
Item
:
'
a
,
{
#
[
inline
]
fn
next_back
(
&
mut
self
)
->
Option
<
Self
::
Item
>
{
self
.
iter
.
next_back
(
)
.
map
(
|elt|
unsafe
{
ptr
::
read
(
elt
as
*
const
_
)
}
)
}
}
impl
<
'
a
,
A
:
Array
>
ExactSizeIterator
for
Drain
<
'
a
,
A
>
where
A
::
Item
:
'
a
{
}
impl
<
'
a
,
A
:
Array
>
Drop
for
Drain
<
'
a
,
A
>
where
A
::
Item
:
'
a
{
fn
drop
(
&
mut
self
)
{
// len is currently 0 so panicking while dropping will not cause a double drop.
// exhaust self first
while
let
Some
(
_
)
=
self
.
next
(
)
{
}
if
self
.
tail_len
>
0
{
unsafe
{
let
ptr =
self
.
source_ptr
;
let
source_len =
&
mut
*
self
.
source_len
;
// memmove back untouched tail, update to new length
let
start = source_len
.
to_usize
(
)
;
let
tail =
self
.
tail_start
;
let
src = ptr
.
offset
(
tail
as
isize
)
;
let
dst = ptr
.
offset
(
start
as
isize
)
;
ptr
::
copy
(
src
,
dst
,
self
.
tail_len
)
;
*
source_len =
Index
::
from
(
start +
self
.
tail_len
)
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL