Skip to content

Commit f0d68e5

Browse files
committed
Have FromIterator receive an IntoIterator type.
Instead of receiving an IteratorBase<Item> type. This allows us to eliminate IteratorBase and drop virtual on Iterator::next(). The downside is the template implementation of Option::from_iter() and Result::from_iter() is much more complex, as you can't deduce the type inside a concept you're receiving as a parameter.
1 parent 496b8b2 commit f0d68e5

25 files changed

+200
-161
lines changed

subdoc/lib/path.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class RecordIter final
8989
return RecordIter(decl);
9090
}
9191

92-
sus::Option<std::string_view> next() noexcept final {
92+
sus::Option<std::string_view> next() noexcept {
9393
if (next_decl_) {
9494
clang::RecordDecl* cur_decl = next_decl_;
9595

@@ -134,7 +134,7 @@ class NamespaceIter final
134134
return NamespaceIter(decl);
135135
}
136136

137-
sus::Option<Namespace> next() noexcept final {
137+
sus::Option<Namespace> next() noexcept {
138138
if (next_ndecl_) {
139139
clang::NamespaceDecl* cur_ndecl = next_ndecl_;
140140
next_ndecl_ =

subspace/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ target_sources(subspace PUBLIC
5151
"fn/fn_bind.h"
5252
"fn/fn_defn.h"
5353
"fn/fn_impl.h"
54+
"iter/__private/into_iterator_archetype.h"
5455
"iter/__private/iterator_end.h"
5556
"iter/__private/iterator_loop.h"
5657
"iter/__private/step.h"
5758
"iter/boxed_iterator.h"
5859
"iter/filter.h"
5960
"iter/from_iterator.h"
61+
"iter/into_iterator.h"
6062
"iter/iterator.h"
6163
"iter/iterator_concept.h"
6264
"iter/iterator_defn.h"

subspace/containers/__private/array_iter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct ArrayIntoIter final
4040
return ArrayIntoIter(::sus::move(array));
4141
}
4242

43-
Option<Item> next() noexcept final {
43+
Option<Item> next() noexcept {
4444
if (front_index_ == back_index_) [[unlikely]]
4545
return Option<Item>::none();
4646
// SAFETY: The front and back indicies are kept within the length of the

subspace/containers/__private/slice_iter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct [[sus_trivial_abi]] SliceIter final
5050
return SliceIter(start, len);
5151
}
5252

53-
Option<Item> next() noexcept final {
53+
Option<Item> next() noexcept {
5454
if (ptr_ == end_) [[unlikely]]
5555
return Option<Item>::none();
5656
// SAFETY: end_ is always > ptr_ when we get here (this was checked by the
@@ -121,7 +121,7 @@ struct [[sus_trivial_abi]] SliceIterMut final
121121
}
122122

123123
// sus::iter::Iterator trait.
124-
Option<Item> next() noexcept final {
124+
Option<Item> next() noexcept {
125125
if (ptr_ == end_) [[unlikely]]
126126
return Option<Item>::none();
127127
// SAFETY: end_ is always > ptr_ when we get here (this was checked by the

subspace/containers/__private/vec_iter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct VecIntoIter final
4141
}
4242

4343
/// sus::iter::Iterator trait.
44-
Option<Item> next() noexcept final {
44+
Option<Item> next() noexcept {
4545
if (front_index_ == back_index_) [[unlikely]]
4646
return Option<Item>::none();
4747
// SAFETY: This class owns the Vec and does not expose it, so its length is

subspace/containers/vec.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "subspace/containers/__private/vec_marker.h"
2525
#include "subspace/containers/slice.h"
2626
#include "subspace/iter/from_iterator.h"
27+
#include "subspace/iter/into_iterator.h"
2728
#include "subspace/macros/compiler.h"
2829
#include "subspace/mem/move.h"
2930
#include "subspace/mem/relocate.h"
@@ -110,9 +111,10 @@ class Vec {
110111
/// Constructs a vector by taking all the elements from the iterator.
111112
///
112113
/// sus::iter::FromIterator trait.
113-
static constexpr Vec from_iter(::sus::iter::IteratorBase<T>&& iter) noexcept
114+
static constexpr Vec from_iter(::sus::iter::IntoIterator<T> auto&& into_iter) noexcept
114115
requires(::sus::mem::Move<T> && !std::is_reference_v<T>)
115116
{
117+
auto&& iter = sus::move(into_iter).into_iter();
116118
auto [lower, upper] = iter.size_hint();
117119
auto v = Vec::with_capacity(::sus::move(upper).unwrap_or(lower));
118120
for (T t : iter) v.push(::sus::move(t));
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include "subspace/iter/into_iterator.h"
18+
19+
namespace sus::option {
20+
template <class T>
21+
class Option;
22+
}
23+
24+
namespace sus::iter {
25+
template <class SubclassT, class ItemT>
26+
class IteratorImpl;
27+
} // namespace sus::iter
28+
29+
namespace sus::iter::__private {
30+
31+
template <class T>
32+
struct IntoIteratorArchetype {
33+
template <class Item>
34+
struct Iter final : public IteratorImpl<Iter<Item>, Item> {
35+
::sus::option::Option<Item> next() noexcept {
36+
return ::sus::option::Option<Item>::none();
37+
}
38+
};
39+
Iter<T> into_iter() && { return Iter<T>(); }
40+
static_assert(::sus::iter::IntoIterator<Iter<T>, T>);
41+
};
42+
43+
} // namespace sus::iter::__private

subspace/iter/boxed_iterator.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
namespace sus::iter {
2424

25-
template <class Item>
26-
class IteratorBase;
2725
template <class Iter, class Item>
2826
class IteratorImpl;
2927

@@ -91,7 +89,7 @@ class [[nodiscard]] [[sus_trivial_abi]] BoxedIterator final
9189
}
9290

9391
// sus::iter::Iterator trait.
94-
Option<Item> next() noexcept final { return next_(iter_); }
92+
Option<Item> next() noexcept { return next_(iter_); }
9593
// sus::iter::DoubleEndedIterator trait.
9694
Option<Item> next_back() noexcept
9795
requires(DoubleEnded)

subspace/iter/empty.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class [[nodiscard]] [[sus_trivial_abi]] Empty final
2929
constexpr Empty() = default;
3030

3131
// sus::iter::Iterator trait.
32-
constexpr Option<Item> next() noexcept final {
32+
constexpr Option<Item> next() noexcept {
3333
return sus::Option<Item>::none();
3434
}
3535
// sus::iter::DoubleEndedIterator trait.

subspace/iter/filter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class [[nodiscard]] [[sus_trivial_abi]] Filter final
4040
}
4141

4242
// sus::iter::Iterator trait.
43-
Option<Item> next() noexcept final {
43+
Option<Item> next() noexcept {
4444
InnerSizedIter& iter = next_iter_;
4545
Pred& pred = pred_;
4646

0 commit comments

Comments
 (0)