-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jorge Prendes <[email protected]>
- Loading branch information
Showing
10 changed files
with
172 additions
and
139 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 +1,51 @@ | ||
use std::fs::{File, OpenOptions}; | ||
use std::fs::OpenOptions; | ||
use std::io::Result; | ||
use std::os::fd::OwnedFd; | ||
pub use std::os::fd::{AsRawFd as StdioAsRawFd, RawFd as StdioRawFd}; | ||
use std::os::fd::{IntoRawFd, OwnedFd, RawFd}; | ||
use std::path::Path; | ||
|
||
use crossbeam::atomic::AtomicCell; | ||
pub use libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO}; | ||
|
||
pub fn try_into_fd(f: impl Into<OwnedFd>) -> Result<impl StdioAsRawFd> { | ||
Ok(f.into()) | ||
pub type StdioRawFd = RawFd; | ||
|
||
pub struct StdioOwnedFd(AtomicCell<StdioRawFd>); | ||
|
||
impl Drop for StdioOwnedFd { | ||
fn drop(&mut self) { | ||
let fd = self.0.swap(-1); | ||
if fd >= 0 { | ||
unsafe { libc::close(fd) }; | ||
} | ||
} | ||
} | ||
|
||
impl Default for StdioOwnedFd { | ||
fn default() -> Self { | ||
Self(AtomicCell::new(-1)) | ||
} | ||
} | ||
|
||
pub fn open_file<P: AsRef<Path>>(path: P) -> Result<File> { | ||
OpenOptions::new().read(true).write(true).open(path) | ||
impl StdioOwnedFd { | ||
pub fn try_from(f: impl Into<OwnedFd>) -> Result<Self> { | ||
let fd = f.into().into_raw_fd(); | ||
Ok(unsafe { Self::from_raw_fd(fd) }) | ||
} | ||
|
||
pub unsafe fn from_raw_fd(fd: StdioRawFd) -> Self { | ||
Self(AtomicCell::new(fd)) | ||
} | ||
|
||
pub fn as_raw_fd(&self) -> Option<StdioRawFd> { | ||
let fd = self.0.load(); | ||
(fd >= 0).then_some(fd) | ||
} | ||
|
||
pub fn take(&self) -> Self { | ||
let fd = self.0.swap(-1); | ||
unsafe { Self::from_raw_fd(fd) } | ||
} | ||
|
||
pub fn try_from_path(path: impl AsRef<Path>) -> Result<Self> { | ||
Self::try_from(OpenOptions::new().read(true).write(true).open(path)?) | ||
} | ||
} |
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
Oops, something went wrong.