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>
impl<'a, R: Read> VarReader<'a, R>
Sourcepub fn new(reader: &'a mut R) -> Self
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}
Sourcepub fn read_data(&mut self) -> Result<Vec<Segment>>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more