Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support output iterators #460

Closed
Manishearth opened this issue Mar 15, 2024 · 0 comments · Fixed by #462
Closed

Support output iterators #460

Manishearth opened this issue Mar 15, 2024 · 0 comments · Fixed by #462
Assignees
Milestone

Comments

@Manishearth
Copy link
Contributor

Manishearth commented Mar 15, 2024

Split out from #251

There are two things here. Iterators and Iterables. Iterables are the equivalent of IntoIterator, which themselves store no iteration state but can be "iterated over", producing a temporary iterator type. Iterators are the equivalent of Iterator which store iterator state and can get "exhausted" once iterated.

Most language with iteration glue have that glue prioritize iterables, not iterators, though usually there's a standard way to make iterators be iterables as well. For example, in Javascript, iterables have an @@iterator method that produces an iterator, and anything with a next() method that returns an object with done: bool and value fields. The way you make an iterator also an iterable is just to give it a [Symbol.iterator]() {return this;} method.

From an ICU4X standpoint it's more important for us to prioritize iterators; most of the time we have methods that take additional parameters to produce an iterator, so they're not themselves iterable.

Roughly, we should do something like this:

#[diplomat::opaque]
struct ICU4XLineBreaker(..);

impl ICU4XLineBreaker {
    pub fn break(&self, text: &str) -> ICU4XLineBreakIterator {..}
}


struct ICU4XLineBreakIterator<'a>(...);

impl<'a> ICU4XLineBreakIterator<'a> {
    #[diplomat::attr(*, iterator_next)]
    pub fn next(&self) -> Result<SomeType, ()> {
      // ...
    }
}

An actual next() method is not generated (in the long run we can make htis configurable). This would generate an ICU4XLineBreakIterator struct with a properly wrapped next() class that produces the {done, value} enum and additionally has a return this @@iterator method.

We might want to add a supports=iterators name/value Diplomat attribute CFG as well.

For iterables, we can also add a #[diplomat::attr(*, iterable)] type that can be tagged on a single method that has no additional parameters to mark things.

Originally posted by @Manishearth in #251 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant