Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit c39d242

Browse files
committed
Add documentation
1 parent 6dd9867 commit c39d242

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/lib.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![warn(missing_docs)]
2+
13
//! A library for consistent and reliable error handling
24
//!
35
//! This crate defines an opinionated strategy for error handling in Rust,
@@ -317,6 +319,8 @@
317319
extern crate backtrace;
318320

319321
use std::error;
322+
use std::iter::Iterator;
323+
use std::sync::Arc;
320324

321325
#[cfg(feature = "backtrace")]
322326
pub use backtrace::Backtrace;
@@ -348,27 +352,28 @@ macro_rules! error_chain {
348352
) => {
349353

350354

351-
// The Error type
352-
// --------------
353-
354-
// This has a simple structure to support pattern matching
355-
// during error handling. The second field is internal state
356-
// that is mostly irrelevant for error handling purposes.
355+
/// The Error type
356+
///
357+
/// This has a simple structure to support pattern matching
358+
/// during error handling. The second field is internal state
359+
/// that is mostly irrelevant for error handling purposes.
357360
#[derive(Debug)]
358361
pub struct $error_name(pub $error_kind_name,
359362
pub (Option<Box<::std::error::Error + Send>>,
360363
Option<::std::sync::Arc<$crate::Backtrace>>));
361364

362-
#[allow(unused)]
363365
impl $error_name {
366+
/// Returns the kind of the error.
364367
pub fn kind(&self) -> &$error_kind_name {
365368
&self.0
366369
}
367370

371+
/// Iterates over the error chain.
368372
pub fn iter(&self) -> $crate::ErrorChainIter {
369373
$crate::ErrorChainIter(Some(self))
370374
}
371375

376+
/// Returns the backtrace associated with this error.
372377
pub fn backtrace(&self) -> Option<&$crate::Backtrace> {
373378
(self.1).1.as_ref().map(|v| &**v)
374379
}
@@ -504,10 +509,6 @@ macro_rules! error_chain {
504509
$error_name(kind, backtrace)
505510
}
506511

507-
// Use downcasts to extract the backtrace from types we know,
508-
// to avoid generating a new one. It would be better to not
509-
// define this in the macro, but types need some additional
510-
// machinery to make it work.
511512
fn backtrace_from_box(mut e: Box<::std::error::Error + Send + 'static>)
512513
-> (Box<::std::error::Error + Send + 'static>,
513514
Option<Option<::std::sync::Arc<$crate::Backtrace>>>) {
@@ -651,17 +652,13 @@ macro_rules! error_chain {
651652
);
652653
}
653654

654-
655-
use std::error::Error as StdError;
656-
use std::iter::Iterator;
657-
use std::sync::Arc;
658-
659-
pub struct ErrorChainIter<'a>(pub Option<&'a StdError>);
655+
/// Iterator over the error chain.
656+
pub struct ErrorChainIter<'a>(pub Option<&'a error::Error>);
660657

661658
impl<'a> Iterator for ErrorChainIter<'a> {
662-
type Item = &'a StdError;
659+
type Item = &'a error::Error;
663660

664-
fn next<'b>(&'b mut self) -> Option<&'a StdError> {
661+
fn next<'b>(&'b mut self) -> Option<&'a error::Error> {
665662
match self.0.take() {
666663
Some(e) => {
667664
self.0 = e.cause();
@@ -688,13 +685,21 @@ pub fn make_backtrace() -> Option<Arc<Backtrace>> {
688685
None
689686
}
690687

688+
/// This trait is an implementation detail, you should have to implement or
689+
/// use it.
691690
pub trait Error: error::Error + Send + 'static {
691+
/// Associated kind type.
692692
type ErrorKind;
693-
fn new(kind: Self::ErrorKind, backtrace: (Option<Box<error::Error + Send + 'static>>,
694-
Option<Arc<Backtrace>>)) -> Self;
693+
/// Creates an error from it's parts.
694+
fn new(kind: Self::ErrorKind,
695+
state: (Option<Box<error::Error + Send + 'static>>,
696+
Option<Arc<Backtrace>>)) -> Self;
697+
/// Use downcasts to extract the backtrace from types we know,
698+
/// to avoid generating a new one. It would be better to not
699+
/// define this in the macro, but types need some additional
700+
/// machinery to make it work.
695701
fn backtrace_from_box(e: Box<error::Error + Send + 'static>)
696-
-> (Box<error::Error + Send + 'static>,
697-
Option<Option<Arc<Backtrace>>>);
702+
-> (Box<error::Error + Send + 'static>, Option<Option<Arc<Backtrace>>>);
698703
}
699704

700705
/// Additionnal methods for `Result`, for easy interaction with this crate.

0 commit comments

Comments
 (0)