Epoll

Struct Epoll 

Source
pub struct Epoll { /* private fields */ }
Expand description

Rust abstraction atop linux’s epoll interface. Wrapper type around an epoll file descriptor. Performs proper cleanup on drop.

use std::{time::Duration, fs::File};
use std::os::unix::io::AsRawFd;
use epoll_rs::{Epoll, Opts, EpollEvent};

let mut epoll = Epoll::new()?;
let file = File::open("/")?;
let token = epoll.add(file, Opts::IN)?;
// add other files...
let mut buf = [EpollEvent::zeroed(); 10];
let events = epoll.wait_timeout(&mut buf, Duration::from_millis(50))?;
for event in events {
    if token.fd() == event.fd() {
        println!("File ready for reading");
    } else {
        println!("File not ready for reading");
    }
}
epoll.remove(token); // this cleanup is performed when epoll goes out of scope

Implementations§

Source§

impl Epoll

Source

pub fn new() -> Result<Self>

Source

pub fn add<'a, 'b: 'a, F: OwnedRawFd>( &'b self, file: F, opts: Opts, ) -> Result<Token<'a, F>>

Add a file-like struct to the epoll instance

The returned token can be ignored if you don’t need to distinguish which file is ready, but dropping the token closes the added file

Source

pub fn remove<'a, 'b: 'a, F: OwnedRawFd>( &'b self, token: Token<'a, F>, ) -> Result<F>

Remove a previously added file-like struct from this epoll instance

No new events will be deilvered referring to this token even if the event occurs before removal. Consumes the token, returning the contained file, since the file it is associated with is no longer in this epoll instance

Source

pub fn modify<'a, 'b: 'a, F: OwnedRawFd>( &'b self, token: &'a Token<'a, F>, opts: Opts, ) -> Result<()>

Change the Opts of a previously added file-like struct

Source

pub fn wait<'a, 'b>( &'a self, buf: &'b mut [EpollEvent], ) -> Result<&'b mut [EpollEvent]>

Wait indefinetly until at least one event and at most buf.len() events occur.

If passed a zero length buf, this function will return Err

Source

pub fn wait_timeout<'a, 'b>( &'a self, buf: &'b mut [EpollEvent], timeout: Duration, ) -> Result<&'b mut [EpollEvent]>

Wait until at least one event and at most buf.len() events occur or timeout expires.

If passed a zero length buf, this function will return Err

Source

pub fn wait_one(&self) -> Result<EpollEvent>

Wait indefinetly for one event.

Source

pub fn wait_one_timeout(&self, timeout: Duration) -> Result<Option<EpollEvent>>

Wait for one event or until timeout expires.

Return value of Ok(None) indicates timeout expired

Source

pub fn close(&mut self) -> Result<()>

Manually close this epoll instance, handling any potential errors

Same as drop, only this lets the user deal with errors in closing. The invariants of this library should mean that close never fails, but those invariants can be broken with unsafe code.

Source

pub unsafe fn add_raw_fd<'a, 'b: 'a, F: OwnedRawFd>( &'b self, fd: RawFd, opts: Opts, ) -> Result<Token<'a, F>>

Adds a RawFd to an epoll instance directly

This is pretty unsafe, prefer add

§Safety

fd must refer to a currently open, unowned, file descriptor of type F. If fd refers to a file that is dropped or the fd is closed before this Epoll instance is, later use of the returned Token may modify a different file, since file descriptors (RawFds) are reused. The following is notably io unsound

use epoll_rs::{Epoll, Opts, Token};
use std::{fs::File, io, os::unix::io::{FromRawFd, AsRawFd}};
let mut epoll = Epoll::new()?;
{
    let stdin = unsafe{File::from_raw_fd(1)};
    let token: Token<File> = unsafe {
        epoll.add_raw_fd(stdin.as_raw_fd(), Opts::IN)?
    };
} // stdin dropped here, fd 1 `libc::close`d, invariants violated

instead use into_raw_fd to get an unowned RawFd

use epoll_rs::{Epoll, Opts, Token};
use std::{fs::File, io, os::unix::io::{FromRawFd, AsRawFd, IntoRawFd}};
let mut epoll = Epoll::new()?;
{
    let stdin = unsafe{File::from_raw_fd(1)};
    let token: Token<File> = unsafe {
        epoll.add_raw_fd(stdin.into_raw_fd(), Opts::IN)?
    };
} // stdin was consumed by into_raw_fd(), so it's drop code won't be run

Trait Implementations§

Source§

impl AsRawFd for Epoll

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl Debug for Epoll

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Epoll

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl FromRawFd for Epoll

Source§

unsafe fn from_raw_fd(fd: RawFd) -> Self

Safety: super unsafe. Use only if fd came from this library. Otherwise, you need to make sure all fds added to this epoll have epoll_data that contains their own fd, that all added fds are open and that fd is open and refers to an epoll file description.

Source§

impl IntoRawFd for Epoll

Source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more
Source§

impl OwnedRawFd for Epoll

Auto Trait Implementations§

§

impl Freeze for Epoll

§

impl RefUnwindSafe for Epoll

§

impl Send for Epoll

§

impl Sync for Epoll

§

impl Unpin for Epoll

§

impl UnwindSafe for Epoll

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.