From 1914bba0be878d5a16cb633dcbd02ded3bbf1dda Mon Sep 17 00:00:00 2001 From: Sam Day Date: Fri, 29 Mar 2019 09:07:42 +0100 Subject: [PATCH] Implement Debug on Closures resolves #1387 --- src/closure.rs | 10 ++++++++++ tests/wasm/closures.rs | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/src/closure.rs b/src/closure.rs index 957f68fe..885b5975 100644 --- a/src/closure.rs +++ b/src/closure.rs @@ -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}; @@ -489,6 +490,15 @@ fn _check() { _assert::<&Closure String>>(); } +impl fmt::Debug for Closure +where + T: ?Sized, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Closure {{ ... }}") + } +} + impl Drop for Closure where T: ?Sized, diff --git a/tests/wasm/closures.rs b/tests/wasm/closures.rs index ed1e29de..4ce047f0 100755 --- a/tests/wasm/closures.rs +++ b/tests/wasm/closures.rs @@ -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); + assert_eq!(&format!("{:?}", closure), "Closure { ... }"); +} + #[wasm_bindgen_test] fn long_lived() { let hit = Rc::new(Cell::new(false));