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