Skip to content

Commit

Permalink
refactor(core): replace MyIterator with standard Iterator
Browse files Browse the repository at this point in the history
[rust-lang/rust#106541][1] added `#[const_trait]` to `Iterator` as well
as internal attribute to bypass the constness check for other methods
than `Iterator::next`. As a result, user code can now implement `const
Iterator` in a normal way, i.e., just by implementing `Iterator::next`.

[1]: rust-lang/rust#106541
  • Loading branch information
yvt committed Mar 18, 2023
1 parent 8b64feb commit 1d49486
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 33 deletions.
21 changes: 1 addition & 20 deletions doc/toolchain_limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type Alias<T> = Struct<{<T as Trait>::N}>;

### `[tag:const_for]` `for` loops are unusable in `const fn`

Technically it's available under the compiler feature `const_for`, but the lack of essential trait implementations (e.g., `[ref:range_const_iterator]`, `[ref:const_slice_iter]`) and above all the inability of implementing `const Iterator` (`[ref:iterator_const_default]`) make it unusable.
Technically it's available under the compiler feature `const_for`, but the lack of essential trait implementations (e.g., `[ref:range_const_iterator]`, `[ref:const_slice_iter]`) makes it unusable in many cases.


### `[tag:const_static_item_ref]` `const`s and `const fn`s can't refer to `static`s
Expand Down Expand Up @@ -449,25 +449,6 @@ const _: () = assert!(matches!((2..4).next(), Some(2)));
```


### `[tag:iterator_const_default]` `Iterator` lack `#[const_trait]`

```rust,compile_fail
#![feature(const_trait_impl)]
#![feature(const_mut_refs)]
struct MyIterator;
// error: const `impl` for trait `Iterator` which is not marked with `#[const_trait]`
impl const Iterator for MyIterator {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
Some(42)
}
}
```


### `[tag:const_assert_eq]` `assert_eq!` and similar macros are unusable in `const fn`

```rust,compile_fail,E0015
Expand Down
17 changes: 4 additions & 13 deletions src/r3_core/src/bind/sorter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ pub(super) const fn sort_bindings<Callback, SorterUseInfoList, VertexList>(
End,
}

impl const MyIterator for VertexIter<'_> {
impl const Iterator for VertexIter<'_> {
type Item = Vertex;

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -369,7 +369,7 @@ pub(super) const fn sort_bindings<Callback, SorterUseInfoList, VertexList>(
End,
}

impl<Callback> const MyIterator for SuccessorIter<'_, Callback>
impl<Callback> const Iterator for SuccessorIter<'_, Callback>
where
Callback: ~const SorterCallback,
{
Expand Down Expand Up @@ -533,23 +533,14 @@ pub(super) const fn sort_bindings<Callback, SorterUseInfoList, VertexList>(
// Helper traits
// --------------------------------------------------------------------------

// `const Iterator` is currently very hard to implement
// [ref:iterator_const_default]
/// An [`Iterator`][] usable in `const fn`.
#[const_trait]
trait MyIterator {
type Item;
fn next(&mut self) -> Option<Self::Item>;
}

#[const_trait]
trait GraphAccess<VertexRef> {
type VertexIter<'a>: ~const MyIterator<Item = VertexRef> + ~const Destruct + 'a
type VertexIter<'a>: ~const Iterator<Item = VertexRef> + ~const Destruct + 'a
where
Self: 'a;
fn vertices(&self) -> Self::VertexIter<'_>;

type SuccessorIter<'a>: ~const MyIterator<Item = VertexRef> + ~const Destruct + 'a
type SuccessorIter<'a>: ~const Iterator<Item = VertexRef> + ~const Destruct + 'a
where
Self: 'a;
fn successors(&self, v: &VertexRef) -> Self::SuccessorIter<'_>;
Expand Down

0 comments on commit 1d49486

Please sign in to comment.