-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also, since `RawOsHandle` on *nix doesn't need interior mutability wrt the inner raw file descriptor, we can safely swap the `RawFd` for `File` instance.
- Loading branch information
Showing
15 changed files
with
58 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,40 @@ | ||
use crate::sys::AsFile; | ||
use std::cell::Cell; | ||
use std::fs::File; | ||
use std::io; | ||
use std::mem::ManuallyDrop; | ||
use std::os::unix::prelude::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct OsHandle(Cell<RawFd>); | ||
pub(crate) struct RawOsHandle(File); | ||
|
||
impl OsHandle { | ||
impl RawOsHandle { | ||
/// Tries clone `self`. | ||
pub(crate) fn try_clone(&self) -> io::Result<Self> { | ||
let fd = self.as_file()?.try_clone()?; | ||
Ok(Self(Cell::new(fd.into_raw_fd()))) | ||
let fd = self.0.try_clone()?; | ||
Ok(unsafe { Self::from_raw_fd(fd.into_raw_fd()) }) | ||
} | ||
/// Consumes `other` taking the ownership of the underlying | ||
/// `RawFd` file descriptor. | ||
/// | ||
/// Note that the state of `Dir` stream pointer *will* not be carried | ||
/// across from `other` to `self`. | ||
pub(crate) fn update_from(&self, other: Self) { | ||
let new_fd = other.into_raw_fd(); | ||
let old_fd = self.0.get(); | ||
self.0.set(new_fd); | ||
// We need to remember to close the old_fd. | ||
unsafe { | ||
File::from_raw_fd(old_fd); | ||
} | ||
pub(crate) fn update_from(&self, _other: Self) { | ||
panic!("RawOsHandle::update_from should never be issued on Unix!") | ||
} | ||
} | ||
|
||
impl Drop for OsHandle { | ||
fn drop(&mut self) { | ||
unsafe { | ||
File::from_raw_fd(self.as_raw_fd()); | ||
} | ||
} | ||
} | ||
|
||
impl AsRawFd for OsHandle { | ||
impl AsRawFd for RawOsHandle { | ||
fn as_raw_fd(&self) -> RawFd { | ||
self.0.get() | ||
self.0.as_raw_fd() | ||
} | ||
} | ||
|
||
impl FromRawFd for OsHandle { | ||
unsafe fn from_raw_fd(fd: RawFd) -> Self { | ||
Self(Cell::new(fd)) | ||
impl IntoRawFd for RawOsHandle { | ||
fn into_raw_fd(self) -> RawFd { | ||
self.0.into_raw_fd() | ||
} | ||
} | ||
|
||
impl IntoRawFd for OsHandle { | ||
fn into_raw_fd(self) -> RawFd { | ||
// We need to prevent dropping of self | ||
let wrapped = ManuallyDrop::new(self); | ||
wrapped.0.get() | ||
impl FromRawFd for RawOsHandle { | ||
unsafe fn from_raw_fd(raw: RawFd) -> Self { | ||
Self(File::from_raw_fd(raw)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters