HandledFd

Struct HandledFd 

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

Create [HandledFd::new(RawFd)] and then add it to EpollHandler::add_fd()

WARNING: Your kernel may or may not have all wanted modes available Consult your kernels epoll.h header to be sure and / or test if needed

Implementations§

Source§

impl HandledFd

Source

pub fn new(fd: RawFd) -> Self

Create a new EpollHandler associated HandledFd Then add via EpollHandler::add_fd()

Examples found in repository?
examples/listener.rs (line 16)
5fn main() {
6    // The 10 denotes power of two capacity to io_uring::IoUring
7    let mut handler = EpollHandler::new(10).expect("Unable to create EPoll Handler");
8
9    // This works with any impl that provides std::os::fd::AsRawFd impl
10    // In POSIX/UNIX-like it's just i32 file number or "fileno"
11    let listen =
12        std::net::TcpListener::bind(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0))
13            .unwrap();
14
15    // Add the listen handle into EpollHandler
16    let mut handle_fd = HandledFd::new(listen.as_raw_fd());
17    let set_mask = handle_fd.set_in(true);
18    assert_eq!(set_mask, 1);
19    handler.add_fd(&handle_fd).unwrap();
20
21    // Prepare a commit all changes into io_uring::SubmissionQueue
22    let handle_status = handler.prepare_submit().unwrap();
23    assert_eq!(handle_status.count_new(), 1);
24    assert_eq!(handle_status.count_changes(), 0);
25    assert_eq!(handle_status.count_empty(), 0);
26    assert_eq!(handle_status.errors().len(), 0);
27
28    // Take temp ref to io_uring::SubmissionQeueue
29    let submission = handler.io_uring().submission();
30    assert_eq!(submission.len(), 1);
31    assert_eq!(submission.is_empty(), false);
32    assert_eq!(submission.dropped(), 0);
33    assert_eq!(submission.cq_overflow(), false);
34    assert_eq!(submission.is_full(), false);
35    drop(submission);
36
37    // async version is with submit()
38    handler.submit_and_wait(1).unwrap();
39
40    // Ensure that the kernel ate it
41    let submission = handler.io_uring().submission();
42    assert_eq!(submission.len(), 0);
43    assert_eq!(submission.is_empty(), true);
44    assert_eq!(submission.dropped(), 0);
45    assert_eq!(submission.cq_overflow(), false);
46    assert_eq!(submission.is_full(), false);
47    drop(submission);
48
49    let c_queue = handler.io_uring().completion();
50    let mut c_attempts = 0;
51    loop {
52        if c_queue.is_empty() == false {
53            assert_eq!(c_queue.len(), 1);
54            break;
55        }
56        if c_attempts == 10 {
57            panic!("Took more than 100 ms - completion never finished?");
58        }
59        std::thread::sleep(std::time::Duration::from_millis(10));
60        c_attempts += 1;
61    }
62}
Source

pub fn as_raw(&self) -> RawFd

Extract RawFd

Source

pub fn set_in(&mut self, on_or_off: bool) -> i32

Set EPOLLIN per epoll.h in userspace On or Off Returns returns raw mask as to be sent to kernel Use EpollHandler::prepare_submit() after

