Crate variable_len_reader
source ·Expand description
§Variable Len Reader
Read this in other languages: English, 简体中文.
§Description
A Rust crate to read variable length data based on varint format.
§Usage
Add this to your Cargo.toml
:
[dependencies]
variable-len-reader = "^2.1"
§Example
Directly use in tcp stream:
use std::net::{TcpListener, TcpStream};
use variable_len_reader::{VariableReader, VariableWriter};
fn main() {
let addr = "localhost:25564";
let server = TcpListener::bind(addr).unwrap();
let mut client = TcpStream::connect(addr).unwrap();
let mut server = server.incoming().next().unwrap().unwrap();
// Write
client.write_string(&"Hello world!").unwrap();
// Read
let message = server.read_string().unwrap();
assert_eq!("Hello world!", message);
}
Use with bytes crate:
use bytes::{Buf, BufMut, BytesMut};
use variable_len_reader::{VariableReader, VariableWriter};
fn main() {
let message = "Hello world!";
let mut writer = BytesMut::new().writer();
// Write
writer.write_string(message).unwrap();
let bytes = writer.into_inner();
assert_eq!(message.len() as u8, bytes[0]);
assert_eq!(message.as_bytes(), &bytes[1..]);
let mut reader = bytes.reader();
// Read
let string = reader.read_string().unwrap();
assert_eq!(message, string);
}
Async mode with tokio crate: (Require ‘async_default’ feature)
use tokio::net::{TcpListener, TcpStream};
use variable_len_reader::{AsyncVariableReader, AsyncVariableWriter};
#[tokio::main]
async fn main() {
let addr = "localhost:25564";
let server = TcpListener::bind(addr).await.unwrap();
let mut client = TcpStream::connect(addr).await.unwrap();
let (mut server, _) = server.accept().await.unwrap();
// Write
client.write_string(&"Hello tokio!").await.unwrap();
// Read
let message = server.read_string().await.unwrap();
assert_eq!("Hello tokio!", message);
}
Modules§
Macros§
Structs§
- ReadBool
async
- ReadBools2
async
- ReadBools3
async
- ReadBools4
async
- ReadBools5
async
- ReadBools6
async
- ReadBools7
async
- ReadBools8
async
- ReadI8Raw
async
- ReadI8Varint
async
- ReadI16RawBe
async
- ReadI16RawLe
async
- ReadI16Varint
async
- ReadI16Varint2Be
async
- ReadI16Varint2Le
async
- ReadI32RawBe
async
- ReadI32RawLe
async
- ReadI32Varint
async
- ReadI32Varint2Be
async
- ReadI32Varint2Le
async
- ReadI32Varint4Be
async
- ReadI32Varint4Le
async
- ReadI64RawBe
async
- ReadI64RawLe
async
- ReadI64Varint
async
- ReadI64Varint2Be
async
- ReadI64Varint2Le
async
- ReadI64Varint4Be
async
- ReadI64Varint4Le
async
- ReadI64Varint8Be
async
- ReadI64Varint8Le
async
- ReadI128RawBe
async
- ReadI128RawLe
async
- ReadI128Varint
async
- ReadI128Varint2Be
async
- ReadI128Varint2Le
async
- ReadI128Varint4Be
async
- ReadI128Varint4Le
async
- ReadI128Varint8Be
async
- ReadI128Varint8Le
async
- ReadI128Varint16Be
async
- ReadI128Varint16Le
async
- ReadIsizeRawBe
async
- ReadIsizeRawLe
async
- ReadIsizeVarint
async
- ReadIsizeVarint2Be
async
- ReadIsizeVarint2Le
async
- ReadIsizeVarint4Be
async
- ReadIsizeVarint4Le
async
- ReadIsizeVarint8Be
async
- ReadIsizeVarint8Le
async
- ReadIsizeVarint16Be
async
- ReadIsizeVarint16Le
async
- ReadMore
async
- ReadSingle
async
- ReadU8Raw
async
- ReadU8Varint
async
- ReadU16RawBe
async
- ReadU16RawLe
async
- ReadU16Varint
async
- ReadU16Varint2Be
async
- ReadU16Varint2Le
async
- ReadU32RawBe
async
- ReadU32RawLe
async
- ReadU32Varint
async
- ReadU32Varint2Be
async
- ReadU32Varint2Le
async
- ReadU32Varint4Be
async
- ReadU32Varint4Le
async
- ReadU64RawBe
async
- ReadU64RawLe
async
- ReadU64Varint
async
- ReadU64Varint2Be
async
- ReadU64Varint2Le
async
- ReadU64Varint4Be
async
- ReadU64Varint4Le
async
- ReadU64Varint8Be
async
- ReadU64Varint8Le
async
- ReadU128RawBe
async
- ReadU128RawLe
async
- ReadU128Varint
async
- ReadU128Varint2Be
async
- ReadU128Varint2Le
async
- ReadU128Varint4Be
async
- ReadU128Varint4Le
async
- ReadU128Varint8Be
async
- ReadU128Varint8Le
async
- ReadU128Varint16Be
async
- ReadU128Varint16Le
async
- ReadUsizeRawBe
async
- ReadUsizeRawLe
async
- ReadUsizeVarint
async
- ReadUsizeVarint2Be
async
- ReadUsizeVarint2Le
async
- ReadUsizeVarint4Be
async
- ReadUsizeVarint4Le
async
- ReadUsizeVarint8Be
async
- ReadUsizeVarint8Le
async
- ReadUsizeVarint16Be
async
- ReadUsizeVarint16Le
async
- WriteBool
async
- WriteBools2
async
- WriteBools3
async
- WriteBools4
async
- WriteBools5
async
- WriteBools6
async
- WriteBools7
async
- WriteBools8
async
- WriteI8Raw
async
- WriteI8Varint
async
- WriteI16RawBe
async
- WriteI16RawLe
async
- WriteI16Varint
async
- WriteI16Varint2Be
async
- WriteI16Varint2Le
async
- WriteI32RawBe
async
- WriteI32RawLe
async
- WriteI32Varint
async
- WriteI32Varint2Be
async
- WriteI32Varint2Le
async
- WriteI32Varint4Be
async
- WriteI32Varint4Le
async
- WriteI64RawBe
async
- WriteI64RawLe
async
- WriteI64Varint
async
- WriteI64Varint2Be
async
- WriteI64Varint2Le
async
- WriteI64Varint4Be
async
- WriteI64Varint4Le
async
- WriteI64Varint8Be
async
- WriteI64Varint8Le
async
- WriteI128RawBe
async
- WriteI128RawLe
async
- WriteI128Varint
async
- WriteI128Varint2Be
async
- WriteI128Varint2Le
async
- WriteI128Varint4Be
async
- WriteI128Varint4Le
async
- WriteI128Varint8Be
async
- WriteI128Varint8Le
async
- WriteI128Varint16Be
async
- WriteI128Varint16Le
async
- WriteIsizeRawBe
async
- WriteIsizeRawLe
async
- WriteIsizeVarint
async
- WriteIsizeVarint2Be
async
- WriteIsizeVarint2Le
async
- WriteIsizeVarint4Be
async
- WriteIsizeVarint4Le
async
- WriteIsizeVarint8Be
async
- WriteIsizeVarint8Le
async
- WriteIsizeVarint16Be
async
- WriteIsizeVarint16Le
async
- WriteMore
async
- WriteSingle
async
- WriteU8Raw
async
- WriteU8Varint
async
- WriteU16RawBe
async
- WriteU16RawLe
async
- WriteU16Varint
async
- WriteU16Varint2Be
async
- WriteU16Varint2Le
async
- WriteU32RawBe
async
- WriteU32RawLe
async
- WriteU32Varint
async
- WriteU32Varint2Be
async
- WriteU32Varint2Le
async
- WriteU32Varint4Be
async
- WriteU32Varint4Le
async
- WriteU64RawBe
async
- WriteU64RawLe
async
- WriteU64Varint
async
- WriteU64Varint2Be
async
- WriteU64Varint2Le
async
- WriteU64Varint4Be
async
- WriteU64Varint4Le
async
- WriteU64Varint8Be
async
- WriteU64Varint8Le
async
- WriteU128RawBe
async
- WriteU128RawLe
async
- WriteU128Varint
async
- WriteU128Varint2Be
async
- WriteU128Varint2Le
async
- WriteU128Varint4Be
async
- WriteU128Varint4Le
async
- WriteU128Varint8Be
async
- WriteU128Varint8Le
async
- WriteU128Varint16Be
async
- WriteU128Varint16Le
async
- WriteUsizeRawBe
async
- WriteUsizeRawLe
async
- WriteUsizeVarint
async
- WriteUsizeVarint2Be
async
- WriteUsizeVarint2Le
async
- WriteUsizeVarint4Be
async
- WriteUsizeVarint4Le
async
- WriteUsizeVarint8Be
async
- WriteUsizeVarint8Le
async
- WriteUsizeVarint16Be
async
- WriteUsizeVarint16Le
async
Traits§
- AsyncVariableReader
async
- AsyncVariableWriter
async