[][src]Struct memsocket::MemoryListener

pub struct MemoryListener { /* fields omitted */ }

An in-memory socket server, listening for connections.

After creating a MemoryListener by binding it to a socket address, it listens for incoming connections. These can be accepted by awaiting elements from the async stream of incoming connections, incoming.

The socket will be closed when the value is dropped.

Examples

use std::io::Result;

use memsocket::{MemoryListener, MemorySocket};
use futures::prelude::*;

async fn write_stormlight(mut stream: MemorySocket) -> Result<()> {
    let msg = b"The most important step a person can take is always the next one.";
    stream.write_all(msg).await?;
    stream.flush().await
}

async fn listen() -> Result<()> {
    let mut listener = MemoryListener::bind(16)?;
    let mut incoming = listener.incoming();

    // accept connections and process them serially
    while let Some(stream) = incoming.next().await {
        write_stormlight(stream?).await?;
    }
    Ok(())
}

Implementations

impl MemoryListener[src]

pub fn bind(port: u16) -> Result<Self>[src]

Creates a new MemoryListener which will be bound to the specified port.

The returned listener is ready for accepting connections.

Binding with a port number of 0 will request that a port be assigned to this listener. The port allocated can be queried via the local_addr method.

Examples

Create a MemoryListener bound to port 16:

use memsocket::MemoryListener;

let listener = MemoryListener::bind(16)?;

pub fn local_addr(&self) -> u16[src]

Returns the local address that this listener is bound to.

This can be useful, for example, when binding to port 0 to figure out which port was actually bound.

Examples

use memsocket::MemoryListener;

let listener = MemoryListener::bind(16)?;

assert_eq!(listener.local_addr(), 16);

pub fn incoming(&mut self) -> Incoming<'_>[src]

Consumes this listener, returning a stream of the sockets this listener accepts.

This method returns an implementation of the Stream trait which resolves to the sockets the are accepted on this listener.

Examples

use futures::prelude::*;
use memsocket::MemoryListener;

let mut listener = MemoryListener::bind(16)?;
let mut incoming = listener.incoming();

// accept connections and process them serially
while let Some(stream) = incoming.next().await {
    match stream {
        Ok(stream) => {
            println!("new connection!");
        },
        Err(e) => { /* connection failed */ }
    }
}

Trait Implementations

impl Debug for MemoryListener[src]

impl Drop for MemoryListener[src]

Auto Trait Implementations

impl !RefUnwindSafe for MemoryListener

impl Send for MemoryListener

impl Sync for MemoryListener

impl Unpin for MemoryListener

impl !UnwindSafe for MemoryListener

Blanket Implementations

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

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

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

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

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

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.

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.