Skip to content

Commit b63e3d4

Browse files
committed
add more StackError impls
1 parent 967eefd commit b63e3d4

File tree

2 files changed

+89
-13
lines changed

2 files changed

+89
-13
lines changed

src/error.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,92 @@ impl<'a> Iterator for Chain<'a> {
298298
}
299299
}
300300
}
301+
302+
macro_rules! impl_stack_error_for_std_error {
303+
($ty:ty) => {
304+
impl StackError for $ty {
305+
fn as_std(&self) -> &(dyn std::error::Error + Send + Sync + 'static) {
306+
self
307+
}
308+
309+
fn as_dyn(&self) -> &dyn StackError {
310+
self
311+
}
312+
313+
fn meta(&self) -> Option<&Meta> {
314+
None
315+
}
316+
317+
fn source(&self) -> Option<ErrorRef<'_>> {
318+
std::error::Error::source(self).map(ErrorRef::std)
319+
}
320+
321+
fn fmt_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
322+
fmt::Display::fmt(self, f)
323+
}
324+
325+
fn is_transparent(&self) -> bool {
326+
false
327+
}
328+
}
329+
};
330+
}
331+
332+
impl_stack_error_for_std_error!(std::io::Error);
333+
impl_stack_error_for_std_error!(std::fmt::Error);
334+
impl_stack_error_for_std_error!(std::str::Utf8Error);
335+
impl_stack_error_for_std_error!(std::string::FromUtf8Error);
336+
impl_stack_error_for_std_error!(std::net::AddrParseError);
337+
impl_stack_error_for_std_error!(std::array::TryFromSliceError);
338+
339+
impl StackError for Box<dyn StackError> {
340+
fn as_std(&self) -> &(dyn std::error::Error + Send + Sync + 'static) {
341+
StackError::as_std(&**self)
342+
}
343+
344+
fn as_dyn(&self) -> &dyn StackError {
345+
StackError::as_dyn(&**self)
346+
}
347+
348+
fn meta(&self) -> Option<&Meta> {
349+
StackError::meta(&**self)
350+
}
351+
352+
fn source(&self) -> Option<ErrorRef<'_>> {
353+
StackError::source(&**self)
354+
}
355+
356+
fn fmt_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
357+
StackError::fmt_message(&**self, f)
358+
}
359+
360+
fn is_transparent(&self) -> bool {
361+
StackError::is_transparent(&**self)
362+
}
363+
}
364+
365+
impl StackError for std::sync::Arc<dyn StackError> {
366+
fn as_std(&self) -> &(dyn std::error::Error + Send + Sync + 'static) {
367+
StackError::as_std(&**self)
368+
}
369+
370+
fn as_dyn(&self) -> &dyn StackError {
371+
StackError::as_dyn(&**self)
372+
}
373+
374+
fn meta(&self) -> Option<&Meta> {
375+
StackError::meta(&**self)
376+
}
377+
378+
fn source(&self) -> Option<ErrorRef<'_>> {
379+
StackError::source(&**self)
380+
}
381+
382+
fn fmt_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
383+
StackError::fmt_message(&**self, f)
384+
}
385+
386+
fn is_transparent(&self) -> bool {
387+
StackError::is_transparent(&**self)
388+
}
389+
}

src/macros.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,6 @@ macro_rules! ensure_e {
8484
};
8585
}
8686

87-
/// Ensures a condition, otherwise returns the given error.
88-
///
89-
/// The error will be converted into the function's expected error return type
90-
/// with `into`.
91-
#[macro_export]
92-
macro_rules! ensure {
93-
($predicate:expr, $err:expr $(,)?) => {
94-
if !$predicate {
95-
return Err(::std::convert::Into::into($err));
96-
}
97-
};
98-
}
99-
10087
/// Ensures a condition, otherwise returns an [`AnyError`].
10188
///
10289
/// This macro takes an expression as its first argument. If the expression evaluates

0 commit comments

Comments
 (0)