Struct mio::Registration[][src]

pub struct Registration { /* fields omitted */ }
Expand description

Handle to a user space Poll registration.

Registration allows implementing Evented for types that cannot work with the system selector. A Registration is always paired with a SetReadiness, which allows updating the registration’s readiness state. When set_readiness is called and the Registration is associated with a Poll instance, a readiness event will be created and eventually returned by poll.

A Registration / SetReadiness pair is created by calling Registration::new2. At this point, the registration is not being monitored by a Poll instance, so calls to set_readiness will not result in any readiness notifications.

Registration implements Evented, so it can be used with Poll using the same register, reregister, and deregister functions used with TCP, UDP, etc… types. Once registered with Poll, readiness state changes result in readiness events being dispatched to the Poll instance with which Registration is registered.

Note, before using Registration be sure to read the set_readiness documentation and the portability notes. The guarantees offered by Registration may be weaker than expected.

For high level documentation, see Poll.

Examples

use mio::{Ready, Registration, Poll, PollOpt, Token};
use mio::event::Evented;

use std::io;
use std::time::Instant;
use std::thread;

pub struct Deadline {
    when: Instant,
    registration: Registration,
}

impl Deadline {
    pub fn new(when: Instant) -> Deadline {
        let (registration, set_readiness) = Registration::new2();

        thread::spawn(move || {
            let now = Instant::now();

            if now < when {
                thread::sleep(when - now);
            }

            set_readiness.set_readiness(Ready::readable());
        });

        Deadline {
            when: when,
            registration: registration,
        }
    }

    pub fn is_elapsed(&self) -> bool {
        Instant::now() >= self.when
    }
}

impl Evented for Deadline {
    fn register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt)
        -> io::Result<()>
    {
        self.registration.register(poll, token, interest, opts)
    }

    fn reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt)
        -> io::Result<()>
    {
        self.registration.reregister(poll, token, interest, opts)
    }

    fn deregister(&self, poll: &Poll) -> io::Result<()> {
        poll.deregister(&self.registration)
    }
}

Implementations

impl Registration[src]

pub fn new2() -> (Registration, SetReadiness)[src]

Create and return a new Registration and the associated SetReadiness.

See struct documentation for more detail and Poll for high level documentation on polling.

Examples

use mio::{Events, Ready, Registration, Poll, PollOpt, Token};
use std::thread;

let (registration, set_readiness) = Registration::new2();

thread::spawn(move || {
    use std::time::Duration;
    thread::sleep(Duration::from_millis(500));

    set_readiness.set_readiness(Ready::readable());
});

let poll = Poll::new()?;
poll.register(&registration, Token(0), Ready::readable() | Ready::writable(), PollOpt::edge())?;

let mut events = Events::with_capacity(256);

loop {
    poll.poll(&mut events, None);

    for event in &events {
        if event.token() == Token(0) && event.readiness().is_readable() {
            return Ok(());
        }
    }
}

Trait Implementations

impl Debug for Registration[src]

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

Formats the value using the given formatter. Read more

impl Drop for Registration[src]

fn drop(&mut self)[src]

Executes the destructor for this type. Read more

impl Evented for Registration[src]

fn register(
    &self,
    poll: &Poll,
    token: Token,
    interest: Ready,
    opts: PollOpt
) -> Result<()>
[src]

Register self with the given Poll instance. Read more

fn reregister(
    &self,
    poll: &Poll,
    token: Token,
    interest: Ready,
    opts: PollOpt
) -> Result<()>
[src]

Re-register self with the given Poll instance. Read more

fn deregister(&self, poll: &Poll) -> Result<()>[src]

Deregister self from the given Poll instance Read more

impl Send for Registration[src]

impl Sync for Registration[src]

Auto Trait Implementations

impl !RefUnwindSafe for Registration

impl Unpin for Registration

impl !UnwindSafe for Registration

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.