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 + } + } }