Skip to content

Commit 032cec7

Browse files
committed
Add optional embedded_io::Write impl for Vec.
1 parent 2db8844 commit 032cec7

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ jobs:
176176
cargo check --target="${target}" --features="portable-atomic-critical-section"
177177
cargo check --target="${target}" --features="serde"
178178
cargo check --target="${target}" --features="ufmt"
179+
cargo check --target="${target}" --features="embedded-io"
179180
env:
180181
target: ${{ matrix.target }}
181182

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added optional `embedded_io::Write` impl for `Vec`.
13+
1014
### Changed
1115

1216
- `bytes::BufMut` is now implemented on `VecInner`.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ ufmt = ["dep:ufmt", "dep:ufmt-write"]
3838
# Implement `defmt::Format`.
3939
defmt = ["dep:defmt"]
4040

41+
# Implement `embedded_io::Write`
42+
embedded-io = ["dep:embedded-io"]
43+
4144
# Enable larger MPMC sizes.
4245
mpmc_large = []
4346

@@ -54,6 +57,7 @@ serde = { version = "1", optional = true, default-features = false }
5457
ufmt = { version = "0.2", optional = true }
5558
ufmt-write = { version = "0.1", optional = true }
5659
defmt = { version = "1.0.1", optional = true }
60+
embedded-io = { version = "0.6", optional = true }
5761

5862
# for the pool module
5963
[target.'cfg(any(target_arch = "arm", target_pointer_width = "32", target_pointer_width = "64"))'.dependencies]

src/embedded_io.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use crate::{
2+
len_type::LenType,
3+
vec::{VecInner, VecStorage},
4+
CapacityError,
5+
};
6+
use embedded_io::{Error, ErrorKind, ErrorType, Write};
7+
8+
impl Error for CapacityError {
9+
fn kind(&self) -> ErrorKind {
10+
ErrorKind::OutOfMemory
11+
}
12+
}
13+
14+
impl<LenT: LenType, S: VecStorage<u8> + ?Sized> ErrorType for VecInner<u8, LenT, S> {
15+
type Error = CapacityError;
16+
}
17+
18+
impl<LenT: LenType, S: VecStorage<u8> + ?Sized> Write for VecInner<u8, LenT, S> {
19+
#[inline]
20+
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
21+
self.extend_from_slice(buf)?;
22+
Ok(buf.len())
23+
}
24+
25+
#[inline]
26+
fn flush(&mut self) -> Result<(), Self::Error> {
27+
Ok(())
28+
}
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use crate::Vec;
34+
use embedded_io::{Error, ErrorKind, Write};
35+
36+
fn write(w: &mut impl Write, data: &[u8]) -> Result<(), ErrorKind> {
37+
w.write_all(data).map_err(|e| e.kind())
38+
}
39+
40+
#[test]
41+
fn test_write() {
42+
let mut v: Vec<u8, 4> = Vec::new();
43+
assert_eq!(v.len(), 0);
44+
write(&mut v, &[1, 2]).unwrap();
45+
assert_eq!(v.len(), 2);
46+
assert_eq!(v.as_slice(), &[1, 2]);
47+
write(&mut v, &[3]).unwrap();
48+
assert_eq!(v.len(), 3);
49+
assert_eq!(v.as_slice(), &[1, 2, 3]);
50+
assert!(write(&mut v, &[4, 5]).is_err());
51+
assert_eq!(v.len(), 3);
52+
assert_eq!(v.as_slice(), &[1, 2, 3]);
53+
}
54+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ pub mod spsc;
237237
#[cfg(feature = "ufmt")]
238238
mod ufmt;
239239

240+
#[cfg(feature = "embedded-io")]
241+
mod embedded_io;
242+
240243
/// Implementation details for macros.
241244
/// Do not use. Used for macros only. Not covered by semver guarantees.
242245
#[doc(hidden)]

0 commit comments

Comments
 (0)