cloneable-file 0.1.1

Cloneable file descriptor
Documentation
//! OS-independent utility to obtain a file handle from [`std::fs::File`]
//! instance.

#[cfg(not(target_family = "windows"))]
mod unix {
    use std::{
        fs::File,
        os::fd::{AsRawFd, RawFd},
    };

    pub(crate) type PortableFileHandle = RawFd;

    pub(crate) fn portable_file_handle(file: &File) -> PortableFileHandle {
        file.as_raw_fd()
    }
}

#[cfg(target_family = "windows")]
mod windows {
    use std::{
        fs::File,
        os::windows::io::{AsRawHandle, RawHandle},
    };

    pub(crate) type PortableFileHandle = RawHandle;

    pub(crate) fn portable_file_handle(file: &File) -> PortableFileHandle {
        file.as_raw_handle()
    }
}

#[cfg(not(target_family = "windows"))]
pub(crate) use unix::{portable_file_handle, PortableFileHandle};

#[cfg(target_family = "windows")]
pub(crate) use windows::{portable_file_handle, PortableFileHandle};

#[cfg(test)]
mod tests {
    use std::{cmp::Ordering, io};

    use super::*;
    #[test]
    fn smoke_test() -> io::Result<()> {
        let f1 = tempfile::tempfile()?;
        let f2 = tempfile::tempfile()?;

        let fd1 = portable_file_handle(&f1);
        let fd2 = portable_file_handle(&f2);

        assert!(matches!(fd1.cmp(&fd2), Ordering::Greater | Ordering::Less));
        Ok(())
    }
}