Skip to main content

ZonWriter

Struct ZonWriter 

Source
pub struct ZonWriter { /* private fields */ }

Implementations§

Source§

impl ZonWriter

Source

pub fn new() -> Self

Examples found in repository?
examples/basic.rs (line 7)
3fn main() {
4    println!("Running ZON Basic Example...");
5
6    // 1. Write
7    let mut writer = ZonWriter::new();
8    let text_off = writer.write_string("Hello Zero-Copy World!");
9    let num_off = writer.write_u32(42);
10    
11    writer.set_root(text_off);
12    
13    println!("Written {} bytes.", writer.len());
14    
15    // 2. Read
16    let buffer = writer.as_bytes();
17    let reader = ZonReader::new(buffer).expect("Buffer verification failed");
18    
19    let root = reader.read_u32(8).unwrap();
20    let text = reader.read_string(root).unwrap();
21    let num = reader.read_u32(num_off).unwrap();
22    
23    println!("Read Root String: {}", text);
24    println!("Read Number: {}", num);
25    
26    assert_eq!(text, "Hello Zero-Copy World!");
27    assert_eq!(num, 42);
28    
29    println!("Success!");
30}
Source

pub fn len(&self) -> usize

Examples found in repository?
examples/basic.rs (line 13)
3fn main() {
4    println!("Running ZON Basic Example...");
5
6    // 1. Write
7    let mut writer = ZonWriter::new();
8    let text_off = writer.write_string("Hello Zero-Copy World!");
9    let num_off = writer.write_u32(42);
10    
11    writer.set_root(text_off);
12    
13    println!("Written {} bytes.", writer.len());
14    
15    // 2. Read
16    let buffer = writer.as_bytes();
17    let reader = ZonReader::new(buffer).expect("Buffer verification failed");
18    
19    let root = reader.read_u32(8).unwrap();
20    let text = reader.read_string(root).unwrap();
21    let num = reader.read_u32(num_off).unwrap();
22    
23    println!("Read Root String: {}", text);
24    println!("Read Number: {}", num);
25    
26    assert_eq!(text, "Hello Zero-Copy World!");
27    assert_eq!(num, 42);
28    
29    println!("Success!");
30}
Source

pub fn is_empty(&self) -> bool

Source

pub fn as_bytes(&self) -> &[u8]

Examples found in repository?
examples/basic.rs (line 16)
3fn main() {
4    println!("Running ZON Basic Example...");
5
6    // 1. Write
7    let mut writer = ZonWriter::new();
8    let text_off = writer.write_string("Hello Zero-Copy World!");
9    let num_off = writer.write_u32(42);
10    
11    writer.set_root(text_off);
12    
13    println!("Written {} bytes.", writer.len());
14    
15    // 2. Read
16    let buffer = writer.as_bytes();
17    let reader = ZonReader::new(buffer).expect("Buffer verification failed");
18    
19    let root = reader.read_u32(8).unwrap();
20    let text = reader.read_string(root).unwrap();
21    let num = reader.read_u32(num_off).unwrap();
22    
23    println!("Read Root String: {}", text);
24    println!("Read Number: {}", num);
25    
26    assert_eq!(text, "Hello Zero-Copy World!");
27    assert_eq!(num, 42);
28    
29    println!("Success!");
30}
Source

pub fn write_u32(&mut self, val: u32) -> u32

Appends the 4 bytes of val to the buffer. Returns the offset (index) where those bytes were written.

Examples found in repository?
examples/basic.rs (line 9)
3fn main() {
4    println!("Running ZON Basic Example...");
5
6    // 1. Write
7    let mut writer = ZonWriter::new();
8    let text_off = writer.write_string("Hello Zero-Copy World!");
9    let num_off = writer.write_u32(42);
10    
11    writer.set_root(text_off);
12    
13    println!("Written {} bytes.", writer.len());
14    
15    // 2. Read
16    let buffer = writer.as_bytes();
17    let reader = ZonReader::new(buffer).expect("Buffer verification failed");
18    
19    let root = reader.read_u32(8).unwrap();
20    let text = reader.read_string(root).unwrap();
21    let num = reader.read_u32(num_off).unwrap();
22    
23    println!("Read Root String: {}", text);
24    println!("Read Number: {}", num);
25    
26    assert_eq!(text, "Hello Zero-Copy World!");
27    assert_eq!(num, 42);
28    
29    println!("Success!");
30}
Source

pub fn write_string(&mut self, val: &str) -> u32

First, append a 4-byte length (u32). Then, append the raw string bytes. Crucial: Append padding zeros until the buffer’s total size is a multiple of 4 bytes. Returns the offset where the length was written.

Examples found in repository?
examples/basic.rs (line 8)
3fn main() {
4    println!("Running ZON Basic Example...");
5
6    // 1. Write
7    let mut writer = ZonWriter::new();
8    let text_off = writer.write_string("Hello Zero-Copy World!");
9    let num_off = writer.write_u32(42);
10    
11    writer.set_root(text_off);
12    
13    println!("Written {} bytes.", writer.len());
14    
15    // 2. Read
16    let buffer = writer.as_bytes();
17    let reader = ZonReader::new(buffer).expect("Buffer verification failed");
18    
19    let root = reader.read_u32(8).unwrap();
20    let text = reader.read_string(root).unwrap();
21    let num = reader.read_u32(num_off).unwrap();
22    
23    println!("Read Root String: {}", text);
24    println!("Read Number: {}", num);
25    
26    assert_eq!(text, "Hello Zero-Copy World!");
27    assert_eq!(num, 42);
28    
29    println!("Success!");
30}
Source

pub fn set_root(&mut self, offset: u32)

Updates the root offset in the header. The header is always at the start of the buffer.

Examples found in repository?
examples/basic.rs (line 11)
3fn main() {
4    println!("Running ZON Basic Example...");
5
6    // 1. Write
7    let mut writer = ZonWriter::new();
8    let text_off = writer.write_string("Hello Zero-Copy World!");
9    let num_off = writer.write_u32(42);
10    
11    writer.set_root(text_off);
12    
13    println!("Written {} bytes.", writer.len());
14    
15    // 2. Read
16    let buffer = writer.as_bytes();
17    let reader = ZonReader::new(buffer).expect("Buffer verification failed");
18    
19    let root = reader.read_u32(8).unwrap();
20    let text = reader.read_string(root).unwrap();
21    let num = reader.read_u32(num_off).unwrap();
22    
23    println!("Read Root String: {}", text);
24    println!("Read Number: {}", num);
25    
26    assert_eq!(text, "Hello Zero-Copy World!");
27    assert_eq!(num, 42);
28    
29    println!("Success!");
30}

Trait Implementations§

Source§

impl Default for ZonWriter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.