Fastly Compute C++ SDK
Loading...
Searching...
No Matches
rust_iterator_range.h
Go to the documentation of this file.
1#ifndef FASTLY_DETAIL_RUST_ITERATOR_RANGE_H
2#define FASTLY_DETAIL_RUST_ITERATOR_RANGE_H
3
4#include <fastly/sdk-sys.h>
5#include <optional>
6
7namespace fastly::detail {
8// A CRTP class that adapts a Rust-style iterator to a C++ range.
9// CppRng is the C++ class that implements the iterator interface,
10// and RustIt is the Rust iterator type that we are wrapping.
11// The C++ class should implement a `next()` method that returns an
12// `std::optional<value_type>`, where `value_type` is the type of the values
13// being iterated over.
14template <class CppRng, class RustIt> class RustIteratorRange {
15public:
16 RustIteratorRange(rust::Box<RustIt> iter) : iter_(std::move(iter)) {}
17
18 class iterator {
19 public:
20 using iterator_category = std::input_iterator_tag;
21 // next() should return an optional<value_type>, so we extract the value
22 // type from that
23 using value_type =
24 typename decltype(std::declval<CppRng>().next())::value_type;
25 using difference_type = std::ptrdiff_t;
28
29 iterator() : range_(nullptr) {}
30 explicit iterator(RustIteratorRange *range) : range_(range) {
31 if (range) {
32 ++(*this); // Initialize the first value
33 }
34 }
35
37 if (!range_) {
38 return *this; // No range to iterate over
39 }
40
41 auto val = static_cast<CppRng *>(range_)->next();
42 if (val) {
43 cache_.emplace(std::move(*val));
44 } else {
45 range_ = nullptr; // Signal end of iteration
46 }
47 return *this;
48 }
49
51 iterator tmp = *this;
52 ++(*this);
53 return tmp;
54 }
55
56 value_type &operator*() { return *cache_; }
57
58 bool operator==(const iterator &other) const {
59 return range_ == other.range_;
60 }
61
62 bool operator!=(const iterator &other) const { return !(*this == other); }
63
64 private:
65 RustIteratorRange *range_;
66 std::optional<value_type> cache_;
67 };
68
69 iterator begin() { return iterator(this); }
70 iterator end() { return iterator(nullptr); }
71
72protected:
73 rust::Box<RustIt> iter_;
74};
75} // namespace fastly::detail
76
77#endif
Definition rust_iterator_range.h:18
iterator(RustIteratorRange *range)
Definition rust_iterator_range.h:30
bool operator!=(const iterator &other) const
Definition rust_iterator_range.h:62
value_type & reference
Definition rust_iterator_range.h:27
value_type * pointer
Definition rust_iterator_range.h:26
iterator()
Definition rust_iterator_range.h:29
bool operator==(const iterator &other) const
Definition rust_iterator_range.h:58
std::ptrdiff_t difference_type
Definition rust_iterator_range.h:25
std::input_iterator_tag iterator_category
Definition rust_iterator_range.h:20
value_type & operator*()
Definition rust_iterator_range.h:56
iterator operator++(int)
Definition rust_iterator_range.h:50
typename decltype(std::declval< CppRng >().next())::value_type value_type
Definition rust_iterator_range.h:23
iterator & operator++()
Definition rust_iterator_range.h:36
iterator end()
Definition rust_iterator_range.h:70
rust::Box< RustIt > iter_
Definition rust_iterator_range.h:73
iterator begin()
Definition rust_iterator_range.h:69
RustIteratorRange(rust::Box< RustIt > iter)
Definition rust_iterator_range.h:16
Definition access_bridge_internals.h:6