Skip to content

Commit fd2d4fa

Browse files
author
Tim Siemers
committed
added comments and better std support
1 parent d0e6784 commit fd2d4fa

File tree

4 files changed

+94
-11
lines changed

4 files changed

+94
-11
lines changed

examples/reader.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn main () {
1010
let mut buffer = Buffer::new(&mut bytes);
1111

1212
// Write some bytes
13-
buffer.write_all("abc".as_bytes()).unwrap();
13+
buffer.write_all("👌abc".as_bytes()).unwrap();
1414

1515
// try to read to a komma but there is none
1616
let reader = buffer.create_reader();
@@ -24,7 +24,7 @@ fn main () {
2424
// try to read to a komma. now there is one
2525
let reader = buffer.create_reader();
2626
let result = read_til_komma(&reader);
27-
assert_eq!(result, Some("abcdef"));
27+
assert_eq!(result, Some("👌abcdef"));
2828
drop(reader);
2929

3030
assert_eq!(buffer.data(), "1234".as_bytes());
@@ -50,8 +50,10 @@ fn read_til_komma<'a>(reader: &'a impl BufferReader) -> Option<&'a str> {
5050
if let Some(comma_position) = comma_position {
5151
let data = &str[..comma_position];
5252

53+
let bytes_read = data.as_bytes().len() + 1;
54+
5355
// Tell the reader that you have read `data.len() + 1` bytes
54-
reader.add_bytes_read(data.len() + 1);
56+
reader.add_bytes_read(bytes_read);
5557
Some(data)
5658
} else {
5759
None

examples/stack_buffer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use embytes_buffer::new_stack_buffer;
1+
use embytes_buffer::Buffer;
22
use embedded_io::{Read, Write};
33

44

55

66
fn main () {
77

88
// Create a new buffern with an array as byte source on the stack
9-
let mut buffer = new_stack_buffer::<1024>();
9+
let mut buffer = Buffer::<[u8; 1024]>::new_stack();
1010

1111
// Write some bytes to buffer
1212
buffer.write_all("hello world".as_bytes()).unwrap();

examples/writer.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ fn main () {
1313
// Create a writer, write some bytes but do not commit
1414
// writer implements DerefMut<Target = [u8]> and can be used as a mutable bytes slice
1515
let mut writer = buffer.create_writer();
16-
writer[0] = 36; // ascii '$'
16+
writer[0] = '$' as u8;
17+
18+
// The writer is dropped without committing so the write has no effect
1719
drop(writer);
1820

1921
// Create a new writer
2022
let mut writer = buffer.create_writer();
21-
writer[0] = 100; // ascii d
22-
writer[1] = 101; // ascii e
23-
writer[2] = 102; // ascii f
23+
writer[0] = 'd' as u8;
24+
writer[1] = 'e' as u8;
25+
writer[2] = 'f' as u8;
2426

2527
// Commit that 3 bytes are written
2628
// writing bytes has only an effect if the written bytes are committed

src/lib.rs

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub struct Buffer<T: AsMut<[u8]> + AsRef<[u8]>> {
5959
}
6060

6161
/// Creates a new [`Buffer`] that is backed by an owned [`u8`] array with size `N`
62+
#[deprecated]
6263
pub fn new_stack_buffer<const N: usize>() -> Buffer<[u8; N]> {
6364
Buffer::<[u8; N]> {
6465
source: [0; N],
@@ -67,6 +68,49 @@ pub fn new_stack_buffer<const N: usize>() -> Buffer<[u8; N]> {
6768
}
6869
}
6970

71+
impl <const N: usize> Buffer<[u8; N]> {
72+
73+
/// Creates a new [`Buffer`] that is backed by an owned [`u8`] array with size `N`
74+
pub fn new_stack() -> Self {
75+
Self {
76+
source: [0; N],
77+
read_position: 0,
78+
write_position: 0,
79+
}
80+
}
81+
}
82+
83+
#[cfg(feature = "std")]
84+
impl Buffer<Vec<u8>> {
85+
86+
/// Creates a new [`Buffer`] that is backed by an owned [`Vec<u8>`]
87+
pub fn new_heap(size: usize) -> Self {
88+
Self {
89+
source: vec![0; size],
90+
read_position: 0,
91+
write_position: 0,
92+
}
93+
}
94+
95+
/// Grows the buffer capacity by `grow_by` bytes
96+
pub fn grow(&mut self, grow_by: usize) {
97+
self.source.extend(
98+
std::iter::repeat_n(0, grow_by)
99+
);
100+
}
101+
102+
/// Shrink the buffer capacity by `shrink_by` bytes.
103+
/// If this would remove written data, an [`BufferError::NoCapacity`] is returned.
104+
pub fn shrink(&mut self, shrink_by: usize) -> Result<(), BufferError> {
105+
if self.remaining_capacity() < shrink_by {
106+
Err(BufferError::NoCapacity)
107+
} else {
108+
self.source.truncate(self.source.len().saturating_sub(shrink_by));
109+
Ok(())
110+
}
111+
}
112+
}
113+
70114
#[cfg(feature = "defmt")]
71115
impl <T: AsMut<[u8]> + AsRef<[u8]>> defmt::Format for Buffer<T> {
72116
fn format(&self, fmt: defmt::Formatter) {
@@ -377,7 +421,7 @@ impl <T: AsMut<[u8]> + AsRef<[u8]> + Clone> Clone for Buffer<T> {
377421
#[cfg(test)]
378422
mod tests {
379423

380-
use crate::{new_stack_buffer, Buffer, BufferError};
424+
use crate::{Buffer, BufferError};
381425

382426
#[test]
383427
fn test_std_write_high_cap() {
@@ -472,10 +516,12 @@ mod tests {
472516
#[test]
473517
fn test_stack_buffer() {
474518

475-
let mut buf = new_stack_buffer::<4>();
519+
let mut buf = Buffer::<[u8; 4]>::new_stack();
476520

477521
buf.write_base(&[1, 2, 3, 4]).unwrap();
478522

523+
assert!(! buf.has_remaining_capacity());
524+
assert_eq!(buf.remaining_len(), 4);
479525
}
480526

481527
#[test]
@@ -506,4 +552,37 @@ mod tests {
506552
let res = buf.skip(5);
507553
assert_eq!(res, Err(BufferError::NoData));
508554
}
555+
556+
#[cfg(feature = "std")]
557+
#[test]
558+
fn test_vec_source_grow() {
559+
use std::io::Write;
560+
561+
let mut buffer = Buffer::new_heap(8);
562+
buffer.write_all(&[1, 2, 3, 4, 5, 6, 7, 8]).unwrap();
563+
564+
assert!(buffer.write_all(&[9]).is_err());
565+
566+
buffer.grow(1);
567+
buffer.write_all(&[9]).unwrap();
568+
assert!(buffer.write_all(&[10]).is_err());
569+
570+
}
571+
572+
#[cfg(feature = "std")]
573+
#[test]
574+
fn test_vec_source_shrink() {
575+
use std::io::Write;
576+
577+
let mut buffer = Buffer::new_heap(8);
578+
buffer.write_all(&[1, 2, 3, 4]).unwrap();
579+
assert_eq!(buffer.remaining_capacity(), 4);
580+
581+
buffer.shrink(4).unwrap();
582+
assert_eq!(buffer.remaining_capacity(), 0);
583+
assert!(buffer.write_all(&[5]).is_err());
584+
585+
assert!(buffer.shrink(1).is_err());
586+
587+
}
509588
}

0 commit comments

Comments
 (0)