# Struct mio::unix::SourceFd [−] [src]

    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 mio::{Interest, Poll, Token};
    use mio::unix::SourceFd;

    use std::os::unix::io::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)?;

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

ⓘ

    use mio::{event, Interest, Registry, Token};
    use mio::unix::SourceFd;

    use std::os::unix::io::RawFd;
    use std::io;

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

## Trait Implementations

### `impl<'a> Debug for SourceFd<'a>` [src]

#### `fn fmt(&self, f: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter. Read more

### `impl<'a> Source for SourceFd<'a>` [src]

#### `fn register(
&mut self,
registry: &Registry,
token: Token,
interests: Interest
) -> Result<()>` [src]

Register `self` with the given `Registry` instance. Read more

#### `fn reregister(
&mut self,
registry: &Registry,
token: Token,
interests: Interest
) -> Result<()>` [src]

Re-register `self` with the given `Registry` instance. Read more

#### `fn deregister(&mut self, registry: &Registry) -> Result<()>` [src]

Deregister `self` from the given `Registry` instance. Read more

## Auto Trait Implementations

### `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> UnwindSafe for SourceFd<'a>`

## Blanket Implementations

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `pub fn type_id(&self) -> TypeId` [src]

Gets the `TypeId` of `self`. Read more

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

#### `pub fn borrow(&self) -> &T` [src]

Immutably borrows from an owned value. Read more

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

#### `pub fn borrow_mut(&mut self) -> &mut T` [src]

Mutably borrows from an owned value. Read more

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

#### `pub fn from(t: T) -> T` [src]

Performs the conversion.

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

#### `pub fn into(self) -> U` [src]

Performs the conversion.

### `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.

#### `pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>` [src]

Performs the conversion.

### `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.

#### `pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>` [src]

Performs the conversion.