Skip to content

Commit c5f18b6

Browse files
authored
Merge pull request #1408 from samcday/impl-debug-on-closure
Implement Debug on Closures
2 parents 1121393 + 1914bba commit c5f18b6

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/closure.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! closures" from Rust to JS. Some more details can be found on the `Closure`
55
//! type itself.
66
7+
use std::fmt;
78
#[cfg(feature = "nightly")]
89
use std::marker::Unsize;
910
use std::mem::{self, ManuallyDrop};
@@ -489,6 +490,15 @@ fn _check() {
489490
_assert::<&Closure<FnMut() -> String>>();
490491
}
491492

493+
impl<T> fmt::Debug for Closure<T>
494+
where
495+
T: ?Sized,
496+
{
497+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
498+
write!(f, "Closure {{ ... }}")
499+
}
500+
}
501+
492502
impl<T> Drop for Closure<T>
493503
where
494504
T: ?Sized,

tests/wasm/closures.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ fn cannot_reuse() {
107107
assert!(cannot_reuse_call_again().is_err());
108108
}
109109

110+
#[wasm_bindgen_test]
111+
fn debug() {
112+
let closure = Closure::wrap(Box::new(|| {}) as Box<FnMut()>);
113+
assert_eq!(&format!("{:?}", closure), "Closure { ... }");
114+
}
115+
110116
#[wasm_bindgen_test]
111117
fn long_lived() {
112118
let hit = Rc::new(Cell::new(false));

0 commit comments

Comments
 (0)