7 releases
0.2.2 | Jan 31, 2024 |
---|---|
0.2.1 | Jan 31, 2024 |
0.1.5 | Dec 10, 2023 |
#1130 in Network programming
105 downloads per month
25KB
365 lines
Send-it
A rust crate for sending multiple segments of information together over a stream using variable length encoding.
* this does not compress or encrypt the data. It turns the data into a stream of bytes and adds length information about each segment
Example
use send_it::writer::VarWriter;
use send_it::reader::VarReader;
// Create a new VarWriter
let mut writer = VarWriter::new();
// Add some sample data
writer.add_string("Hello, ");
writer.add_string("World!");
// Use any Write implementor as your stream (i.e. TcpStream)
let mut stream: Vec<u8> = Vec::new();
// encode the data and send it over the stream
writer.send(&mut stream).expect("Failed to send data");
// turn the vector into a slice as Vec does not implement Read
let mut fake_stream = stream.as_slice();
// create a new VarReader to read from the stream we wrote to
let mut reader = VarReader::new(&mut fake_stream);
// read the data from the stream
let data = reader.read_data().unwrap();
assert_eq!(data[0].to_string(), "Hello, ");
assert_eq!(data[1].to_string(), "World!");
Features
writing (enabled by default)
Adds the VarWriter struct, which is used to write data to a stream using variable-length encoding
reading (enabled by default)
Adds the VarReader struct, which is used to read data from a stream using variable-length encoding
big-endian (disabled by default)
Changes the encoding to use big-endian instead of little-endian
Usage
VarWriter
A struct used to write data to a stream using variable-length encoding
feature: 'writing' (enabled by default)
use send_it::writer::VarWriter;
// Create a new VarWriter
let mut writer = VarWriter::new();
// Add some sample data
writer.add_string("Hello, ");
writer.add_string("World!");
// Use any Write implementor as your stream (i.e. TcpStream)
let mut stream: Vec<u8> = Vec::new();
// encode the data and send it over the stream
writer.send(&mut stream).expect("Failed to send data");
VarReader
A struct used to read data from a stream using variable-length encoding
feature: 'reading' (enabled by default)
use send_it::reader::VarReader;
// Create a sample stream, this is the output from the above VarWriter example
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 = VarReader::new(&mut fake_stream);
// read the data from the stream
let data = reader.read_data().unwrap();
assert_eq!(data[0].to_string(), "Hello, ");
assert_eq!(data[1].to_string(), "World!");
Segment
A struct used to represent a segment of data
use send_it::Segment;
let mut segment = Segment::new();
segment.append(Segment::from("Hello, "));
segment.append(Segment::from("World!"));
assert_eq!(segment.to_string(), "Hello, World!");
Dependencies
~0–5.5MB
~20K SLoC