Skip to content

Commit 6eb30c5

Browse files
committed
io: add impls for slices.
1 parent 6e27094 commit 6eb30c5

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

embedded-io/src/impls/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod slice_mut;
2+
mod slice_ref;

embedded-io/src/impls/slice_mut.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::{Io, Write};
2+
use core::mem;
3+
4+
impl Io for &mut [u8] {
5+
type Error = core::convert::Infallible;
6+
}
7+
8+
/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
9+
/// its data.
10+
///
11+
/// Note that writing updates the slice to point to the yet unwritten part.
12+
/// The slice will be empty when it has been completely overwritten.
13+
///
14+
/// If the number of bytes to be written exceeds the size of the slice, write operations will
15+
/// return short writes: ultimately, `Ok(0)`; in this situation, `write_all` returns an error of
16+
/// kind `ErrorKind::WriteZero`.
17+
impl Write for &mut [u8] {
18+
#[inline]
19+
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
20+
let amt = core::cmp::min(buf.len(), self.len());
21+
let (a, b) = mem::take(self).split_at_mut(amt);
22+
a.copy_from_slice(&buf[..amt]);
23+
*self = b;
24+
Ok(amt)
25+
}
26+
27+
#[inline]
28+
fn flush(&mut self) -> Result<(), Self::Error> {
29+
Ok(())
30+
}
31+
}

embedded-io/src/impls/slice_ref.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use crate::{BufRead, Io, Read};
2+
3+
impl Io for &[u8] {
4+
type Error = core::convert::Infallible;
5+
}
6+
7+
/// Read is implemented for `&[u8]` by copying from the slice.
8+
///
9+
/// Note that reading updates the slice to point to the yet unread part.
10+
/// The slice will be empty when EOF is reached.
11+
impl Read for &[u8] {
12+
#[inline]
13+
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
14+
let amt = core::cmp::min(buf.len(), self.len());
15+
let (a, b) = self.split_at(amt);
16+
17+
// First check if the amount of bytes we want to read is small:
18+
// `copy_from_slice` will generally expand to a call to `memcpy`, and
19+
// for a single byte the overhead is significant.
20+
if amt == 1 {
21+
buf[0] = a[0];
22+
} else {
23+
buf[..amt].copy_from_slice(a);
24+
}
25+
26+
*self = b;
27+
Ok(amt)
28+
}
29+
}
30+
31+
impl BufRead for &[u8] {
32+
#[inline]
33+
fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
34+
Ok(*self)
35+
}
36+
37+
#[inline]
38+
fn consume(&mut self, amt: usize) {
39+
*self = &self[amt..];
40+
}
41+
}

embedded-io/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use core::fmt;
66

7+
mod impls;
8+
79
/// Enumeration of possible methods to seek within an I/O object.
810
///
911
/// Semantics are the same as [`std::io::SeekFrom`], check its documentation for details.

0 commit comments

Comments
 (0)