Skip to content

Commit

Permalink
Improve contributor friendliness
Browse files Browse the repository at this point in the history
Add documentation for internal items
Add doc recipe to Justfile
Move getting OwnedHandle logic into FastClose::get_handle
Minor public documentation tweaks
  • Loading branch information
alpha-tango-kilo committed Nov 10, 2023
1 parent db38948 commit 8a3841a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
5 changes: 5 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ alias c := clippy
alias check := clippy
alias t := test
alias fmt := format
alias d := doc

@_default:
echo "Using this Justfile for clippy/test requires cargo-hack & the"
Expand Down Expand Up @@ -33,3 +34,7 @@ test:
# Run nightly rustfmt
format:
cargo +nightly fmt

# Build internal documentation for this crate
doc:
cargo doc --open --document-private-items
42 changes: 32 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![deny(clippy::undocumented_unsafe_blocks)]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(unsafe_op_in_unsafe_fn)]
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]

Expand Down Expand Up @@ -32,6 +33,7 @@ pub use windows::*;

pub mod fs;

/// The Windows implementation of [`FastClose`]
#[cfg(windows)]
mod windows {
#[cfg(feature = "backend-threadpool")]
Expand All @@ -46,6 +48,7 @@ mod windows {
#[cfg(feature = "backend-threadpool")]
use threadpool::{Builder as ThreadPoolBuilder, ThreadPool};

/// A lazily initialised [`ThreadPool`] to send handle closures to
#[cfg(feature = "backend-threadpool")]
static CLOSER_POOL: OnceLock<ThreadPool> = OnceLock::new();

Expand All @@ -61,11 +64,23 @@ mod windows {
/// Creates a new fast-closing file handle
///
/// You may find it more convenient to use
/// [FastCloseable::fast_close()](crate::FastCloseable::fast_close)
/// [`FastCloseable::fast_close()`](crate::FastCloseable::fast_close)
#[inline(always)]
pub fn new(handle: H) -> Self {
FastClose(ManuallyDrop::new(handle))
}

/// Gets the interal [`OwnedHandle`]
///
/// # Safety
///
/// `self.0` must never be accessed again.
/// This method should only be called on drop
#[inline]
unsafe fn get_handle(&mut self) -> OwnedHandle {
// SAFETY: relies on self.0 never being accessed again
unsafe { ManuallyDrop::take(&mut self.0) }.into()
}
}

impl<H> Drop for FastClose<H>
Expand All @@ -81,7 +96,7 @@ mod windows {
let closer_pool =
CLOSER_POOL.get_or_init(|| ThreadPoolBuilder::new().build());
// SAFETY: we're in Drop, so self.0 won't be accessed again
let handle = unsafe { ManuallyDrop::take(&mut self.0) }.into();
let handle = unsafe { self.get_handle() };
closer_pool.execute(move || drop(handle));
}

Expand All @@ -93,7 +108,7 @@ mod windows {
#[cfg(feature = "backend-rayon")]
fn drop(&mut self) {
// SAFETY: we're in Drop, so self.0 won't be accessed again
let handle = unsafe { ManuallyDrop::take(&mut self.0) }.into();
let handle = unsafe { self.get_handle() };
rayon::spawn(move || drop(handle));
}

Expand All @@ -105,7 +120,7 @@ mod windows {
#[cfg(feature = "backend-async-std")]
fn drop(&mut self) {
// SAFETY: we're in Drop, so self.0 won't be accessed again
let handle = unsafe { ManuallyDrop::take(&mut self.0) }.into();
let handle = unsafe { self.get_handle() };
async_std::task::spawn(async move { drop(handle) });
}

Expand All @@ -117,7 +132,7 @@ mod windows {
#[cfg(feature = "backend-smol")]
fn drop(&mut self) {
// SAFETY: we're in Drop, so self.0 won't be accessed again
let handle = unsafe { ManuallyDrop::take(&mut self.0) }.into();
let handle = unsafe { self.get_handle() };
smol::spawn(async move { drop(handle) }).detach();
}
}
Expand Down Expand Up @@ -155,6 +170,7 @@ mod windows {
}
}

/// The non-Windows stub implementation of [`FastClose`]
#[cfg(not(windows))]
mod stub {
use std::os::fd::OwnedFd;
Expand All @@ -170,7 +186,8 @@ mod stub {
/// Creates a new fast-closing file handle
///
/// You may find it more convenient to use
/// [FastCloseable::fast_close()](crate::FastCloseable::fast_close)
/// [`FastCloseable::fast_close()`](crate::FastCloseable::fast_close)
#[inline(always)]
pub fn new(handle: H) -> Self {
FastClose(handle)
}
Expand All @@ -180,15 +197,18 @@ mod stub {
where
H: Into<OwnedFd> + ?Sized,
{
/// Submits the file handle to a thread pool to handle its closure
/// Submits the file handle to your chosen backend to handle its closure
///
/// Note: on non-Windows targets, nothing is done, the handle is just
/// dropped normally
fn drop(&mut self) {}
}
}

// Blanket impls that work for stub and non-stub go here
/// Writes blanket trait implementations for [`FastClose`]
///
/// Takes the path of the type the `FastClose`'s inner must convert to, i.e.
/// `OwnedHandle` on Windows
macro_rules! blanket_impls {
($handle_type:path) => {
impl<H> Deref for FastClose<H>
Expand Down Expand Up @@ -324,8 +344,10 @@ blanket_impls!(std::os::windows::io::OwnedHandle);
#[cfg(not(windows))]
blanket_impls!(std::os::fd::OwnedFd);

// Convenience helpers

/// Generates the convenience [`FastCloseable`] trait
///
/// Takes the path of the type the `FastClose`'s inner must convert to, i.e.
/// `OwnedHandle` on Windows
macro_rules! fast_closeable {
($handle_type:path) => {
/// Provides a convenience method to chain with that wraps a file handle
Expand Down

0 comments on commit 8a3841a

Please sign in to comment.