Skip to content
Myr

Implement DoubleEndedIterator for Chain

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent a8c199b · Browse files · 3 files+45 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+45 added−6 removed

  • other:+45 added−6 removed

3 files+45 added−6 removed

modified:src/chain.rs+39 added-3 removed
1use self::ChainState::{Buffered, Linked};2use crate::Chain;3use std::error::Error as StdError;4use std::vec;56#[derive(Clone)]7pub(crate) enum ChainState<'a> {8 Linked {9 next: Option<&'a (dyn StdError + 'static)>,10 },11 Buffered {12 rest: vec::IntoIter<&'a (dyn StdError + 'static)>,13 },14}1516impl<'a> Iterator for Chain<'a> {17 type Item = &'a (dyn StdError + 'static);1819 fn next(&mut self) -> Option<Self::Item> {8 let next = self.next?;9 self.next = next.source();10 Some(next)20 match &mut self.state {21 Linked { next } => {22 let error = (*next)?;23 *next = error.source();24 Some(error)25 }26 Buffered { rest } => rest.next(),27 }28 }29}3031impl DoubleEndedIterator for Chain<'_> {32 fn next_back(&mut self) -> Option<Self::Item> {33 match &mut self.state {34 Linked { mut next } => {35 let mut rest = Vec::new();36 while let Some(cause) = next {37 next = cause.source();38 rest.push(cause);39 }40 let mut rest = rest.into_iter();41 let last = rest.next_back();42 self.state = Buffered { rest };43 last44 }45 Buffered { rest } => rest.next_back(),46 }47 }48}
modified:src/error.rs+4 added-1 removed
1use crate::backtrace::Backtrace;2use crate::chain::ChainState;3use crate::{Chain, Error};4use std::any::TypeId;5use std::error::Error as StdError;6use std::fmt::{self, Debug, Display};
666 unmodified lines
673 }674675 pub(crate) fn chain(&self) -> Chain {676 Chain {677 state: ChainState::Linked {676 next: Some(self.error()),678 next: Some(self.error()),679 },680 }681 }682}683
48 unmodified lines
modified:src/lib.rs+2 added-2 removed
177 unmodified lines
178179#[cfg(not(feature = "std"))]180compile_error!("no_std support is not implemented yet");181182use crate::chain::ChainState;183use crate::error::ErrorImpl;183use std::error::Error as StdError;184use std::fmt::Display;185use std::mem::ManuallyDrop;186187pub use anyhow as format_err;
114 unmodified lines
302/// }303/// ```304#[derive(Clone)]305pub struct Chain<'a> {306 next: Option<&'a (dyn StdError + 'static)>,306 state: ChainState<'a>,307}308309/// `Result<T, Error>`310///
222 unmodified lines