Skip to content

Commit

Permalink
Change Debug repr of FastClose on Windows
Browse files Browse the repository at this point in the history
Matches non-Windows and hides implementation detail
Add test
  • Loading branch information
alpha-tango-kilo committed Nov 6, 2023
1 parent 65c7ea3 commit b013323
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ pub mod fs;
#[cfg(windows)]
mod windows {
pub use std::os::windows::io::OwnedHandle;
use std::{io, mem::ManuallyDrop, os::windows::prelude::*, sync::OnceLock};
use std::{
fmt, io, mem::ManuallyDrop, ops::Deref, os::windows::prelude::*,
sync::OnceLock,
};

use threadpool::{Builder as ThreadPoolBuilder, ThreadPool};

static CLOSER_POOL: OnceLock<ThreadPool> = OnceLock::new();

/// A zero-sized wrapper that moves a file handle to a thread pool on drop
#[derive(Debug)]
pub struct FastClose<H: Into<OwnedHandle> + ?Sized>(
pub(super) ManuallyDrop<H>,
);
Expand Down Expand Up @@ -71,6 +73,15 @@ mod windows {
}
}

impl<H> fmt::Debug for FastClose<H>
where
H: fmt::Debug + Into<OwnedHandle>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("FastClose").field(&self.0.deref()).finish()
}
}

// Windows-only blanket impls
impl<H> AsHandle for FastClose<H>
where
Expand Down Expand Up @@ -310,4 +321,31 @@ mod tests {
"FastClose is not a ZST"
);
}

#[test]
fn debug_repr_hides_manually_drop() {
let file = FastClose::new(File::open("Cargo.toml").unwrap());

let debug = format!("{file:?}");
println!("Debug: {debug}");
assert!(
!debug.contains("ManuallyDrop"),
"Debug should hide implementation details"
);
assert!(
debug.contains("File"),
"Debug (pretty) should show inner type"
);

let debug_pretty = format!("{file:#?}");
println!("Pretty debug: {debug_pretty}");
assert!(
!debug_pretty.contains("ManuallyDrop"),
"Debug (pretty) should hide implementation details"
);
assert!(
debug_pretty.contains("File"),
"Debug should show inner type"
);
}
}

0 comments on commit b013323

Please sign in to comment.