[][src]Struct mio::Registration

pub struct Registration { /* fields omitted */ }

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)
    }
}

Methods

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]

impl Drop for Registration[src]

impl Evented for Registration[src]

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]

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

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

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

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

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.

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.