Skip to content

Improve io and error #3

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

Merged
merged 1 commit into from
Nov 11, 2020
Merged
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
47 changes: 37 additions & 10 deletions std/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
use crate::boxed::Box;
use crate::fmt::{self, Debug, Display};
use crate::string::String;

#[cfg(feature = "std")]
pub use std::error::Error;

#[cfg(not(feature = "std"))]
pub trait Error: core::fmt::Debug + core::fmt::Display {
fn source(&self) -> Option<&(dyn Error + 'static)> {
None
}
}

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

#[cfg(not(feature = "std"))]
impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
Box::new(err)
}
}

impl<T: Error> Error for Box<T> {}

#[cfg(not(feature = "std"))]
impl Error for crate::string::String {}
impl From<String> for Box<dyn Error + Send + Sync> {
#[inline]
fn from(err: String) -> Box<dyn Error + Send + Sync> {
struct StringError(String);

impl Error for StringError {}

impl Display for StringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0, f)
}
}

#[cfg(not(feature = "std"))]
impl Error for crate::io::Error {}
// Purposefully skip printing "StringError(..)"
impl Debug for StringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}

Box::new(StringError(err))
}
}

impl<'a> From<&'a str> for Box<dyn Error + Send + Sync> {
#[inline]
fn from(err: &'a str) -> Box<dyn Error + Send + Sync> {
From::from(String::from(err))
}
}
120 changes: 0 additions & 120 deletions std/src/io.rs

This file was deleted.

Loading