Skip to content

Commit

Permalink
Slice<T>::iterator to be contiguous and random_access
Browse files Browse the repository at this point in the history
`rust::Vec` fails `std::ranges::contiguous_range` checks as those have
to satisfy `random_access_range`, and `contiguous_iterator`. This has
caused mismatches with certain `span` types in other codebases.

This PR adds the necessary increment and concept category to
`Slice<T>::iterator`, to correct this issue.

Bug: dtolnay#1392
  • Loading branch information
cdesouza-chromium committed Nov 9, 2024
1 parent 22d40f7 commit 3f04db0
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/cxx.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <algorithm>
#include <array>
#include <concepts>
#include <cassert>
#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -217,6 +218,7 @@ template <typename T>
class Slice<T>::iterator final {
public:
using iterator_category = std::random_access_iterator_tag;
using iterator_concept = std::contiguous_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = typename std::add_pointer<T>::type;
Expand Down Expand Up @@ -244,6 +246,10 @@ class Slice<T>::iterator final {
bool operator>(const iterator &) const noexcept;
bool operator>=(const iterator &) const noexcept;

template <std::integral N>
friend iterator operator+(N n, const iterator& it) noexcept {
return it + n;
}
private:
friend class Slice;
void *pos;
Expand Down

0 comments on commit 3f04db0

Please sign in to comment.