pub struct ZonWriter { /* private fields */ }Implementations§
Source§impl ZonWriter
impl ZonWriter
Sourcepub fn new() -> Self
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}Sourcepub fn len(&self) -> usize
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}pub fn is_empty(&self) -> bool
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
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}Sourcepub fn write_u32(&mut self, val: u32) -> u32
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}Sourcepub fn write_string(&mut self, val: &str) -> u32
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}Sourcepub fn set_root(&mut self, offset: u32)
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§
Auto Trait Implementations§
impl Freeze for ZonWriter
impl RefUnwindSafe for ZonWriter
impl Send for ZonWriter
impl Sync for ZonWriter
impl Unpin for ZonWriter
impl UnsafeUnpin for ZonWriter
impl UnwindSafe for ZonWriter
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