diff --git a/src/clircle_unix.rs b/src/clircle_unix.rs index 101f208..56448b9 100644 --- a/src/clircle_unix.rs +++ b/src/clircle_unix.rs @@ -4,7 +4,7 @@ use std::convert::TryFrom; use std::fs::File; use std::io::{self, Seek}; use std::os::unix::fs::MetadataExt; -use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd}; +use std::os::unix::io::{FromRawFd, IntoRawFd, OwnedFd, RawFd}; use std::{cmp, hash, ops}; /// Re-export of libc @@ -53,6 +53,13 @@ impl UnixIdentifier { ident }) } + + fn try_from_owned_fd(fd: OwnedFd) -> io::Result { + Self::try_from(File::from(fd)).map(|mut ident| { + ident.owns_fd = true; + ident + }) + } } impl Clircle for UnixIdentifier { @@ -135,6 +142,7 @@ mod tests { use std::error::Error; use std::io::Write; + use std::os::unix::io::AsRawFd; use nix::pty::{openpty, OpenptyResult}; use nix::unistd::close; @@ -186,7 +194,7 @@ mod tests { #[test] fn test_pty_equal_but_not_conflicting() -> Result<(), &'static str> { let OpenptyResult { master, slave } = openpty(None, None).expect("Could not open pty."); - let res = unsafe { UnixIdentifier::try_from_raw_fd(slave, false) } + let res = unsafe { UnixIdentifier::try_from_raw_fd(slave.as_raw_fd(), false) } .map_err(|_| "Error creating UnixIdentifier from pty fd") .and_then(|ident| { if !ident.eq(&ident) { @@ -196,7 +204,7 @@ mod tests { return Err("pty fd does not conflict with itself, but conflict detected"); } - let second_ident = unsafe { UnixIdentifier::try_from_raw_fd(slave, false) } + let second_ident = UnixIdentifier::try_from_owned_fd(slave) .map_err(|_| "Error creating second Identifier to pty")?; if !ident.eq(&second_ident) { return Err("ident != second_ident"); @@ -209,12 +217,6 @@ mod tests { Ok(()) }); - let r1 = close(master); - let r2 = close(slave); - - r1.expect("Error closing master end of pty"); - r2.expect("Error closing slave end of pty"); - res } }