Trait mio::event::Source

source ·
pub trait Source {
    // Required methods
    fn register(
        &mut self,
        registry: &Registry,
        token: Token,
        interests: Interest
    ) -> Result<()>;
    fn reregister(
        &mut self,
        registry: &Registry,
        token: Token,
        interests: Interest
    ) -> Result<()>;
    fn deregister(&mut self, registry: &Registry) -> Result<()>;
}
Expand description

An event source that may be registered with Registry.

Types that implement event::Source can be registered with Registry. Users of Mio should not use the event::Source trait functions directly. Instead, the equivalent functions on Registry should be used.

See Registry for more details.

Implementing event::Source

Event sources are always backed by system handles, such as sockets or other system handles. These event::Sources will be monitored by the system selector. An implementation of Source will almost always delegates to a lower level handle. Examples of this are TcpStreams, or the unix only SourceFd.

Dropping event::Sources

All event::Sources, unless otherwise specified, need to be deregistered before being dropped for them to not leak resources. This goes against the normal drop behaviour of types in Rust which cleanup after themselves, e.g. a File will close itself. However since deregistering needs access to Registry this cannot be done while being dropped.

Examples

Implementing Source on a struct containing a socket:

use mio::{Interest, Registry, Token};
use mio::event::Source;
use mio::net::TcpStream;

use std::io;

pub struct MySource {
    socket: TcpStream,
}

impl Source for MySource {
    fn register(&mut self, registry: &Registry, token: Token, interests: Interest)
        -> io::Result<()>
    {
        // Delegate the `register` call to `socket`
        self.socket.register(registry, token, interests)
    }

    fn reregister(&mut self, registry: &Registry, token: Token, interests: Interest)
        -> io::Result<()>
    {
        // Delegate the `reregister` call to `socket`
        self.socket.reregister(registry, token, interests)
    }

    fn deregister(&mut self, registry: &Registry) -> io::Result<()> {
        // Delegate the `deregister` call to `socket`
        self.socket.deregister(registry)
    }
}

Required Methods§

source

fn register( &mut self, registry: &Registry, token: Token, interests: Interest ) -> Result<()>

Register self with the given Registry instance.

This function should not be called directly. Use Registry::register instead. Implementors should handle registration by delegating the call to another Source type.

source

fn reregister( &mut self, registry: &Registry, token: Token, interests: Interest ) -> Result<()>

Re-register self with the given Registry instance.

This function should not be called directly. Use Registry::reregister instead. Implementors should handle re-registration by either delegating the call to another Source type.

source

fn deregister(&mut self, registry: &Registry) -> Result<()>

Deregister self from the given Registry instance.

This function should not be called directly. Use Registry::deregister instead. Implementors should handle deregistration by delegating the call to another Source type.

Implementations on Foreign Types§

source§

impl<T> Source for Box<T>where T: Source + ?Sized,

source§

fn register( &mut self, registry: &Registry, token: Token, interests: Interest ) -> Result<()>

source§

fn reregister( &mut self, registry: &Registry, token: Token, interests: Interest ) -> Result<()>

source§

fn deregister(&mut self, registry: &Registry) -> Result<()>

Implementors§

source§

impl Source for TcpListener

Available on crate feature net only.
source§

impl Source for TcpStream

Available on crate feature net only.
source§

impl Source for UdpSocket

Available on crate feature net only.
source§

impl Source for UnixDatagram

Available on crate feature net only.
source§

impl Source for UnixListener

Available on crate feature net only.
source§

impl Source for UnixStream

Available on crate feature net only.
source§

impl Source for Receiver

Available on crate features os-ext and os-poll only.
source§

impl Source for Sender

Available on crate features os-ext and os-poll only.
source§

impl<'a> Source for SourceFd<'a>

Available on crate feature os-poll only.