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

Add Array#flat and Array#flatMap to js-sys (fixes #1454) #1573

Merged

Conversation

not-an-aardvark
Copy link
Contributor

This adds Array#flat and Array#flatMap to js-sys. These JS proposals both reached stage 4 in January, as noted in #1454.

This is my first time working with wasm-bindgen, so please review thoroughly.

#[wasm_bindgen(method, js_name = flatMap)]
pub fn flat_map(
this: &Array,
callback: &mut dyn FnMut(JsValue, u32, Array) -> Vec<JsValue>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure of the best type to use for the callback here. In JS, it's possible for the flatMap callback to return a non-array value v, which has the same effect as returning the one-element array [v]. But it seems cleaner to be more strict in the Rust type.

I think Vec<JsValue> is the most appropriate return value here (as opposed to e.g. a slice), but it might be worth double-checking this; I don't have a lot of experience with lifetimes.

@alexcrichton
Copy link
Contributor

Looks great to me and I agree this is a good signature for flat_map for now, thanks @not-an-aardvark!

@alexcrichton alexcrichton merged commit b11b6df into rustwasm:master Jun 4, 2019
@not-an-aardvark not-an-aardvark deleted the implement-flat-and-flatmap branch June 4, 2019 17:16
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.

None yet

2 participants