Skip to content

Commit 94633ec

Browse files
committed
Improve io and error
1 parent d522da8 commit 94633ec

File tree

5 files changed

+854
-135
lines changed

5 files changed

+854
-135
lines changed

std/src/error.rs

+37-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,54 @@
11
use crate::boxed::Box;
2+
use crate::fmt::{self, Debug, Display};
3+
use crate::string::String;
24

3-
#[cfg(feature = "std")]
4-
pub use std::error::Error;
5-
6-
#[cfg(not(feature = "std"))]
75
pub trait Error: core::fmt::Debug + core::fmt::Display {
86
fn source(&self) -> Option<&(dyn Error + 'static)> {
97
None
108
}
119
}
1210

13-
#[cfg(not(feature = "std"))]
1411
impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
1512
fn from(err: E) -> Self {
1613
Box::new(err)
1714
}
1815
}
1916

20-
#[cfg(not(feature = "std"))]
17+
impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
18+
fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
19+
Box::new(err)
20+
}
21+
}
22+
2123
impl<T: Error> Error for Box<T> {}
2224

23-
#[cfg(not(feature = "std"))]
24-
impl Error for crate::string::String {}
25+
impl From<String> for Box<dyn Error + Send + Sync> {
26+
#[inline]
27+
fn from(err: String) -> Box<dyn Error + Send + Sync> {
28+
struct StringError(String);
29+
30+
impl Error for StringError {}
31+
32+
impl Display for StringError {
33+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34+
Display::fmt(&self.0, f)
35+
}
36+
}
2537

26-
#[cfg(not(feature = "std"))]
27-
impl Error for crate::io::Error {}
38+
// Purposefully skip printing "StringError(..)"
39+
impl Debug for StringError {
40+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41+
Debug::fmt(&self.0, f)
42+
}
43+
}
44+
45+
Box::new(StringError(err))
46+
}
47+
}
48+
49+
impl<'a> From<&'a str> for Box<dyn Error + Send + Sync> {
50+
#[inline]
51+
fn from(err: &'a str) -> Box<dyn Error + Send + Sync> {
52+
From::from(String::from(err))
53+
}
54+
}

std/src/io.rs

-120
This file was deleted.

0 commit comments

Comments
 (0)