Skip to content

Commit 9fa0ab9

Browse files
arnauorriolsArnau Orriols
andauthored
Implement Extend<A> for Array where A: AsRef<JsValue> (#2681)
Co-authored-by: Arnau Orriols <[email protected]>
1 parent 3f3ed81 commit 9fa0ab9

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

crates/js-sys/src/lib.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,13 +657,23 @@ where
657657
where
658658
T: IntoIterator<Item = A>,
659659
{
660-
let out = Array::new();
660+
let mut out = Array::new();
661+
out.extend(iter);
662+
out
663+
}
664+
}
661665

666+
impl<A> std::iter::Extend<A> for Array
667+
where
668+
A: AsRef<JsValue>,
669+
{
670+
fn extend<T>(&mut self, iter: T)
671+
where
672+
T: IntoIterator<Item = A>,
673+
{
662674
for value in iter {
663-
out.push(value.as_ref());
675+
self.push(value.as_ref());
664676
}
665-
666-
out
667677
}
668678
}
669679

crates/js-sys/tests/wasm/Array.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ fn from_iter() {
8181
);
8282
}
8383

84+
#[wasm_bindgen_test]
85+
fn extend() {
86+
let mut array = array!["a", "b"];
87+
array.extend(vec![JsValue::from("c"), JsValue::from("d")]);
88+
assert_eq!(array, array!["a", "b", "c", "d"]);
89+
}
90+
8491
#[wasm_bindgen_test]
8592
fn to_vec() {
8693
let array = vec![JsValue::from("a"), JsValue::from("b"), JsValue::from("c")]

0 commit comments

Comments
 (0)