embytes_buffer_async/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
2
3
4
5use thiserror::Error;
6
7use crate::{inner::BufferInner, mutex::{Mutex, MutexImpl}};
8
9pub(crate) mod mutex;
10
11mod reader;
12pub use reader::*;
13
14mod writer;
15pub use writer::*;
16
17mod inner;
18
19#[cfg(feature = "embedded")]
20pub mod pipe;
21
22#[derive(Error, Debug, PartialEq)]
24#[cfg_attr(feature = "defmt", derive(defmt::Format))]
25pub enum BufferError {
26
27 #[error("Error writing to buffer: no remaining capacity")]
28 NoCapacity,
29
30 #[error("The provided slice to read from or write to has a len = 0")]
31 ProvidedSliceEmpty,
32
33 #[error("Error reading from buffer: no remaining data")]
34 NoData,
35}
36
37pub struct AsyncBuffer <const C: usize, T: AsRef<[u8]> + AsMut<[u8]>> {
39 pub(crate) inner: MutexImpl<BufferInner<C, T>>
40}
41
42impl <const C: usize, T: AsRef<[u8]> + AsMut<[u8]>> AsyncBuffer<C, T> {
43
44 pub fn new(source: T) -> Self {
47 assert!(source.as_ref().len() > 0);
48 Self {
49 inner: MutexImpl::new(BufferInner::new(source))
50 }
51 }
52
53 pub fn create_reader<'a>(&'a self) -> BufferReader<'a, C, T> {
56 BufferReader::new(self)
57 }
58
59 pub fn create_writer<'a>(&'a self) -> BufferWriter<'a, C, T> {
62 BufferWriter::new(self)
63 }
64}
65
66impl <const C: usize, const N: usize> AsyncBuffer<C, [u8; N]> {
67
68 pub fn new_stack() -> Self {
69 Self::new([0; N])
70 }
71
72}