Skip to content

Commit

Permalink
Merge pull request #1408 from samcday/impl-debug-on-closure
Browse files Browse the repository at this point in the history
Implement Debug on Closures
  • Loading branch information
alexcrichton authored Mar 29, 2019
2 parents 1121393 + 1914bba commit c5f18b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! closures" from Rust to JS. Some more details can be found on the `Closure`
//! type itself.

use std::fmt;
#[cfg(feature = "nightly")]
use std::marker::Unsize;
use std::mem::{self, ManuallyDrop};
Expand Down Expand Up @@ -489,6 +490,15 @@ fn _check() {
_assert::<&Closure<FnMut() -> String>>();
}

impl<T> fmt::Debug for Closure<T>
where
T: ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Closure {{ ... }}")
}
}

impl<T> Drop for Closure<T>
where
T: ?Sized,
Expand Down
6 changes: 6 additions & 0 deletions tests/wasm/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ fn cannot_reuse() {
assert!(cannot_reuse_call_again().is_err());
}

#[wasm_bindgen_test]
fn debug() {
let closure = Closure::wrap(Box::new(|| {}) as Box<FnMut()>);
assert_eq!(&format!("{:?}", closure), "Closure { ... }");
}

#[wasm_bindgen_test]
fn long_lived() {
let hit = Rc::new(Cell::new(false));
Expand Down

0 comments on commit c5f18b6

Please sign in to comment.