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
use crate::ops::{Begin, BeginMut, Decrement, End, EndMut, Increment, Indirection};
use crate::{CppBox, CppDeletable, MutPtr, MutRef, Ptr, Ref};
use std::os::raw::c_char;

/// `Iterator` and `DoubleEndedIterator` backed by C++ iterators.
///
/// This object is produced by `IntoIterator` implementations on  pointer types
/// (`&CppBox`, `&mut CppBox`, `Ptr`, `MutPtr`, `Ref`, `MutRef`). You can also use
/// `cpp_iter` function to construct it manually from two C++ iterator objects.
pub struct CppIterator<T1, T2>
where
    T1: CppDeletable,
    T2: CppDeletable,
{
    begin: CppBox<T1>,
    end: CppBox<T2>,
}

/// Constructs a Rust-style iterator from C++ iterators pointing to begin and end
/// of the collection.
///
/// ### Safety
///
/// `begin` and `end` must be valid. It's not possible to make any guarantees about safety, since
/// `CppIterator` will call arbitrary C++ library code when used.
pub unsafe fn cpp_iter<T1, T2>(begin: CppBox<T1>, end: CppBox<T2>) -> CppIterator<T1, T2>
where
    T1: CppDeletable,
    T2: CppDeletable,
{
    CppIterator { begin, end }
}

impl<T1, T2> Iterator for CppIterator<T1, T2>
where
    T1: CppDeletable + PartialEq<Ref<T2>> + Indirection + Increment,
    T2: CppDeletable,
{
    type Item = <T1 as Indirection>::Output;

    fn next(&mut self) -> Option<Self::Item> {
        unsafe {
            if self.begin == self.end.as_ref() {
                None
            } else {
                let inner = &mut *self.begin.as_mut_raw_ptr();
                let value = inner.indirection();
                let inner = &mut *self.begin.as_mut_raw_ptr();
                inner.inc();
                Some(value)
            }
        }
    }
}

impl<T1, T2> DoubleEndedIterator for CppIterator<T1, T2>
where
    T1: CppDeletable + PartialEq<Ref<T2>> + Indirection + Increment,
    T2: CppDeletable + Decrement + Indirection<Output = <T1 as Indirection>::Output>,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        unsafe {
            if self.begin == self.end.as_ref() {
                None
            } else {
                let inner = &mut *self.end.as_mut_raw_ptr();
                inner.dec();
                let inner = &mut *self.end.as_mut_raw_ptr();
                let value = inner.indirection();
                Some(value)
            }
        }
    }
}

impl<T, T1, T2> IntoIterator for Ptr<T>
where
    T: Begin<Output = CppBox<T1>> + End<Output = CppBox<T2>>,
    T1: CppDeletable + PartialEq<Ref<T2>> + Increment + Indirection,
    T2: CppDeletable,
{
    type Item = <T1 as Indirection>::Output;
    type IntoIter = CppIterator<T1, T2>;

    fn into_iter(self) -> Self::IntoIter {
        unsafe { cpp_iter(self.begin(), self.end()) }
    }
}

impl<T, T1, T2> IntoIterator for MutPtr<T>
where
    T: BeginMut<Output = CppBox<T1>> + EndMut<Output = CppBox<T2>>,
    T1: CppDeletable + PartialEq<Ref<T2>> + Indirection + Increment,
    T2: CppDeletable,
{
    type Item = <T1 as Indirection>::Output;
    type IntoIter = CppIterator<T1, T2>;

    fn into_iter(mut self) -> Self::IntoIter {
        unsafe { cpp_iter(self.begin_mut(), self.end_mut()) }
    }
}

impl<T, T1, T2> IntoIterator for Ref<T>
where
    T: Begin<Output = CppBox<T1>> + End<Output = CppBox<T2>>,
    T1: CppDeletable + PartialEq<Ref<T2>> + Indirection + Increment,
    T2: CppDeletable,
{
    type Item = <T1 as Indirection>::Output;
    type IntoIter = CppIterator<T1, T2>;

    fn into_iter(self) -> Self::IntoIter {
        unsafe { cpp_iter(self.begin(), self.end()) }
    }
}

impl<T, T1, T2> IntoIterator for MutRef<T>
where
    T: BeginMut<Output = CppBox<T1>> + EndMut<Output = CppBox<T2>>,
    T1: CppDeletable + PartialEq<Ref<T2>> + Indirection + Increment,
    T2: CppDeletable,
{
    type Item = <T1 as Indirection>::Output;
    type IntoIter = CppIterator<T1, T2>;

    fn into_iter(mut self) -> Self::IntoIter {
        unsafe { cpp_iter(self.begin_mut(), self.end_mut()) }
    }
}

impl<'a, T, T1, T2> IntoIterator for &'a CppBox<T>
where
    T: CppDeletable + Begin<Output = CppBox<T1>> + End<Output = CppBox<T2>>,
    T1: CppDeletable + PartialEq<Ref<T2>> + Indirection + Increment,
    T2: CppDeletable,
{
    type Item = <T1 as Indirection>::Output;
    type IntoIter = CppIterator<T1, T2>;

    fn into_iter(self) -> Self::IntoIter {
        unsafe { cpp_iter(self.begin(), self.end()) }
    }
}

impl<'a, T, T1, T2> IntoIterator for &'a mut CppBox<T>
where
    T: CppDeletable + BeginMut<Output = CppBox<T1>> + EndMut<Output = CppBox<T2>>,
    T1: CppDeletable + PartialEq<Ref<T2>> + Indirection + Increment,
    T2: CppDeletable,
{
    type Item = <T1 as Indirection>::Output;
    type IntoIter = CppIterator<T1, T2>;

    fn into_iter(self) -> Self::IntoIter {
        unsafe { cpp_iter(self.begin_mut(), self.end_mut()) }
    }
}

pub trait SliceAsBeginEnd {
    type Item;
    fn begin_ptr(&self) -> Ptr<Self::Item>;
    fn end_ptr(&self) -> Ptr<Self::Item>;
}

impl<'a, T> SliceAsBeginEnd for &'a [T] {
    type Item = T;
    fn begin_ptr(&self) -> Ptr<T> {
        unsafe { Ptr::from_raw(self.as_ptr()) }
    }
    fn end_ptr(&self) -> Ptr<T> {
        unsafe { Ptr::from_raw(self.as_ptr().add(self.len())) }
    }
}

impl<'a> SliceAsBeginEnd for &'a str {
    type Item = c_char;
    fn begin_ptr(&self) -> Ptr<c_char> {
        unsafe { Ptr::from_raw(self.as_ptr() as *const c_char) }
    }
    fn end_ptr(&self) -> Ptr<c_char> {
        unsafe { Ptr::from_raw(self.as_ptr().add(self.len()) as *const c_char) }
    }
}