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)
1fn main() {
2 // create a connection to our example server in server.rs
3 let mut connection = std::net::TcpStream::connect("localhost:3333").expect("Failed to connect to server!");
4
5 // create a writer
6 let mut writer = send_it::writer::VarWriter::default();
7
8 // add two segments: "Hello, " and "world!"
9 writer.add_string("Hello, ");
10 writer.add_string("world!");
11
12 // send our two segments to the TcpStream and exit
13 writer.send(&mut connection).expect("Failed to send data!");
14 println!("sent data!");
15}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)
1fn main() {
2 // create a connection to our example server in server.rs
3 let mut connection = std::net::TcpStream::connect("localhost:3333").expect("Failed to connect to server!");
4
5 // create a writer
6 let mut writer = send_it::writer::VarWriter::default();
7
8 // add two segments: "Hello, " and "world!"
9 writer.add_string("Hello, ");
10 writer.add_string("world!");
11
12 // send our two segments to the TcpStream and exit
13 writer.send(&mut connection).expect("Failed to send data!");
14 println!("sent data!");
15}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>
Writes a buffer into this writer, returning how many bytes were written. Read more
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flushes 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 Freeze for VarWriter
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