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