Struct mio_st::unix::EventedFd

source ·
pub struct EventedFd<'a>(pub &'a RawFd);
Expand description

Adapter for a RawFd providing an Evented implementation.

EventedFd enables registering any type with an file descriptor with Poller.

While only implementations for TCP and UDP are provided, Mio supports registering any file descriptor that can be registered with the underlying OS selector. EventedFd provides the necessary bridge.

Note that EventedFd takes a reference to a RawFd. This is because EventedFd does not take ownership of the file descriptor. Specifically, it will not manage any lifecycle related operations, such as closing the file descriptor on drop. It is expected that the EventedFd is constructed right before a call to Poller.register. See the examples below for more detail.

For a owned, or managed, type see EventedIo.

Deregistering

The file descriptor doesn’t need to be deregistered iff the file descriptor is unique (i.e. it is not duplicated via dup(2)) and will be deregistered when it is closed.

Examples

Basic usage

use std::net::TcpListener;
use std::os::unix::io::AsRawFd;

use mio_st::event::{Evented, EventedId, Ready};
use mio_st::poll::{Interests, PollOption, Poller};
use mio_st::unix::EventedFd;

// Bind a listener from the standard library.
let listener = TcpListener::bind("127.0.0.1:0")?;

let mut poller = Poller::new()?;

// Register the listener using `EventedFd`.
poller.register(&mut EventedFd(&listener.as_raw_fd()), EventedId(0), Interests::READABLE, PollOption::Edge)?;

Implementing Evented for a custom type backed by a RawFd.

use std::io;
use std::os::unix::io::RawFd;

use mio_st::event::{Evented, EventedId, Ready};
use mio_st::poll::{Interests, PollOption, Poller};
use mio_st::unix::EventedFd;

pub struct MyIo {
    fd: RawFd,
}

impl Evented for MyIo {
    fn register(&mut self, poller: &mut Poller, id: EventedId, interests: Interests, opt: PollOption) -> io::Result<()> {
        EventedFd(&self.fd).register(poller, id, interests, opt)
    }

    fn reregister(&mut self, poller: &mut Poller, id: EventedId, interests: Interests, opt: PollOption) -> io::Result<()> {
        EventedFd(&self.fd).reregister(poller, id, interests, opt)
    }

    fn deregister(&mut self, poller: &mut Poller) -> io::Result<()> {
        EventedFd(&self.fd).deregister(poller)
    }
}

Tuple Fields§

§0: &'a RawFd

Trait Implementations§

Formats the value using the given formatter. Read more
Register self with the given Poller instance. Read more
Reregister self with the given Poller instance. Read more
Deregister self from the given Poller instance Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.