Struct corcovado::Registration

source ·
pub struct Registration { /* private fields */ }
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 corcovado::{Ready, Registration, Poll, PollOpt, Token};
use corcovado::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§

source§

impl Registration

source

pub fn new2() -> (Registration, SetReadiness)

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 corcovado::{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(());
        }
    }
}
source

pub fn new( poll: &Poll, token: Token, interest: Ready, opt: PollOpt, ) -> (Registration, SetReadiness)

source

pub fn update( &self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt, ) -> Result<()>

source

pub fn deregister(&self, poll: &Poll) -> Result<()>

Trait Implementations§

source§

impl Debug for Registration

source§

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

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

impl Drop for Registration

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Evented for Registration

source§

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

Register self with the given Poll instance. Read more
source§

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

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

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

Deregister self from the given Poll instance Read more
source§

impl Send for Registration

source§

impl Sync for Registration

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.