Skip to content

implement Write for uninitialized slices #674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
29 changes: 27 additions & 2 deletions embedded-io-async/src/impls/slice_mut.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::mem;
use core::mem::{self, MaybeUninit};
use embedded_io::SliceWriteError;

use crate::Write;
Expand All @@ -20,7 +20,32 @@ impl Write for &mut [u8] {
return Err(SliceWriteError::Full);
}
let (a, b) = mem::take(self).split_at_mut(amt);
a.copy_from_slice(&buf[..amt]);
a.copy_from_slice(buf.split_at(amt).0);
*self = b;
Ok(amt)
}
}

/// Write is implemented for `&mut [MaybeUninit<u8>]` by copying into the slice, initializing
/// & overwriting its data.
///
/// Note that writing updates the slice to point to the yet unwritten part.
/// The slice will be empty when it has been completely overwritten.
///
/// If the number of bytes to be written exceeds the size of the slice, write operations will
/// return short writes: ultimately, `Ok(0)`; in this situation, `write_all` returns an error of
/// kind `ErrorKind::WriteZero`.
impl Write for &mut [MaybeUninit<u8>] {
#[inline]
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let amt = core::cmp::min(buf.len(), self.len());
if !buf.is_empty() && amt == 0 {
return Err(SliceWriteError::Full);
}
let (a, b) = mem::take(self).split_at_mut(amt);
unsafe {
core::ptr::copy_nonoverlapping(buf.as_ptr(), a.as_mut_ptr() as *mut u8, amt);
}
*self = b;
Ok(amt)
}
Expand Down
52 changes: 46 additions & 6 deletions embedded-io/src/impls/slice_mut.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Error, ErrorKind, ErrorType, SliceWriteError, Write, WriteReady};
use core::mem;
use core::mem::{self, MaybeUninit};

impl Error for SliceWriteError {
fn kind(&self) -> ErrorKind {
Expand All @@ -9,10 +9,6 @@ impl Error for SliceWriteError {
}
}

impl ErrorType for &mut [u8] {
type Error = SliceWriteError;
}

impl core::fmt::Display for SliceWriteError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{self:?}")
Expand All @@ -21,6 +17,10 @@ impl core::fmt::Display for SliceWriteError {

impl core::error::Error for SliceWriteError {}

impl ErrorType for &mut [u8] {
type Error = SliceWriteError;
}

/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
/// its data.
///
Expand All @@ -37,7 +37,7 @@ impl Write for &mut [u8] {
return Err(SliceWriteError::Full);
}
let (a, b) = mem::take(self).split_at_mut(amt);
a.copy_from_slice(&buf[..amt]);
a.copy_from_slice(buf.split_at(amt).0);
*self = b;
Ok(amt)
}
Expand All @@ -54,3 +54,43 @@ impl WriteReady for &mut [u8] {
Ok(true)
}
}

impl ErrorType for &mut [MaybeUninit<u8>] {
type Error = SliceWriteError;
}

/// Write is implemented for `&mut [MaybeUninit<u8>]` by copying into the slice, initializing
/// & overwriting its data.
///
/// Note that writing updates the slice to point to the yet unwritten part.
/// The slice will be empty when it has been completely overwritten.
///
/// If the number of bytes to be written exceeds the size of the slice, write operations will
/// return short writes: ultimately, a `SliceWriteError::Full`.
impl Write for &mut [MaybeUninit<u8>] {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let amt = core::cmp::min(buf.len(), self.len());
if !buf.is_empty() && amt == 0 {
return Err(SliceWriteError::Full);
}
let (a, b) = mem::take(self).split_at_mut(amt);
unsafe {
core::ptr::copy_nonoverlapping(buf.as_ptr(), a.as_mut_ptr() as *mut u8, amt);
}
*self = b;
Ok(amt)
}

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

impl WriteReady for &mut [MaybeUninit<u8>] {
#[inline]
fn write_ready(&mut self) -> Result<bool, Self::Error> {
Ok(true)
}
}