mirror of
https://github.com/topjohnwu/cxx.git
synced 2025-02-23 09:30:45 +00:00
Merge pull request #516 from dtolnay/sliceiterator
Add iterator for rust::Slice
This commit is contained in:
commit
474f188828
@ -26,6 +26,25 @@ public:
|
||||
T *data() const noexcept;
|
||||
size_t size() const noexcept;
|
||||
size_t length() const noexcept;
|
||||
|
||||
class iterator;
|
||||
iterator begin() const noexcept;
|
||||
iterator end() const noexcept;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class Slice<T>::iterator final {
|
||||
public:
|
||||
using value_type = T;
|
||||
using pointer = T *;
|
||||
using reference = T &;
|
||||
|
||||
T &operator*() const noexcept;
|
||||
T *operator->() const noexcept;
|
||||
iterator &operator++() noexcept;
|
||||
iterator operator++(int) noexcept;
|
||||
bool operator==(const iterator &) const noexcept;
|
||||
bool operator!=(const iterator &) const noexcept;
|
||||
};
|
||||
#
|
||||
# } // namespace rust
|
||||
|
@ -118,6 +118,10 @@ public:
|
||||
Slice(const Slice<T> &) noexcept = default;
|
||||
~Slice() noexcept = default;
|
||||
|
||||
class iterator;
|
||||
iterator begin() const noexcept;
|
||||
iterator end() const noexcept;
|
||||
|
||||
private:
|
||||
friend impl<Slice>;
|
||||
// Not necessarily ABI compatible with &[T]. Codegen will translate to
|
||||
@ -125,6 +129,27 @@ private:
|
||||
T *ptr;
|
||||
size_t len;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class Slice<T>::iterator final {
|
||||
public:
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = T;
|
||||
using pointer = typename std::add_pointer<T>::type;
|
||||
using reference = typename std::add_lvalue_reference<T>::type;
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
|
||||
T &operator*() const noexcept;
|
||||
T *operator->() const noexcept;
|
||||
iterator &operator++() noexcept;
|
||||
iterator operator++(int) noexcept;
|
||||
bool operator==(const iterator &) const noexcept;
|
||||
bool operator!=(const iterator &) const noexcept;
|
||||
|
||||
private:
|
||||
friend class Slice;
|
||||
T *pos;
|
||||
};
|
||||
#endif // CXXBRIDGE1_RUST_SLICE
|
||||
|
||||
#ifndef CXXBRIDGE1_RUST_BOX
|
||||
@ -409,6 +434,53 @@ template <typename T>
|
||||
size_t Slice<T>::length() const noexcept {
|
||||
return this->len;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T &Slice<T>::iterator::operator*() const noexcept {
|
||||
return *this->pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T *Slice<T>::iterator::operator->() const noexcept {
|
||||
return this->pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
|
||||
++this->pos;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
|
||||
auto ret = iterator(*this);
|
||||
++this->pos;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
|
||||
return this->pos == other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
|
||||
return this->pos != other.pos;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::begin() const noexcept {
|
||||
iterator it;
|
||||
it.pos = this->ptr;
|
||||
return it;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Slice<T>::iterator Slice<T>::end() const noexcept {
|
||||
iterator it = this->begin();
|
||||
it.pos += this->len;
|
||||
return it;
|
||||
}
|
||||
#endif // CXXBRIDGE1_RUST_SLICE
|
||||
|
||||
#ifndef CXXBRIDGE1_RUST_BOX
|
||||
|
Loading…
x
Reference in New Issue
Block a user