Examples found in repository?
examples/listener.rs (line 17)
5fn main() {
6    // The 10 denotes power of two capacity to io_uring::IoUring
7    let mut handler = EpollHandler::new(10).expect("Unable to create EPoll Handler");
8
9    // This works with any impl that provides std::os::fd::AsRawFd impl
10    // In POSIX/UNIX-like it's just i32 file number or "fileno"
11    let listen =
12        std::net::TcpListener::bind(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0))
13            .unwrap();
14
15    // Add the listen handle into EpollHandler
16    let mut handle_fd = HandledFd::new(listen.as_raw_fd());
17    let set_mask = handle_fd.set_in(true);
18    assert_eq!(set_mask, 1);
19    handler.add_fd(&handle_fd).unwrap();
20
21    // Prepare a commit all changes into io_uring::SubmissionQueue
22    let handle_status = handler.prepare_submit().unwrap();
23    assert_eq!(handle_status.count_new(), 1);
24    assert_eq!(handle_status.count_changes(), 0);
25    assert_eq!(handle_status.count_empty(), 0);
26    assert_eq!(handle_status.errors().len(), 0);
27
28    // Take temp ref to io_uring::SubmissionQeueue
29    let submission = handler.io_uring().submission();
30    assert_eq!(submission.len(), 1);
31    assert_eq!(submission.is_empty(), false);
32    assert_eq!(submission.dropped(), 0);
33    assert_eq!(submission.cq_overflow(), false);
34    assert_eq!(submission.is_full(), false);
35    drop(submission);
36
37    // async version is with submit()
38    handler.submit_and_wait(1).unwrap();
39
40    // Ensure that the kernel ate it
41    let submission = handler.io_uring().submission();
42    assert_eq!(submission.len(), 0);
43    assert_eq!(submission.is_empty(), true);
44    assert_eq!(submission.dropped(), 0);
45    assert_eq!(submission.cq_overflow(), false);
46    assert_eq!(submission.is_full(), false);
47    drop(submission);
48
49    let c_queue = handler.io_uring().completion();
50    let mut c_attempts = 0;
51    loop {
52        if c_queue.is_empty() == false {
53            assert_eq!(c_queue.len(), 1);
54            break;
55        }
56        if c_attempts == 10 {
57            panic!("Took more than 100 ms - completion never finished?");
58        }
59        std::thread::sleep(std::time::Duration::from_millis(10));
60        c_attempts += 1;
61    }
62}
Source

pub fn set_pri(&mut self, on_or_off: bool) -> i32

EPOLLPRI

Source

pub fn set_out(&mut self, on_or_off: bool) -> i32

EPOLLOUT

Source

pub fn set_err(&mut self, on_or_off: bool) -> i32

EPOLLERR

Source

pub fn set_hup(&mut self, on_or_off: bool) -> i32

EPOLLHUP

Source

pub fn set_rdnorm(&mut self, on_or_off: bool) -> i32

EPOLLRDNORM

Source

pub fn set_rdband(&mut self, on_or_off: bool) -> i32

EPOLLRDBAND

Source

pub fn set_wrnorm(&mut self, on_or_off: bool) -> i32

EPOLLWRNORM

Source

pub fn set_wrband(&mut self, on_or_off: bool) -> i32

EPOLLWRBAND per epoll.h userspace On or Off

Source

pub fn set_msg(&mut self, on_or_off: bool) -> i32

EPOLLMSG

Source

pub fn set_rdhup(&mut self, on_or_off: bool) -> i32

EPOLLRDHUP

Source

pub fn set_wakeup(&mut self, on_or_off: bool) -> i32

EPOLLWAKEUP

Source

pub fn set_oneshot(&mut self, on_or_off: bool) -> i32

EPOLLONESHOT

Source

pub fn set_et(&mut self, on_or_off: bool) -> i32

EPOLLET

Source

pub fn get_mask_raw(&mut self) -> Option<i32>

Get the raw u32 Epoll event mask as set in userspace This may not have been sent and may be pending send or not committed Use EpollHandler::prepare_submit() after

Source

pub fn set_mask_raw(&mut self, mask: i32)

Set the raw u32 Epoll event mask in the userspace WARNING: Ensure this is valid per epoll.h of your kernel Use EpollHandler::prepare_submit() after

Source

pub fn get_pending(&self) -> Option<i32>

Get the pending eq u32 Epoll This may not be committed into kernel yet use get_committed to check This will be none if there is no pending change or it has not been sent Use EpollHandler::prepare_submit() after

Trait Implementations§

Source§

impl Clone for HandledFd

Source§

fn clone(&self) -> HandledFd

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HandledFd

Source§

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

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

impl PartialEq for HandledFd

Source§

fn eq(&self, other: &HandledFd) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for HandledFd

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.