Skip to content

Commit 27cbded

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

File tree

5 files changed

+67
-0
lines changed

5 files changed

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

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)