Skip to content

Commit ba02096

Browse files
authored
Merge pull request #704 from Conaclos/slice_specialized_impls
[io] Specialize `read_exact` and `write_all` for slices and `Vec`
2 parents 74c8db4 + 43a1bbb commit ba02096

File tree

6 files changed

+50
-0
lines changed

6 files changed

+50
-0
lines changed

embedded-io-async/src/impls/slice_mut.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,13 @@ impl Write for &mut [u8] {
2424
*self = b;
2525
Ok(amt)
2626
}
27+
28+
#[inline]
29+
async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
30+
if self.len() < buf.len() {
31+
return Err(SliceWriteError::Full);
32+
}
33+
self.write(buf).await?;
34+
Ok(())
35+
}
2736
}

embedded-io-async/src/impls/slice_ref.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ impl Read for &[u8] {
2222
*self = b;
2323
Ok(amt)
2424
}
25+
26+
async fn read_exact(
27+
&mut self,
28+
buf: &mut [u8],
29+
) -> Result<(), embedded_io::ReadExactError<Self::Error>> {
30+
if self.len() < buf.len() {
31+
return Err(crate::ReadExactError::UnexpectedEof);
32+
}
33+
self.read(buf).await?;
34+
Ok(())
35+
}
2536
}
2637

2738
impl BufRead for &[u8] {

embedded-io-async/src/impls/vec.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@ impl Write for Vec<u8> {
99
self.extend_from_slice(buf);
1010
Ok(buf.len())
1111
}
12+
13+
#[inline]
14+
async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
15+
self.write(buf).await?;
16+
Ok(())
17+
}
1218
}

embedded-io/src/impls/slice_mut.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ impl Write for &mut [u8] {
4242
Ok(amt)
4343
}
4444

45+
#[inline]
46+
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
47+
if self.len() < buf.len() {
48+
return Err(SliceWriteError::Full);
49+
}
50+
self.write(buf)?;
51+
Ok(())
52+
}
53+
4554
#[inline]
4655
fn flush(&mut self) -> Result<(), Self::Error> {
4756
Ok(())

embedded-io/src/impls/slice_ref.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ impl Read for &[u8] {
2626
*self = b;
2727
Ok(amt)
2828
}
29+
30+
#[inline]
31+
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), crate::ReadExactError<Self::Error>> {
32+
if self.len() < buf.len() {
33+
return Err(crate::ReadExactError::UnexpectedEof);
34+
}
35+
self.read(buf)?;
36+
Ok(())
37+
}
2938
}
3039

3140
impl BufRead for &[u8] {

embedded-io/src/impls/vec.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ impl Write for Vec<u8> {
1414
Ok(buf.len())
1515
}
1616

17+
#[inline]
18+
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
19+
self.write(buf)?;
20+
Ok(())
21+
}
22+
1723
#[inline]
1824
fn flush(&mut self) -> Result<(), Self::Error> {
1925
Ok(())

0 commit comments

Comments
 (0)