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
use std::mem::size_of;
use std::marker::PhantomData;
#[derive(Debug)]
pub struct SliceCopyIter<'a, T: 'a> {
ptr: *const T,
end: *const T,
ty: PhantomData<&'a T>,
}
impl<'a, T> Copy for SliceCopyIter<'a, T> { }
impl<'a, T> Clone for SliceCopyIter<'a, T> {
fn clone(&self) -> Self { *self }
}
impl<'a, T> SliceCopyIter<'a, T>
where T: Copy
{
#[inline]
pub unsafe fn new(ptr: *const T, end: *const T) -> Self {
assert!(size_of::<T>() != 0);
SliceCopyIter {
ptr: ptr,
end: end,
ty: PhantomData,
}
}
pub fn into_raw(self) -> (*const T, *const T) {
(self.ptr, self.end)
}
}
impl<'a, T> Iterator for SliceCopyIter<'a, T>
where T: Copy,
{
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.ptr != self.end {
unsafe {
let elt = Some(*self.ptr);
self.ptr = self.ptr.offset(1);
elt
}
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = (self.end as usize - self.ptr as usize) / size_of::<T>();
(len, Some(len))
}
fn count(self) -> usize {
self.len()
}
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<'a, T> DoubleEndedIterator for SliceCopyIter<'a, T>
where T: Copy
{
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
if self.ptr != self.end {
unsafe {
self.end = self.end.offset(-1);
let elt = Some(*self.end);
elt
}
} else {
None
}
}
}
impl<'a, T> ExactSizeIterator for SliceCopyIter<'a, T> where T: Copy { }
impl<'a, T> From<&'a [T]> for SliceCopyIter<'a, T>
where T: Copy
{
fn from(slice: &'a [T]) -> Self {
assert!(size_of::<T>() != 0);
unsafe {
let ptr = slice.as_ptr();
let end = ptr.offset(slice.len() as isize);
SliceCopyIter::new(ptr, end)
}
}
}
impl<'a, T> Default for SliceCopyIter<'a, T>
where T: Copy
{
fn default() -> Self {
unsafe {
SliceCopyIter::new(0x1 as *const T, 0x1 as *const T)
}
}
}