dope-sys 0.78.0

Typed operating-system call contracts
Documentation
use crate::{
    Errno,
    io::poll::Entry,
    kernel::{self, errno},
};

/// # Safety
/// The returned slice length must satisfy [`Entry::is_valid_len`].
pub unsafe trait Entries<'fd> {
    fn entries(&mut self) -> &mut [Entry<'fd>];
}

// SAFETY: coercing this array to a slice retains its exact N-element extent.
unsafe impl<'fd, const N: usize> Entries<'fd> for [Entry<'fd>; N] {
    fn entries(&mut self) -> &mut [Entry<'fd>] {
        const { assert!(Entry::is_valid_len(N)) };
        self
    }
}

pub(super) fn wait<S: errno::Source + ?Sized, E: From<Errno>>(
    entries: &mut [Entry<'_>],
    timeout: i32,
) -> Result<usize, E> {
    // SAFETY: Entry is transparent over pollfd. The caller admitted the count and
    // the exclusive slice retains every registration for the native call.
    let result = unsafe { libc::poll(entries.as_mut_ptr().cast(), entries.len() as _, timeout) };
    // SAFETY: poll returns -1 or the number of registrations reporting events.
    Ok(unsafe { kernel::raw::Status::new(result).try_count::<S, E>() }?.get())
}