basics/basics.rs
1//! An example of constructing, writing and reading a cueue
2
3fn main() {
4 // Create a cueue with capacity at least 1M.
5 // (The actual capacity will be rounded up to match system requirements, if needed)
6 // w and r are the write and read handles of the cueue, respectively.
7 // These handles can be sent between threads, but cannot be duplicated.
8 let (mut w, mut r) = cueue::cueue(1 << 20).unwrap();
9
10 // To write a cueue, first we need to get a writable slice from it:
11 let buf = w.write_chunk();
12
13 // Check if there are 9 bytes free for writing in the cueue.
14 if buf.len() >= 3 + 3 + 3 {
15 // If yes, write whatever we want
16 buf[..9].copy_from_slice(b"foobarbaz");
17
18 // When done, make the written are available for reading.
19 // Without this, the reader will not see the written but not committed changes.
20 w.commit(9);
21 }
22
23 // Now read whatever is in the queue
24 let read_result = r.read_chunk();
25 assert_eq!(read_result, b"foobarbaz");
26 println!("Read {}", String::from_utf8_lossy(read_result));
27 // Mark the previously returned slice consumed, making it available for writing.
28 r.commit();
29}