pub struct Writer<T> { /* private fields */ }Expand description
Writer of a Cueue.
See examples/ for usage.
Implementations§
Source§impl<T> Writer<T>
impl<T> Writer<T>
Sourcepub fn write_chunk(&mut self) -> &mut [T]
pub fn write_chunk(&mut self) -> &mut [T]
Get a writable slice of maximum available size.
The elements in the returned slice are either default initialized (never written yet) or are the result of previous writes. The writer is free to overwrite or reuse them.
After write, commit must be called, to make the written elements
available for reading.
Examples found in repository?
examples/ipc_write.rs (line 27)
14fn main() -> std::io::Result<()> {
15 let path = CString::new("cueue_ipc").unwrap();
16 let mode = (libc::S_IRUSR | libc::S_IWUSR) as Mode;
17 let f = unsafe { libc::shm_open(path.as_ptr(), libc::O_RDWR | libc::O_CREAT, mode) };
18 if f < 0 {
19 return Err(std::io::Error::last_os_error());
20 }
21
22 let (mut w, _) = cueue::cueue_in_fd(f, Some(1 << 20))?;
23
24 loop {
25 print!("> ");
26 std::io::stdout().flush()?;
27 let buf = w.write_chunk();
28 match std::io::stdin().read(buf) {
29 Ok(0) | Err(_) => break,
30 Ok(n) => {
31 w.commit(n);
32 }
33 }
34 }
35
36 unsafe {
37 libc::shm_unlink(path.as_ptr());
38 }
39
40 Ok(())
41}More examples
examples/basics.rs (line 11)
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}Sourcepub fn commit(&mut self, n: usize) -> usize
pub fn commit(&mut self, n: usize) -> usize
Make n number of elements, written to the slice returned by write_chunk
available for reading.
n is checked: if too large, gets truncated to the maximum committable size.
Returns the number of committed elements.
Examples found in repository?
examples/ipc_write.rs (line 31)
14fn main() -> std::io::Result<()> {
15 let path = CString::new("cueue_ipc").unwrap();
16 let mode = (libc::S_IRUSR | libc::S_IWUSR) as Mode;
17 let f = unsafe { libc::shm_open(path.as_ptr(), libc::O_RDWR | libc::O_CREAT, mode) };
18 if f < 0 {
19 return Err(std::io::Error::last_os_error());
20 }
21
22 let (mut w, _) = cueue::cueue_in_fd(f, Some(1 << 20))?;
23
24 loop {
25 print!("> ");
26 std::io::stdout().flush()?;
27 let buf = w.write_chunk();
28 match std::io::stdin().read(buf) {
29 Ok(0) | Err(_) => break,
30 Ok(n) => {
31 w.commit(n);
32 }
33 }
34 }
35
36 unsafe {
37 libc::shm_unlink(path.as_ptr());
38 }
39
40 Ok(())
41}More examples
examples/basics.rs (line 20)
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}Sourcepub fn is_abandoned(&self) -> bool
pub fn is_abandoned(&self) -> bool
Returns true, if the Reader counterpart was dropped.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Writer<T>
impl<T> RefUnwindSafe for Writer<T>where
T: RefUnwindSafe,
impl<T> !Sync for Writer<T>
impl<T> Unpin for Writer<T>
impl<T> UnwindSafe for Writer<T>where
T: RefUnwindSafe,
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