Move trait bounds to where-clauses

I find it more readable how this appears in rustdoc.
This commit is contained in:
David Tolnay 2019-10-04 23:56:18 -04:00
parent 8da8c97e56
commit 7dcc85121b
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 69 additions and 18 deletions

View File

@ -8,7 +8,10 @@ pub trait AsError {
fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static);
}
impl<T: StdError + Send + Sync + 'static> AsError for T {
impl<T> AsError for T
where
T: StdError + Send + Sync + 'static,
{
fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {
self
}

View File

@ -7,7 +7,9 @@ use crate::Error;
/// Provides the `context` method for `Result`.
pub trait Context<T, E> {
/// Wrap the error value with additional context.
fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Error>;
fn context<C>(self, context: C) -> Result<T, Error>
where
C: Display + Send + Sync + 'static;
/// Wrap the error value with additional context lazily.
fn with_context<C, F>(self, f: F) -> Result<T, Error>
@ -16,8 +18,14 @@ pub trait Context<T, E> {
F: FnOnce(&E) -> C;
}
impl<T, E: StdError + Send + Sync + 'static> Context<T, E> for Result<T, E> {
fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Error> {
impl<T, E> Context<T, E> for Result<T, E>
where
E: StdError + Send + Sync + 'static,
{
fn context<C>(self, context: C) -> Result<T, Error>
where
C: Display + Send + Sync + 'static,
{
self.map_err(|error| Error::from(ContextError { error, context }))
}
@ -36,7 +44,10 @@ impl<T, E: StdError + Send + Sync + 'static> Context<T, E> for Result<T, E> {
}
impl<T> Context<T, Error> for Result<T, Error> {
fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Error> {
fn context<C>(self, context: C) -> Result<T, Error>
where
C: Display + Send + Sync + 'static,
{
self.map_err(|error| Error::from(ContextError { error, context }))
}
@ -59,19 +70,30 @@ struct ContextError<E, C> {
context: C,
}
impl<E: Debug, C: Display> Debug for ContextError<E, C> {
impl<E, C> Debug for ContextError<E, C>
where
E: Debug,
C: Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}\n\n{}", self.error, self.context)
}
}
impl<E, C: Display> Display for ContextError<E, C> {
impl<E, C> Display for ContextError<E, C>
where
C: Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.context, f)
}
}
impl<E: StdError + 'static, C: Display> StdError for ContextError<E, C> {
impl<E, C> StdError for ContextError<E, C>
where
E: StdError + 'static,
C: Display,
{
fn backtrace(&self) -> Option<&Backtrace> {
self.error.backtrace()
}
@ -85,7 +107,10 @@ impl<E: StdError + 'static, C: Display> StdError for ContextError<E, C> {
}
}
impl<C: Display> StdError for ContextError<Error, C> {
impl<C> StdError for ContextError<Error, C>
where
C: Display,
{
fn backtrace(&self) -> Option<&Backtrace> {
Some(self.error.backtrace())
}

View File

@ -100,12 +100,18 @@ impl Error {
}
/// Returns `true` if `E` is the type wrapped by this error object.
pub fn is<E: Display + Debug + Send + Sync + 'static>(&self) -> bool {
pub fn is<E>(&self) -> bool
where
E: Display + Debug + Send + Sync + 'static,
{
TypeId::of::<E>() == self.inner.type_id
}
/// Attempt to downcast the error object to a concrete type.
pub fn downcast<E: Display + Debug + Send + Sync + 'static>(self) -> Result<E, Error> {
pub fn downcast<E>(self) -> Result<E, Error>
where
E: Display + Debug + Send + Sync + 'static,
{
if let Some(error) = self.downcast_ref::<E>() {
unsafe {
let error = ptr::read(error);
@ -119,7 +125,10 @@ impl Error {
}
/// Downcast this error object by reference.
pub fn downcast_ref<E: Display + Debug + Send + Sync + 'static>(&self) -> Option<&E> {
pub fn downcast_ref<E>(&self) -> Option<&E>
where
E: Display + Debug + Send + Sync + 'static,
{
if self.is::<E>() {
unsafe { Some(&*(self.inner.error() as *const dyn StdError as *const E)) }
} else {
@ -128,7 +137,10 @@ impl Error {
}
/// Downcast this error object by mutable reference.
pub fn downcast_mut<E: Display + Debug + Send + Sync + 'static>(&mut self) -> Option<&mut E> {
pub fn downcast_mut<E>(&mut self) -> Option<&mut E>
where
E: Display + Debug + Send + Sync + 'static,
{
if self.is::<E>() {
unsafe { Some(&mut *(self.inner.error_mut() as *mut dyn StdError as *mut E)) }
} else {
@ -137,7 +149,10 @@ impl Error {
}
}
impl<E: StdError + Send + Sync + 'static> From<E> for Error {
impl<E> From<E> for Error
where
E: StdError + Send + Sync + 'static,
{
fn from(error: E) -> Error {
Error::new(error)
}
@ -221,21 +236,29 @@ struct TraitObject {
}
#[repr(transparent)]
struct MessageError<M: Display + Debug>(M);
struct MessageError<M>(M)
where
M: Display + Debug;
impl<M: Display + Debug> Debug for MessageError<M> {
impl<M> Debug for MessageError<M>
where
M: Display + Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl<M: Display + Debug> Display for MessageError<M> {
impl<M> Display for MessageError<M>
where
M: Display + Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl<M: Display + Debug + 'static> StdError for MessageError<M> {}
impl<M> StdError for MessageError<M> where M: Display + Debug + 'static {}
impl ErrorImpl<()> {
fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {