Struct mio::Registration[][src]

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<()> {
        self.registration.deregister(poll)
    }
}

Methods

impl Registration
[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 Send for Registration
[src]

impl Sync for Registration
[src]

impl Evented for Registration
[src]

Register self with the given Poll instance. Read more

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

Deregister self from the given Poll instance Read more

impl Drop for Registration
[src]

Executes the destructor for this type. Read more

impl Debug for Registration
[src]

Formats the value using the given formatter. Read more