Skip to content

Commit

Permalink
feat: add unsafe fn from_owned_fd() for owne fd wrapper types (#2566)
Browse files Browse the repository at this point in the history
* feat: add unsafe fn from_owned_fd() for owne fd wrapper types

* chore: changelog
  • Loading branch information
SteveLauC authored Dec 18, 2024
1 parent 4033e06 commit 5ec9990
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/2566.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `from_owned_fd` constructor to `PtyMaster/Fanotify/Inotify/SignalFd/TimerFd`
11 changes: 11 additions & 0 deletions src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ pub enum ForkptyResult {
#[derive(Debug)]
pub struct PtyMaster(OwnedFd);

impl PtyMaster {
/// Constructs a `PytMaster` wrapping an existing `OwnedFd`.
///
/// # Safety
///
/// `OwnedFd` is a valid `PtyMaster`.
pub unsafe fn from_owned_fd(fd: OwnedFd) -> Self {
Self(fd)
}
}

impl AsRawFd for PtyMaster {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
Expand Down
13 changes: 13 additions & 0 deletions src/sys/fanotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,17 @@ impl From<Fanotify> for OwnedFd {
fn from(value: Fanotify) -> Self {
value.fd
}
}

impl Fanotify {
/// Constructs a `Fanotify` wrapping an existing `OwnedFd`.
///
/// # Safety
///
/// `OwnedFd` is a valid `Fanotify`.
pub unsafe fn from_owned_fd(fd: OwnedFd) -> Self {
Self {
fd
}
}
}
11 changes: 11 additions & 0 deletions src/sys/inotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ impl Inotify {

Ok(events)
}

/// Constructs an `Inotify` wrapping an existing `OwnedFd`.
///
/// # Safety
///
/// `OwnedFd` is a valid `Inotify`.
pub unsafe fn from_owned_fd(fd: OwnedFd) -> Self {
Self {
fd
}
}
}

impl FromRawFd for Inotify {
Expand Down
9 changes: 9 additions & 0 deletions src/sys/signalfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ impl SignalFd {
}
}

/// Constructs a `SignalFd` wrapping an existing `OwnedFd`.
///
/// # Safety
///
/// `OwnedFd` is a valid `SignalFd`.
pub unsafe fn from_owned_fd(fd: OwnedFd) -> Self {
Self(fd)
}

fn update(&self, mask: &SigSet, flags: SfdFlags) -> Result<()> {
let raw_fd = self.0.as_raw_fd();
unsafe {
Expand Down
12 changes: 12 additions & 0 deletions src/sys/timerfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,16 @@ impl TimerFd {

Ok(())
}


/// Constructs a `TimerFd` wrapping an existing `OwnedFd`.
///
/// # Safety
///
/// `OwnedFd` is a valid `TimerFd`.
pub unsafe fn from_owned_fd(fd: OwnedFd) -> Self {
Self {
fd
}
}
}

0 comments on commit 5ec9990

Please sign in to comment.