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

Closure should impl Debug #1387

Closed
Pauan opened this issue Mar 23, 2019 · 1 comment · Fixed by #1408
Closed

Closure should impl Debug #1387

Pauan opened this issue Mar 23, 2019 · 1 comment · Fixed by #1408
Labels
good first issue This is a good issue for people who have never contributed to wasm-bindgen before

Comments

@Pauan
Copy link
Contributor

Pauan commented Mar 23, 2019

Motivation

When implementing rustwasm/gloo#30, I created this type:

pub struct EventListener<'a> {
    target: EventTarget,
    kind: &'a str,
    callback: Option<Closure<FnMut(Event)>>,
}

I would really like to just slap a #[derive(Debug)] and call it a day, but Closure doesn't impl Debug, so I have to do this instead:

impl<'a> std::fmt::Debug for EventListener<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("EventListener")
            .field("target", &self.target)
            .field("kind", &self.kind)
            .field("callback", &"Closure { ... }")
            .finish()
    }
}

Proposed Solution

I propose that Closure should provide a Debug impl, which essentially just outputs Closure { ... } (or similar).

Alternatives

Maybe the Debug impl should expose more information? For example, it could call .toString() on the JS function.

@Pauan Pauan added the enhancement New feature or request label Mar 23, 2019
@alexcrichton alexcrichton added good first issue This is a good issue for people who have never contributed to wasm-bindgen before and removed enhancement New feature or request labels Mar 25, 2019
@fitzgen
Copy link
Member

fitzgen commented Mar 25, 2019

+1 for sure. I've done this everything-except-the-closure debug impl many times as well.

Maybe the Debug impl should expose more information? For example, it could call .toString() on the JS function.

Don't think we should do this -- the function's source won't be anything that the user authored, just wasm-bindgen's internally defined JS source for closures.

samcday added a commit to samcday/wasm-bindgen that referenced this issue Mar 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue This is a good issue for people who have never contributed to wasm-bindgen before
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants