Writer

Struct Writer 

Source
pub struct Writer<T> { /* private fields */ }
Expand description

Writer of a Cueue.

See examples/ for usage.

Implementations§

Source§

impl<T> Writer<T>

Source

pub fn capacity(&self) -> usize

Maximum number of elements the referenced cueue can hold.

Source

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
Hide additional 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}
Source

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
Hide additional 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}
Source

pub fn is_abandoned(&self) -> bool

Returns true, if the Reader counterpart was dropped.

Source

pub fn push(&mut self, t: T) -> Result<(), T>

Write and commit a single element, or return it if the queue was full.

Trait Implementations§

Source§

impl<T: Send> Send for Writer<T>

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> 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.