Struct SourceFd

pub struct SourceFd<'a>(pub &'a RawFd);

Adapter for RawFd providing an event::Source implementation.

SourceFd enables registering any type with an FD with Poll.

While only implementations for TCP and UDP are provided, Mio supports registering any FD that can be registered with the underlying OS selector. SourceFd provides the necessary bridge.

Note that SourceFd takes a &RawFd. This is because SourceFd does not take ownership of the FD. Specifically, it will not manage any lifecycle related operations, such as closing the FD on drop. It is expected that the SourceFd is constructed right before a call to Registry::register. See the examples for more detail.

Examples

Basic usage.

# use std::error::Error;
# fn main() -> Result<(), Box<dyn Error>> {
use mio::{Interest, Poll, Token};
#[cfg(unix)]
use mio::unix::SourceFd;
#[cfg(target_os = "wasi")]
use mio::wasi::SourceFd;

use std::os::fd::AsRawFd;
use std::net::TcpListener;

// Bind a std listener
let listener = TcpListener::bind("127.0.0.1:0")?;

let poll = Poll::new()?;

// Register the listener
poll.registry().register(
    &mut SourceFd(&listener.as_raw_fd()),
    Token(0),
    Interest::READABLE)?;
#     Ok(())
# }

Implementing event::Source for a custom type backed by a RawFd.

use mio::{event, Interest, Registry, Token};
#[cfg(unix)]
use mio::unix::SourceFd;
#[cfg(target_os = "wasi")]
use mio::wasi::SourceFd;

use std::os::fd::RawFd;
use std::io;

# #[allow(dead_code)]
pub struct MyIo {
    fd: RawFd,
}

impl event::Source for MyIo {
    fn register(&mut self, registry: &Registry, token: Token, interests: Interest)
        -> io::Result<()>
    {
        SourceFd(&self.fd).register(registry, token, interests)
    }

    fn reregister(&mut self, registry: &Registry, token: Token, interests: Interest)
        -> io::Result<()>
    {
        SourceFd(&self.fd).reregister(registry, token, interests)
    }

    fn deregister(&mut self, registry: &Registry) -> io::Result<()> {
        SourceFd(&self.fd).deregister(registry)
    }
}

Fields

0: &'a RawFd

Trait Implementations

impl<'a> Debug for SourceFd<'a>

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

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

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

Auto Trait Implementations

impl<'a> Freeze for SourceFd<'a>

impl<'a> RefUnwindSafe for SourceFd<'a>

impl<'a> Send for SourceFd<'a>

impl<'a> Sync for SourceFd<'a>

impl<'a> Unpin for SourceFd<'a>

impl<'a> UnsafeUnpin for SourceFd<'a>

impl<'a> UnwindSafe for SourceFd<'a>

Blanket Implementations

impl<T> Any for SourceFd<'a> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for SourceFd<'a> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for SourceFd<'a> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for SourceFd<'a>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for SourceFd<'a> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for SourceFd<'a> where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for SourceFd<'a> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>