2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-04-28 11:16:50 +00:00

Wrap code more clearly in cfg blocks

This commit is contained in:
Pascal Hertleif 2019-11-17 23:11:11 +01:00
parent 8ce3e78952
commit 99ddfb3f93
2 changed files with 48 additions and 41 deletions

View file

@ -1,4 +1,3 @@
use std::{error::Error, fmt, io};
use crate::utils::VerboseErrorExt; use crate::utils::VerboseErrorExt;
/// Wrap `std::io::Error` with additional message /// Wrap `std::io::Error` with additional message
@ -6,27 +5,28 @@ use crate::utils::VerboseErrorExt;
/// *Note* Only active when `verbose-errors` feature is enabled for this crate! /// *Note* Only active when `verbose-errors` feature is enabled for this crate!
/// ///
/// Keeps the original error kind and stores the original I/O error as `source`. /// Keeps the original error kind and stores the original I/O error as `source`.
impl<T> VerboseErrorExt for Result<T, io::Error> { impl<T> VerboseErrorExt for Result<T, std::io::Error> {
#[cfg(feature = "verbose-errors")]
fn verbose_context(self, message: impl Fn() -> String) -> Self { fn verbose_context(self, message: impl Fn() -> String) -> Self {
if cfg!(feature = "verbose-errors") { self.map_err(|e| verbose::Error::wrap(e, message()))
self.map_err(|e| VerboseError::wrap(e, message()))
} else {
self
}
} }
} }
#[cfg(feature = "verbose-errors")]
mod verbose {
use std::{error::Error as StdError, fmt, io};
#[derive(Debug)] #[derive(Debug)]
struct VerboseError { pub(crate) struct Error {
source: io::Error, source: io::Error,
message: String, message: String,
} }
impl VerboseError { impl Error {
fn wrap(source: io::Error, message: impl Into<String>) -> io::Error { pub(crate) fn wrap(source: io::Error, message: impl Into<String>) -> io::Error {
io::Error::new( io::Error::new(
source.kind(), source.kind(),
VerboseError { Error {
source, source,
message: message.into(), message: message.into(),
}, },
@ -34,18 +34,19 @@ impl VerboseError {
} }
} }
impl fmt::Display for VerboseError { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message) write!(f, "{}", self.message)
} }
} }
impl Error for VerboseError { impl StdError for Error {
fn description(&self) -> &str { fn description(&self) -> &str {
self.source.description() self.source.description()
} }
fn source(&self) -> Option<&(dyn Error + 'static)> { fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(&self.source) Some(&self.source)
} }
} }
}

View file

@ -56,8 +56,14 @@ pub fn random(n: u32) -> u32 {
/// ///
/// *Note for implementors:* The given closure must only be executed when /// *Note for implementors:* The given closure must only be executed when
/// `verbose-errors` feature is enabled for this crate! /// `verbose-errors` feature is enabled for this crate!
pub(crate) trait VerboseErrorExt { pub(crate) trait VerboseErrorExt: Sized {
#[cfg(feature = "verbose-errors")]
fn verbose_context(self, message: impl Fn() -> String) -> Self; fn verbose_context(self, message: impl Fn() -> String) -> Self;
#[cfg(not(feature = "verbose-errors"))]
fn verbose_context(self, _: impl Fn() -> String) -> Self {
self
}
} }
/// Defers evaluation of a block of code until the end of the scope. /// Defers evaluation of a block of code until the end of the scope.