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 support for FnOnce to Closure<T> #1281

Merged
merged 3 commits into from
Mar 1, 2019
Merged

Conversation

fitzgen
Copy link
Member

@fitzgen fitzgen commented Feb 21, 2019

Sort of. By turning them into FnMut and doing a dynamic check that they are only ever called once.

I initially tried to get Closure<FnOnce(...) -> R> working, but that proved too difficult.

@fitzgen fitzgen requested a review from alexcrichton February 21, 2019 00:28
@fitzgen fitzgen added the TWiRaWA Nominate this PR for inclusion in the next issue of This Week in Rust and WebAssembly label Feb 21, 2019
Copy link
Contributor

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Looks great to me! I think CI may be failing though?

{
type FnMut = FnMut($($var),*) -> R;

fn into_fn_mut(self) -> Box<Self::FnMut> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically this isn't quite as optimized as we might otherwise want because a FnOnce that doesn't actually close over anything will allocate because of the discriminant on Option<T>. I think fully supporting this in the limit though will require specialized support in the JS bindings (the JS bindings are the ones to clear the stored pointer).

Mind throwing a comment somewhere about that? I think this is totally fine as a first pass

Copy link
Contributor

Choose a reason for hiding this comment

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

(technically the same comment for the one below too)

@fitzgen
Copy link
Member Author

fitzgen commented Feb 21, 2019

https://travis-ci.com/rustwasm/wasm-bindgen/jobs/179355779#L951-L961

RuntimeError: memory access out of bounds
    at dlmalloc::dlmalloc::Dlmalloc::insert_large_chunk::h752576fb10674c30 (wasm-function[1556]:244)
    at dlmalloc::dlmalloc::Dlmalloc::free::h4e7d0bccb8a2e263 (wasm-function[659]:806)
    at __rdl_dealloc (wasm-function[10159]:8)
    at __rust_dealloc (wasm-function[10043]:7)
    at alloc::alloc::dealloc::h1b984820f03b24d3 (wasm-function[4389]:100)
    at <alloc::alloc::Global as core::alloc::Alloc>::dealloc::h5015cfcc10ef6ccf (wasm-function[5536]:84)
    at <alloc::rc::Rc<T> as core::ops::drop::Drop>::drop::h6a78b25b82ddd7e0 (wasm-function[2222]:216)
    at core::ptr::real_drop_in_place::h3aa3cadebfeeb41a (wasm-function[8375]:38)
    at core::ptr::real_drop_in_place::hc4e654cf5dfc7dc7 (wasm-function[5383]:84)
    at wasm_bindgen_test::__rt::State::log_test_result::h3d6ffabb89d2894e (wasm-function[672]:841)

wat

@fitzgen fitzgen merged commit ba84db5 into rustwasm:master Mar 1, 2019
@fitzgen fitzgen deleted the new-fn-once branch March 1, 2019 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TWiRaWA Nominate this PR for inclusion in the next issue of This Week in Rust and WebAssembly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants