pub struct Fd(/* private fields */);Expand description
An owned file descriptor that closes on drop.
Fd is move-only. Constructing one from a raw descriptor transfers close
ownership to Fd; do not also close the raw descriptor elsewhere.
§Fork Safety
Fd instances created by Core usually have O_CLOEXEC set. If the process
forks, the descriptor will be inherited by the child but will be closed
automatically upon exec. Callers that need a descriptor to survive exec
must clear the flag manually.
Implementations§
Source§impl Fd
impl Fd
Sourcepub unsafe fn from_owned_raw_fd(
fd: RawFd,
op: &'static str,
) -> Result<Self, CoreError>
pub unsafe fn from_owned_raw_fd( fd: RawFd, op: &'static str, ) -> Result<Self, CoreError>
Wrap an owned raw file descriptor.
§Safety
The caller must guarantee fd is valid, open, and uniquely owned by the
returned Fd. Passing a borrowed fd, or closing fd after this call,
can cause double-close or use-after-close bugs.
Sourcepub fn eventfd(init: u32) -> Result<Self, CoreError>
pub fn eventfd(init: u32) -> Result<Self, CoreError>
Create a non-blocking eventfd with EFD_CLOEXEC.
The descriptor is created with FD_CLOEXEC set.
§Errors
EINVAL:initis invalid.EMFILE: Process limit on open file descriptors hit.ENFILE: System-wide limit on open files hit.
Sourcepub fn timerfd() -> Result<Self, CoreError>
pub fn timerfd() -> Result<Self, CoreError>
Create a non-blocking timerfd using CLOCK_MONOTONIC with TFD_CLOEXEC.
The descriptor is created with FD_CLOEXEC set.
§Errors
EMFILE: Process limit on open file descriptors hit.ENFILE: System-wide limit on open files hit.ENOMEM: Insufficient kernel memory.
Sourcepub fn dup2(&self, target: RawFd) -> Result<(), CoreError>
pub fn dup2(&self, target: RawFd) -> Result<(), CoreError>
Perform a dup2 syscall.
§Errors
EBADF: The source or target file descriptor is invalid.EMFILE: The target descriptor exceeds the process limit.
Sourcepub fn set_nonblock(&self) -> Result<(), CoreError>
pub fn set_nonblock(&self) -> Result<(), CoreError>
Sourcepub fn set_cloexec(&self) -> Result<(), CoreError>
pub fn set_cloexec(&self) -> Result<(), CoreError>
Sourcepub fn read_slice(&self, buf: &mut [u8]) -> Result<Option<usize>, CoreError>
pub fn read_slice(&self, buf: &mut [u8]) -> Result<Option<usize>, CoreError>
Read bytes into a mutable slice.
Returns Ok(None) if the operation would block (EAGAIN).
§Edge Cases
- Zero-length read: Returns
Ok(Some(0))immediately. - Partial read: Returns the number of bytes actually read.
§Errors
EBADF: The file descriptor is invalid or not open for reading.EFAULT:bufpoints outside the process’s address space.EIO: Low-level I/O error.
Sourcepub fn seek_set(&self, offset: i64) -> Result<u64, CoreError>
pub fn seek_set(&self, offset: i64) -> Result<u64, CoreError>
Seek to an absolute file offset.
§Errors
EBADF: The file descriptor is not seekable.EINVAL:offsetis invalid.EOVERFLOW: The resulting offset exceeds the off_t range.
Sourcepub fn write_slice(&self, buf: &[u8]) -> Result<Option<usize>, CoreError>
pub fn write_slice(&self, buf: &[u8]) -> Result<Option<usize>, CoreError>
Write bytes from a slice.
Returns Ok(None) if the operation would block (EAGAIN).
§Edge Cases
- Zero-length write: Returns
Ok(Some(0))immediately. - Partial write: Returns the number of bytes actually written.
§Errors
EBADF: The file descriptor is invalid or not open for writing.EFAULT:bufpoints outside the process’s address space.EPIPE: The reading end of a pipe or socket was closed.
Sourcepub fn read_u64(&self) -> Result<Option<u64>, CoreError>
pub fn read_u64(&self) -> Result<Option<u64>, CoreError>
Read a native-endian u64.
Returns Ok(None) if the operation would block (EAGAIN).
Sourcepub fn write_u64(&self, value: u64) -> Result<Option<usize>, CoreError>
pub fn write_u64(&self, value: u64) -> Result<Option<usize>, CoreError>
Write a native-endian u64.
Returns Ok(None) if the operation would block (EAGAIN).
Sourcepub fn set_timer_oneshot(
&self,
delay: Option<Duration>,
) -> Result<(), CoreError>
pub fn set_timer_oneshot( &self, delay: Option<Duration>, ) -> Result<(), CoreError>
Arm or disarm a one-shot timerfd.
Passing None disarms the timer. Zero durations are rounded up to one
nanosecond so the timer still expires.
§Errors
EBADF: The file descriptor is invalid.EINVAL: The duration is invalid or not supported by the kernel.