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

Adding in Array::iter and Array::to_vec #1909

Merged
merged 2 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,76 @@ extern "C" {
pub fn unshift(this: &Array, value: &JsValue) -> u32;
}

/// Iterator returned by `Array::iter`
#[derive(Debug, Clone)]
pub struct ArrayIter<'a> {
start: u32,
end: u32,
Pauan marked this conversation as resolved.
Show resolved Hide resolved
array: &'a Array,
}

impl<'a> std::iter::Iterator for ArrayIter<'a> {
type Item = JsValue;

fn next(&mut self) -> Option<Self::Item> {
if self.start == self.end {
None

} else {
let value = self.array.get(self.start);
self.start += 1;
Some(value)
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let diff = (self.end - self.start) as usize;
(diff, Some(diff))
}
}

impl<'a> std::iter::DoubleEndedIterator for ArrayIter<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.start == self.end {
None

} else {
let value = self.array.get(self.end - 1);
self.end -= 1;
Some(value)
}
}
}

impl<'a> std::iter::FusedIterator for ArrayIter<'a> {}

impl<'a> std::iter::ExactSizeIterator for ArrayIter<'a> {}

impl Array {
/// Returns an iterator over the values of the JS array.
pub fn iter(&self) -> ArrayIter<'_> {
ArrayIter {
start: 0,
end: self.length(),
array: self,
}
}

/// Converts the JS array into a new Vec.
pub fn to_vec(&self) -> Vec<JsValue> {
let len = self.length();

let mut output = Vec::with_capacity(len as usize);

for i in 0..len {
output.push(self.get(i));
}

output
}
}

// TODO pre-initialize the Array with the correct length using TrustedLen
impl<A> std::iter::FromIterator<A> for Array
where
Expand Down
35 changes: 35 additions & 0 deletions crates/js-sys/tests/wasm/Array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,41 @@ fn from_iter() {
);
}

#[wasm_bindgen_test]
fn to_vec() {
let array = vec![JsValue::from("a"), JsValue::from("b"), JsValue::from("c")]
.into_iter()
.collect::<js_sys::Array>();

assert_eq!(array.to_vec(), vec![JsValue::from("a"), JsValue::from("b"), JsValue::from("c")]);
}

#[wasm_bindgen_test]
fn iter() {
let array = vec![JsValue::from("a"), JsValue::from("b"), JsValue::from("c")]
.into_iter()
.collect::<js_sys::Array>();

let mut iter = array.iter();

assert_eq!(iter.size_hint(), (3, Some(3)));
assert_eq!(iter.next(), Some(JsValue::from("a")));

assert_eq!(iter.size_hint(), (2, Some(2)));
assert_eq!(iter.next_back(), Some(JsValue::from("c")));

assert_eq!(iter.size_hint(), (1, Some(1)));
assert_eq!(iter.next_back(), Some(JsValue::from("b")));

assert_eq!(iter.size_hint(), (0, Some(0)));
assert_eq!(iter.next(), None);

assert_eq!(iter.size_hint(), (0, Some(0)));
assert_eq!(iter.next_back(), None);

assert_eq!(array.iter().collect::<Vec<JsValue>>(), vec![JsValue::from("a"), JsValue::from("b"), JsValue::from("c")]);
}

#[wasm_bindgen_test]
fn new_with_length() {
let array = Array::new_with_length(5);
Expand Down