diff --git a/src/external_references.rs b/src/external_references.rs index 6e8441f5..3a9eb106 100644 --- a/src/external_references.rs +++ b/src/external_references.rs @@ -11,6 +11,7 @@ use crate::NamedGetterCallback; use crate::NamedSetterCallback; use crate::PropertyEnumeratorCallback; use std::ffi::c_void; +use std::fmt::Debug; #[derive(Clone, Copy)] pub union ExternalReference<'s> { @@ -26,6 +27,13 @@ pub union ExternalReference<'s> { pub pointer: *mut c_void, } +impl<'s> Debug for ExternalReference<'s> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // SAFETY: All union fields are the same size + unsafe { (self.pointer).fmt(f) } + } +} + #[derive(Debug, Clone)] pub struct ExternalReferences { null_terminated: Vec, diff --git a/tests/test_api.rs b/tests/test_api.rs index fd9886ea..47cadd07 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -5309,7 +5309,7 @@ fn external_references() { let external_ptr = Box::into_raw(vec![0_u8, 1, 2, 3, 4].into_boxed_slice()) as *mut [u8] as *mut c_void; // Push them to the external reference table. - let refs = v8::ExternalReferences::new(&[ + let refs = [ v8::ExternalReference { function: fn_callback.map_fn_to(), }, @@ -5319,7 +5319,10 @@ fn external_references() { v8::ExternalReference { pointer: external_ptr, }, - ]); + ]; + // Exercise the Debug impl + println!("{refs:?}"); + let refs = v8::ExternalReferences::new(&refs); // TODO(piscisaureus): leaking the `ExternalReferences` collection shouldn't // be necessary. The reference needs to remain valid for the lifetime of the // `SnapshotCreator` or `Isolate` that uses it, which would be the case here