pub struct ByteQueue { /* private fields */ }
Expand description
The main queue type. Read the crate documentation for further information and usage examples.
Implementations§
source§impl ByteQueue
impl ByteQueue
sourcepub unsafe fn create(mem: *mut u8, mem_len: usize) -> Self
pub unsafe fn create(mem: *mut u8, mem_len: usize) -> Self
Creates a new queue in the given memory region and initializes both pointers.
§Safety
This method has to be called before the other processor tries to access the queue because the other processor might access an uninitialized memory region otherwise which will most likely result in crashes.
Obviously, the memory pointer and the memory region length must be correct, reserved for this purpose and known to the other processor.
sourcepub unsafe fn attach(mem: *mut u8, mem_len: usize) -> Self
pub unsafe fn attach(mem: *mut u8, mem_len: usize) -> Self
Attaches to a queue which has previously been initialized by ByteQueue::create
,
possibly by an other processor.
§Safety
This method must not be called before the other processor has properly initialized the queue because this will most likely result in crashes.
Obviously, the memory pointer rand the memory region length must be correct, reserved for this purpose and known to the other processor.
sourcepub fn space(&self) -> usize
pub fn space(&self) -> usize
Returns the size of the available space, which can be written into the queue.
sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the size of the written messages, which are to be consumed or read.
sourcepub fn nb_write(&mut self, data: &[u8]) -> Result<(), Infallible>
pub fn nb_write(&mut self, data: &[u8]) -> Result<(), Infallible>
Attempts to write data to the queue in non-blocking mode.
If there is not enough space to write the entire data, returns an error (WouldBlock
).
On success, writes the data into the queue, using memory fences for proper synchronization.
sourcepub fn blocking_write(&mut self, data: &[u8])
pub fn blocking_write(&mut self, data: &[u8])
Blocks until there is enough space in the queue to write the data.
Once space is available, writes the data to the queue using nb_write
.
sourcepub fn nb_read(&mut self, buf: &mut [u8]) -> Result<(), Infallible>
pub fn nb_read(&mut self, buf: &mut [u8]) -> Result<(), Infallible>
Attempts to read data from the queue in non-blocking mode.
If there is not enough data in buf.len()
size available to be read, returns an error
(WouldBlock
). On success, reads the data in buf.len()
size from the queue into the
provided buffer, using memory fences for synchronization.
sourcepub fn consume_at_most(&mut self, buf: &mut [u8]) -> usize
pub fn consume_at_most(&mut self, buf: &mut [u8]) -> usize
Reads up to the available data into the provided buffer, returning the number of bytes read.
This method reads at most the size of the buffer or the amount of available data, whichever is smaller.
sourcepub fn blocking_read(&mut self, buf: &mut [u8])
pub fn blocking_read(&mut self, buf: &mut [u8])
Blocks until there is enough data in the queue to fill the buffer.
Once sufficient data is available, reads the data using nb_read
.
Trait Implementations§
source§impl Write for ByteQueue
impl Write for ByteQueue
impl Send for ByteQueue
The ByteQueue
is not automatically Send
because it contains raw pointers. According to
the Nomicon, raw pointers
could be considered “fine […] to be marked as thread safe” but their “non-trivial untracked
ownership” requires to decide manually if a type containing raw pointers is Send
.
Regarding the ByteQueue
, every instantiation and usage is so unsafe that the user needs to be
careful anyway. If all usage requirements are still met, the ByteQueue
can safely be used
from another thread, too. Therefore, Send
is implemented manually here to increase the
flexibility for the users.
An other perspective on implementing Send
is that the ByteQueue
is fundamentally designed
to be used for inter-processor communication which is in many ways equivalent to inter-thread
operation. Thus, implementing Send
does not introduce any new requirements.