From 5ec9990ee756302d00d2d3315489af8035ec6573 Mon Sep 17 00:00:00 2001 From: SteveLauC Date: Wed, 18 Dec 2024 10:59:22 +0800 Subject: [PATCH] feat: add unsafe fn from_owned_fd() for owne fd wrapper types (#2566) * feat: add unsafe fn from_owned_fd() for owne fd wrapper types * chore: changelog --- changelog/2566.added.md | 1 + src/pty.rs | 11 +++++++++++ src/sys/fanotify.rs | 13 +++++++++++++ src/sys/inotify.rs | 11 +++++++++++ src/sys/signalfd.rs | 9 +++++++++ src/sys/timerfd.rs | 12 ++++++++++++ 6 files changed, 57 insertions(+) create mode 100644 changelog/2566.added.md diff --git a/changelog/2566.added.md b/changelog/2566.added.md new file mode 100644 index 0000000000..a1b4d2fd83 --- /dev/null +++ b/changelog/2566.added.md @@ -0,0 +1 @@ +Added `from_owned_fd` constructor to `PtyMaster/Fanotify/Inotify/SignalFd/TimerFd` \ No newline at end of file diff --git a/src/pty.rs b/src/pty.rs index 5211d16075..a41f817594 100644 --- a/src/pty.rs +++ b/src/pty.rs @@ -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() diff --git a/src/sys/fanotify.rs b/src/sys/fanotify.rs index cf07fdb719..c2bde9a841 100644 --- a/src/sys/fanotify.rs +++ b/src/sys/fanotify.rs @@ -423,4 +423,17 @@ impl From 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 + } + } } \ No newline at end of file diff --git a/src/sys/inotify.rs b/src/sys/inotify.rs index 0a9abf39e6..3a00ea0d1d 100644 --- a/src/sys/inotify.rs +++ b/src/sys/inotify.rs @@ -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 { diff --git a/src/sys/signalfd.rs b/src/sys/signalfd.rs index 61604f0552..3614881252 100644 --- a/src/sys/signalfd.rs +++ b/src/sys/signalfd.rs @@ -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 { diff --git a/src/sys/timerfd.rs b/src/sys/timerfd.rs index 21e2c4aa10..92ebc29d76 100644 --- a/src/sys/timerfd.rs +++ b/src/sys/timerfd.rs @@ -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 + } + } }