Skip to content

Commit f9ef162

Browse files
committed
fix: simplify the no-std error code
As the no-std `Error` is a trimmed down version specifically for this library, it can be simplified. Also add some doc comments back in.
1 parent d2b2fc8 commit f9ef162

2 files changed

Lines changed: 17 additions & 40 deletions

File tree

src/no_std_io/error.rs

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,14 @@ use core::{fmt, result};
33
pub(super) type Result<T> = result::Result<T, Error>;
44

55
/// A minimal backfill of the [`std::io::Error`] for `no_std` environments.
6-
pub struct Error {
7-
repr: Repr,
8-
}
9-
10-
impl core::error::Error for Error {}
11-
12-
impl fmt::Debug for Error {
13-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14-
fmt::Debug::fmt(&self.repr, f)
15-
}
16-
}
17-
18-
enum Repr {
19-
Custom(Custom),
20-
}
21-
226
#[derive(Debug)]
23-
struct Custom {
7+
pub struct Error {
248
kind: ErrorKind,
259
error: &'static str,
2610
}
2711

12+
impl core::error::Error for Error {}
13+
2814
/// A list specifying general categories of I/O error.
2915
///
3016
/// This list is intended to grow over time and it is not recommended to
@@ -66,35 +52,17 @@ impl Error {
6652
/// originate from the OS itself. The `error` argument is an arbitrary
6753
/// payload which will be contained in this [`Error`].
6854
pub(super) fn new(kind: ErrorKind, error: &'static str) -> Error {
69-
Self::_new(kind, error)
70-
}
71-
72-
fn _new(kind: ErrorKind, error: &'static str) -> Error {
73-
Error {
74-
repr: Repr::Custom(Custom { kind, error }),
75-
}
55+
Error { kind, error }
7656
}
7757

7858
/// Returns the corresponding [`ErrorKind`] for this error.
7959
pub(super) fn kind(&self) -> ErrorKind {
80-
match self.repr {
81-
Repr::Custom(ref c) => c.kind,
82-
}
83-
}
84-
}
85-
86-
impl fmt::Debug for Repr {
87-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
88-
match *self {
89-
Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt),
90-
}
60+
self.kind
9161
}
9262
}
9363

9464
impl fmt::Display for Error {
9565
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
96-
match self.repr {
97-
Repr::Custom(ref c) => c.error.fmt(fmt),
98-
}
66+
self.error.fmt(fmt)
9967
}
10068
}

src/no_std_io/impls.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//! Ported from <https://docs.rs/crate/core2/0.4.0/source/src/io/impls.rs>
2-
31
use super::error::{Error, ErrorKind, Result};
42
use super::traits::{Read, Write};
53

@@ -22,6 +20,10 @@ impl<W: Write + ?Sized> Write for &mut W {
2220
}
2321
}
2422

23+
/// Read is implemented for `&[u8]` by copying from the slice.
24+
///
25+
/// Note that reading updates the slice to point to the yet unread part.
26+
/// The slice will be empty when EOF is reached.
2527
impl Read for &[u8] {
2628
#[inline]
2729
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
@@ -65,6 +67,11 @@ impl Read for &[u8] {
6567
}
6668
}
6769

70+
/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
71+
/// its data.
72+
///
73+
/// Note that writing updates the slice to point to the yet unwritten part.
74+
/// The slice will be empty when it has been completely overwritten.
6875
impl Write for &mut [u8] {
6976
#[inline]
7077
fn write(&mut self, data: &[u8]) -> Result<usize> {
@@ -88,6 +95,8 @@ impl Write for &mut [u8] {
8895
}
8996
}
9097

98+
/// Write is implemented for `Vec<u8>` by appending to the vector.
99+
/// The vector will grow as needed.
91100
#[cfg(feature = "alloc")]
92101
impl Write for alloc::vec::Vec<u8> {
93102
#[inline]

0 commit comments

Comments
 (0)