[][src]Struct sonr::net::stream::Stream

pub struct Stream<T: Read + Write + Evented> { /* fields omitted */ }

When a Stream reacts the inner evented reactor is marked as either readable and / or writable depending on the Ready state of the Event.

It is likely that a Stream is used as part of a larger Reactor, and the react method is called not by the System but rather the container of the Stream:

 use sonr::prelude::*;
 use sonr::net::tcp::ReactiveTcpStream;

 type WriteBuffer = Vec<u8>;
 
 struct Connections {
     streams: HashMap<Token, (ReactiveTcpStream, WriteBuffer)>
 }
 
 impl Connections {
     pub fn new() -> Self {
         Self {
             streams: HashMap::new(),
         }
     }
 }
 
 impl Reactor for Connections {
     type Input = ReactiveTcpStream;
     type Output = ();
 
     fn react(&mut self, reaction: Reaction<Self::Input>) -> Reaction<Self::Output> {
         use Reaction::*;
         match reaction {
             Value(stream) => {
                 // New stream
                 self.streams.insert(stream.token(), (stream, WriteBuffer::new()));
                 Continue
             }
             Event(event) => {
                 // Check if the event belongs to one of the streams, otherwise
                 // pass the event to the next reactor
                 if let Some((stream, write_buffer)) = self.streams.get_mut(&event.token()) {
                     stream.react(event.into());
 
                     // Read
                     while stream.readable() {
                         // Read until the stream block
                         // as the stream will not receive
                         // a new read event until it blocks
                         break
                     }
 
                     while stream.writable() && !write_buffer.is_empty() {
                         // Write to the stream until there is nothing 
                         // left in the write buffer
                         break
                     }
 
                     Continue
                 } else {
                     event.into()
                 }
             }
             Continue => Continue,
         }
     }
 }

Methods

impl<T> Stream<T> where
    T: Debug + Evented + Read + Write
[src]

Important traits for EventedReactor<E>
pub fn into_inner(self) -> EventedReactor<T>[src]

Consume the stream and return the underlying evented reactor

impl<T: Read + Write + Evented> Stream<T>[src]

pub fn new(inner: T) -> Result<Self>[src]

Create a new stream

pub fn token(&self) -> Token[src]

The token used to track readiness of the underlying stream

pub fn readable(&self) -> bool[src]

Is the underlying object readable?

pub fn writable(&self) -> bool[src]

Is the underlying object writable?

pub fn inner(&self) -> &T[src]

Reference the underlying object

pub fn inner_mut(&mut self) -> &mut T[src]

Mutable reference to the underlying object

Trait Implementations

impl<T: Read + Write + Evented> Reactor for Stream<T>[src]

type Output = ()

The output passed to the next reactor in the chain.

type Input = ()

Expected input type from the previous reactor in the chain.

fn chain<T: Reactor>(self, to: T) -> Chain<Self, T>[src]

Chain two reactors together. The output of the first reactor is the input of the second reactor. Read more

fn and<C: Reactor>(self, second: C) -> And<Self, C>[src]

Run two reactors independent of each other. ```no_run # use sonr::reactor::Reactor; # use sonr::errors::Result; use sonr::system::System; use sonr::net::tcp::ReactiveTcpListener; Read more

fn map<F, T>(self, callback: F) -> Map<Self, F, T>[src]

Capture the output of a reactor in a closure. no_run // Create a listener, print the address every time // the listener accepts a new connection, then push that // connection onto a queue. # use sonr::net::tcp; # use sonr::sync::queue::ReactiveQueue; # use sonr::errors::Result; # use sonr::prelude::*; # fn main() -> Result<()> { System::init(); let listener = tcp::ReactiveTcpListener::bind("127.0.0.1:5000")?; let queue = ReactiveQueue::unbounded(); let server = listener.map(|(stream, addr)| { stream }).chain(queue); System::start(server); # Ok(()) # } Read more

fn or<T: Reactor>(self, second: T) -> Or<Self, T>[src]

Pass the output from a reactor into one of two reactors depending on the output. Note that both Reactors in an or are required to have the same output, and it's only possible to chain two ored reactors if the reactor that does the chaining outputs Either. Read more

impl<T: Evented + Write + Read> AsRef<Stream<T>> for Stream<T>[src]

impl<T: Read + Write + Evented> From<EventedReactor<T>> for Stream<T>[src]

impl<T> Debug for Stream<T> where
    T: Debug + Evented + Read + Write
[src]

impl<T: Read + Write + Evented> Read for Stream<T>[src]

fn read_vectored(&mut self, bufs: &mut [IoVecMut]) -> Result<usize, Error>[src]

🔬 This is a nightly-only experimental API. (iovec)

Like read, except that it reads into a slice of buffers. Read more

unsafe fn initializer(&self) -> Initializer[src]

🔬 This is a nightly-only experimental API. (read_initializer)

Determines if this Reader can work with buffers of uninitialized memory. Read more

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
1.0.0
[src]

Read all bytes until EOF in this source, placing them into buf. Read more

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0
[src]

Read all bytes until EOF in this source, appending them to buf. Read more

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0
[src]

Read the exact number of bytes required to fill buf. Read more

fn by_ref(&mut self) -> &mut Self
1.0.0
[src]

Creates a "by reference" adaptor for this instance of Read. Read more

fn bytes(self) -> Bytes<Self>
1.0.0
[src]

Transforms this Read instance to an [Iterator] over its bytes. Read more

fn chain<R>(self, next: R) -> Chain<Self, R> where
    R: Read
1.0.0
[src]

Creates an adaptor which will chain this stream with another. Read more

fn take(self, limit: u64) -> Take<Self>
1.0.0
[src]

Creates an adaptor which will read at most limit bytes from it. Read more

impl<T: Read + Write + Evented> Write for Stream<T>[src]

fn write_vectored(&mut self, bufs: &[IoVec]) -> Result<usize, Error>[src]

🔬 This is a nightly-only experimental API. (iovec)

Like write, except that it writes from a slice of buffers. Read more

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0
[src]

Attempts to write an entire buffer into this writer. Read more

fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0
[src]

Writes a formatted string into this writer, returning any error encountered. Read more

fn by_ref(&mut self) -> &mut Self
1.0.0
[src]

Creates a "by reference" adaptor for this instance of Write. Read more

Auto Trait Implementations

impl<T> !Send for Stream<T>

impl<T> !Sync for Stream<T>

Blanket Implementations

impl<T> From for T[src]

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

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

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

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Erased for T