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 IntoIterator for Array #3477

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

## [Unreleased](https://github.com/rustwasm/wasm-bindgen/compare/0.2.86...main)

### Added

* Implemented `IntoIterator` for `Array`.
[#3477](https://github.com/rustwasm/wasm-bindgen/pull/3477)

### Changed

* Deprecate `HtmlMenuItemElement` and parts of `HtmlMenuElement`.
Expand Down
96 changes: 96 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,64 @@ extern "C" {
pub fn unshift(this: &Array, value: &JsValue) -> u32;
}

/// Iterator returned by `Array::into_iter`
#[derive(Debug, Clone)]
pub struct ArrayIntoIter {
range: std::ops::Range<u32>,
array: Array,
}

impl std::iter::Iterator for ArrayIntoIter {
type Item = JsValue;

fn next(&mut self) -> Option<Self::Item> {
let index = self.range.next()?;
Some(self.array.get(index))
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.range.size_hint()
}

#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.range.count()
}

#[inline]
fn last(self) -> Option<Self::Item>
where
Self: Sized,
{
let Self { range, array } = self;
range.last().map(|index| array.get(index))
}

#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.range.nth(n).map(|index| self.array.get(index))
}
}

impl std::iter::DoubleEndedIterator for ArrayIntoIter {
fn next_back(&mut self) -> Option<Self::Item> {
let index = self.range.next_back()?;
Some(self.array.get(index))
}

fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
self.range.nth_back(n).map(|index| self.array.get(index))
}
}

impl std::iter::FusedIterator for ArrayIntoIter {}

impl std::iter::ExactSizeIterator for ArrayIntoIter {}

/// Iterator returned by `Array::iter`
#[derive(Debug, Clone)]
pub struct ArrayIter<'a> {
Expand All @@ -629,13 +687,39 @@ impl<'a> std::iter::Iterator for ArrayIter<'a> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.range.size_hint()
}

#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.range.count()
}

#[inline]
fn last(self) -> Option<Self::Item>
where
Self: Sized,
{
let Self { range, array } = self;
range.last().map(|index| array.get(index))
}

#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.range.nth(n).map(|index| self.array.get(index))
}
}

impl<'a> std::iter::DoubleEndedIterator for ArrayIter<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
let index = self.range.next_back()?;
Some(self.array.get(index))
}

fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
self.range.nth_back(n).map(|index| self.array.get(index))
}
}

impl<'a> std::iter::FusedIterator for ArrayIter<'a> {}
Expand Down Expand Up @@ -665,6 +749,18 @@ impl Array {
}
}

impl std::iter::IntoIterator for Array {
type Item = JsValue;
type IntoIter = ArrayIntoIter;

fn into_iter(self) -> Self::IntoIter {
ArrayIntoIter {
range: 0..self.length(),
array: self,
}
}
}

// TODO pre-initialize the Array with the correct length using TrustedLen
impl<A> std::iter::FromIterator<A> for Array
where
Expand Down