Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ jobs:
cargo check --target="${target}" --features="portable-atomic-critical-section"
cargo check --target="${target}" --features="serde"
cargo check --target="${target}" --features="ufmt"
cargo check --target="${target}" --features="embedded-io"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the embedded-io feature to the tests and to the miri test runs too

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you want this done? None of the other features appears to be tested in this manner, so I'm not sure what you expect.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the embedded-io feature line 51 and 87 of the .github/workflows/build.yml file.

env:
target: ${{ matrix.target }}

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- Added optional `embedded_io::Write` impl for `Vec`.

### Changed

- `bytes::BufMut` is now implemented on `VecInner`.
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ ufmt = ["dep:ufmt", "dep:ufmt-write"]
# Implement `defmt::Format`.
defmt = ["dep:defmt"]

# Implement `embedded_io::Write`
embedded-io = ["dep:embedded-io"]

# Enable larger MPMC sizes.
mpmc_large = []

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

# for the pool module
[target.'cfg(any(target_arch = "arm", target_pointer_width = "32", target_pointer_width = "64"))'.dependencies]
Expand Down
55 changes: 55 additions & 0 deletions src/embedded_io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use embedded_io::{Error, ErrorKind, ErrorType, Write};

use crate::{
len_type::LenType,
vec::{VecInner, VecStorage},
CapacityError,
};

impl Error for CapacityError {
fn kind(&self) -> ErrorKind {
ErrorKind::OutOfMemory
}
}

impl<LenT: LenType, S: VecStorage<u8> + ?Sized> ErrorType for VecInner<u8, LenT, S> {
type Error = CapacityError;
}

impl<LenT: LenType, S: VecStorage<u8> + ?Sized> Write for VecInner<u8, LenT, S> {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
self.extend_from_slice(buf)?;
Ok(buf.len())
}

#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::Vec;
use embedded_io::{Error, ErrorKind, Write};

fn write(w: &mut impl Write, data: &[u8]) -> Result<(), ErrorKind> {
w.write_all(data).map_err(|e| e.kind())
}

#[test]
fn test_write() {
let mut v: Vec<u8, 4> = Vec::new();
assert_eq!(v.len(), 0);
write(&mut v, &[1, 2]).unwrap();
assert_eq!(v.len(), 2);
assert_eq!(v.as_slice(), &[1, 2]);
write(&mut v, &[3]).unwrap();
assert_eq!(v.len(), 3);
assert_eq!(v.as_slice(), &[1, 2, 3]);
assert!(write(&mut v, &[4, 5]).is_err());
assert_eq!(v.len(), 3);
assert_eq!(v.as_slice(), &[1, 2, 3]);
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ pub mod spsc;
#[cfg(feature = "ufmt")]
mod ufmt;

#[cfg(feature = "embedded-io")]
mod embedded_io;

/// Implementation details for macros.
/// Do not use. Used for macros only. Not covered by semver guarantees.
#[doc(hidden)]
Expand Down