pub struct VarWriter { /* private fields */ }Expand description
A writer for sending several segments over a stream using variable length encoding Data is written in little-endian if the feature “big-endian” is not enabled
Example
use send_it::writer::VarWriter;
let mut sender = VarWriter::new();
sender.add_string("Hello");
sender.add_string("World");
let mut buffer = Vec::new();
sender.send(&mut buffer).unwrap();Implementations§
source§impl VarWriter
impl VarWriter
sourcepub fn add_string<S: Into<String>>(&mut self, string: S)
pub fn add_string<S: Into<String>>(&mut self, string: S)
Add a string to the writer
Example
use send_it::writer::VarWriter;
let mut sender = VarWriter::new();
sender.add_string("Hello");Examples found in repository?
examples/client.rs (line 9)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
fn main() {
// create a connection to our example server in server.rs
let mut connection = std::net::TcpStream::connect("localhost:3333").expect("Failed to connect to server!");
// create a writer
let mut writer = send_it::writer::VarWriter::default();
// add two segments: "Hello, " and "world!"
writer.add_string("Hello, ");
writer.add_string("world!");
// send our two segments to the TcpStream and exit
writer.send(&mut connection).expect("Failed to send data!");
println!("sent data!");
}sourcepub fn add_raw(&mut self, raw: &[u8])
pub fn add_raw(&mut self, raw: &[u8])
Add raw data to the writer
Example
use send_it::writer::VarWriter;
let mut sender = VarWriter::new();
sender.add_raw(&[0x48, 0x65, 0x6C, 0x6C, 0x6F]);sourcepub fn send<W: Write>(&mut self, stream: &mut W) -> Result<()>
pub fn send<W: Write>(&mut self, stream: &mut W) -> Result<()>
Encodes the data and sends it over the stream.
- The data is cleared after sending.
Example
use send_it::writer::VarWriter;
let mut sender = VarWriter::new();
sender.add_string("Hello");
sender.add_string("World");
let mut buffer = Vec::new();
sender.send(&mut buffer).unwrap();Examples found in repository?
examples/client.rs (line 13)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
fn main() {
// create a connection to our example server in server.rs
let mut connection = std::net::TcpStream::connect("localhost:3333").expect("Failed to connect to server!");
// create a writer
let mut writer = send_it::writer::VarWriter::default();
// add two segments: "Hello, " and "world!"
writer.add_string("Hello, ");
writer.add_string("world!");
// send our two segments to the TcpStream and exit
writer.send(&mut connection).expect("Failed to send data!");
println!("sent data!");
}sourcepub fn send_without_clearing<W: Write>(&mut self, stream: &mut W) -> Result<()>
pub fn send_without_clearing<W: Write>(&mut self, stream: &mut W) -> Result<()>
Encodes the data and sends it over the stream.
- The data is not cleared after sending.
Example
use send_it::writer::VarWriter;
let mut sender = VarWriter::new();
sender.add_string("Hello");
sender.add_string("World");
let mut buffer = Vec::new();
sender.send_without_clearing(&mut buffer).unwrap();Trait Implementations§
source§impl Write for VarWriter
impl Write for VarWriter
source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write a buffer into this writer, returning how many bytes were written. Read more
source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flush this output stream, ensuring that all intermediately buffered
contents reach their destination. Read more
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector)1.0.0 · source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer. Read more
source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
write_all_vectored)Attempts to write multiple buffers into this writer. Read more
Auto Trait Implementations§
impl RefUnwindSafe for VarWriter
impl Send for VarWriter
impl Sync for VarWriter
impl Unpin for VarWriter
impl UnwindSafe for VarWriter
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