Struct VarReader

Source
pub struct VarReader<'a, R: Read> { /* private fields */ }
Expand description

A reader that reads variable-length encoded data from a stream. Data is read in little-endian unless the big-endian feature is enabled.

§Example

use send_it::reader::VarReader;

// Create a sample stream, this is the output from the above test_writer test
let stream: Vec<u8> = vec![21, 7, 0, 0, 0, 72, 101, 108, 108, 111, 44, 32, 6, 0, 0, 0, 87, 111, 114, 108, 100, 33];
// turn the vector into a slice as Vec does not implement Read
let mut fake_stream = stream.as_slice();

// create a new VarReader
let mut reader = crate::reader::VarReader::new(&mut fake_stream);

let data = reader.read_data().unwrap();
assert_eq!(data[0].to_string(), "Hello");
assert_eq!(data[1].to_string(), "World");

Implementations§

Source§

impl<'a, R: Read> VarReader<'a, R>

Source

pub fn new(reader: &'a mut R) -> Self

Create a new VarReader

Examples found in repository?
examples/server.rs (line 15)
3fn main() {
4    // create a TcpListener
5    let listener = std::net::TcpListener::bind("0.0.0.0:3333").expect("Failed to start tcp listener!");
6    println!("Listening on :3333");
7
8    // listen for incoming connections
9    for stream in listener.incoming() {
10        let mut stream = stream.expect("Invalid stream!");
11        
12        // handle a new connection
13        std::thread::spawn(move || {
14            // create a reader for the TcpStream
15            let mut reader = send_it::reader::VarReader::new(&mut stream);
16            // loop while there is incoming data
17            while let Ok(data) = reader.read_data() {
18                // convert our segments to strings in the vector
19                let readable_data = Segment::to_readable(data);
20                
21                // print out the vector of strings
22                println!("Segments from client: [{}]", readable_data.join(", "));
23            }
24            println!("Connection closed!");
25        });
26    }
27}
Source

pub fn read_data(&mut self) -> Result<Vec<Segment>>

Reads data from the stream.

§Example
use send_it::reader::VarReader;

let stream: Vec<u8> = vec![21, 7, 0, 0, 0, 72, 101, 108, 108, 111, 44, 32, 6, 0, 0, 0, 87, 111, 114, 108, 100, 33];
// turn the vector into a slice as Vec does not implement Read
let mut fake_stream = stream.as_slice();

// create a new VarReader
let mut reader = crate::reader::VarReader::new(&mut fake_stream);

let data = reader.read_data().unwrap();
assert_eq!(data[0].to_string(), "Hello");
assert_eq!(data[1].to_string(), "World");
Examples found in repository?
examples/server.rs (line 17)
3fn main() {
4    // create a TcpListener
5    let listener = std::net::TcpListener::bind("0.0.0.0:3333").expect("Failed to start tcp listener!");
6    println!("Listening on :3333");
7
8    // listen for incoming connections
9    for stream in listener.incoming() {
10        let mut stream = stream.expect("Invalid stream!");
11        
12        // handle a new connection
13        std::thread::spawn(move || {
14            // create a reader for the TcpStream
15            let mut reader = send_it::reader::VarReader::new(&mut stream);
16            // loop while there is incoming data
17            while let Ok(data) = reader.read_data() {
18                // convert our segments to strings in the vector
19                let readable_data = Segment::to_readable(data);
20                
21                // print out the vector of strings
22                println!("Segments from client: [{}]", readable_data.join(", "));
23            }
24            println!("Connection closed!");
25        });
26    }
27}

Auto Trait Implementations§

§

impl<'a, R> Freeze for VarReader<'a, R>

§

impl<'a, R> RefUnwindSafe for VarReader<'a, R>
where R: RefUnwindSafe,

§

impl<'a, R> Send for VarReader<'a, R>
where R: Send,

§

impl<'a, R> Sync for VarReader<'a, R>
where R: Sync,

§

impl<'a, R> Unpin for VarReader<'a, R>

§

impl<'a, R> !UnwindSafe for VarReader<'a, R>

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.