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

Implement the Iterator trait for JS iterators #797

Merged
merged 1 commit into from
Sep 10, 2018

Conversation

alexcrichton
Copy link
Contributor

This commit implements the standard library's Iterator trait for the
js_sys::Iterator type, using the iterator protocol described on MDN

Closes #777

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments below about iterables that throw

let val = Iterator::next(&*self)
.expect("JS object didn't obey iterator protocol");
if val.done() {
None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was wrong, and that if both done and value are present (happens when returning from a generator), that we should add the value to the iterator, but it seems that it doesn't get into the iterable in JS:

done-and-value

Carry on!


fn next(&mut self) -> Option<JsValue> {
let val = Iterator::next(&*self)
.expect("JS object didn't obey iterator protocol");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterables (such as generators) can totally throw, and it isn't really violating the protocol :-/

generator-throws

I think we should have Iterator<Item = Result<JsValue, JsValue>> and then fuse the iterator such that calling next after the first Err always returns None and doesn't call back into JS again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting!

I don't think we could auto-fuse though unless we start setting a JS property on the iterator (which seems icky) or have a wrapper in Rust. I'd be ok returning a Result<JsValue, JsValue> but that would make fusing difficult for sure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the fusing is important for combinators and .collect() to work correctly.

Perhaps we should implement IntoIterator for &'a js_sys::Iterator witha custom type that has a bool for fusing?

This commit implements the standard library's `Iterator` trait for the
`js_sys::Iterator` type, using the iterator protocol described on [MDN]

Closes rustwasm#777

[MDN]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
@alexcrichton
Copy link
Contributor Author

IntoIterator sounds like a great idea to me, updated to that!

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice -- thanks!

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 this pull request may close these issues.

2 participants