Struct MemoryListener

Source
pub struct MemoryListener { /* private fields */ }
Expand description

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 calling accept or by iterating over iterating over the Incoming iterator returned by incoming.

The socket will be closed when the value is dropped.

§Examples

use std::io::{Read, Result, Write};

use memory_socket::{MemoryListener, MemorySocket};

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)?;
    stream.flush()
}

fn main() -> Result<()> {
    let mut listener = MemoryListener::bind(16)?;

    // accept connections and process them serially
    for stream in listener.incoming() {
        write_stormlight(stream?)?;
    }
    Ok(())
}

Implementations§

Source§

impl MemoryListener

Source

pub fn incoming_stream(&mut self) -> IncomingStream<'_>

Returns a stream over the connections being received on this listener.

The returned stream will never return None.

§Examples
use futures::prelude::*;
use memory_socket::MemoryListener;

let mut listener = MemoryListener::bind(80).unwrap();
let mut incoming = listener.incoming_stream();

while let Some(stream) = incoming.next().await {
    match stream {
        Ok(stream) => {
            println!("new client!");
        }
        Err(e) => { /* connection failed */ }
    }
}
Source§

impl MemoryListener

Source

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

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 memory_socket::MemoryListener;

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

pub fn local_addr(&self) -> u16

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 memory_socket::MemoryListener;

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

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

pub fn incoming(&self) -> Incoming<'_>

Returns an iterator over the connections being received on this listener.

The returned iterator will never return None. Iterating over it is equivalent to calling accept in a loop.

§Examples
use memory_socket::MemoryListener;
use std::io::{Read, Write};

let mut listener = MemoryListener::bind(80).unwrap();

for stream in listener.incoming() {
    match stream {
        Ok(stream) => {
            println!("new client!");
        }
        Err(e) => { /* connection failed */ }
    }
}
Source

pub fn accept(&self) -> Result<MemorySocket>

Accept a new incoming connection from this listener.

This function will block the calling thread until a new connection is established. When established, the corresponding MemorySocket will be returned.

§Examples
use std::net::TcpListener;
use memory_socket::MemoryListener;

let mut listener = MemoryListener::bind(8080).unwrap();
match listener.accept() {
    Ok(_socket) => println!("new client!"),
    Err(e) => println!("couldn't get client: {:?}", e),
}

Trait Implementations§

Source§

impl Drop for MemoryListener

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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

Source§

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

Source§

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.