Skip to content
Myr

Merge pull request #45 from dtolnay/chain

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

Provide DoubleEndedIterator and ExactSizeIterator for anyhow::Chain

authored by David Tolnay · committed by GitHub · parents 7246344, 9ef3d2d · Browse files · 4 files+121 added−13 removed
This commit

State

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

Diff stats

4 files+121 added−13 removed

  • tests:+45 added−0 removed
  • other:+76 added−13 removed

4 files+121 added−13 removed

added:src/chain.rs+69 added
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> {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 }2930 fn size_hint(&self) -> (usize, Option<usize>) {31 let len = self.len();32 (len, Some(len))33 }34}3536impl DoubleEndedIterator for Chain<'_> {37 fn next_back(&mut self) -> Option<Self::Item> {38 match &mut self.state {39 Linked { mut next } => {40 let mut rest = Vec::new();41 while let Some(cause) = next {42 next = cause.source();43 rest.push(cause);44 }45 let mut rest = rest.into_iter();46 let last = rest.next_back();47 self.state = Buffered { rest };48 last49 }50 Buffered { rest } => rest.next_back(),51 }52 }53}5455impl ExactSizeIterator for Chain<'_> {56 fn len(&self) -> usize {57 match &self.state {58 Linked { mut next } => {59 let mut len = 0;60 while let Some(cause) = next {61 next = cause.source();62 len += 1;63 }64 len65 }66 Buffered { rest } => rest.len(),67 }68 }69}
modified:src/error.rs+4 added-11 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
44 unmodified lines
728 fn from(error: Error) -> Self {729 Box::<dyn StdError + Send + Sync>::from(error)730 }731}729730impl<'a> Iterator for Chain<'a> {731 type Item = &'a (dyn StdError + 'static);732733 fn next(&mut self) -> Option<Self::Item> {734 let next = self.next?;735 self.next = next.source();736 Some(next)737 }738}
modified:src/lib.rs+3 added-2 removed
167 unmodified lines
168)]169170#[macro_use]171mod backtrace;172mod chain;173mod context;174mod error;175mod fmt;176mod kind;
1 unmodified line
178179#[cfg(not(feature = "std"))]180compile_error!("no_std support is not implemented yet");181182use crate::chain::ChainState;183use crate::error::ErrorImpl;182use 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> {305 next: Option<&'a (dyn StdError + 'static)>,306 state: ChainState<'a>,307}308309/// `Result<T, Error>`310///
222 unmodified lines
added:tests/test_chain.rs+45 added
1use anyhow::{anyhow, Error};23fn error() -> Error {4 anyhow!(0).context(1).context(2).context(3)5}67#[test]8fn test_iter() {9 let e = error();10 let mut chain = e.chain();11 assert_eq!("3", chain.next().unwrap().to_string());12 assert_eq!("2", chain.next().unwrap().to_string());13 assert_eq!("1", chain.next().unwrap().to_string());14 assert_eq!("0", chain.next().unwrap().to_string());15 assert!(chain.next().is_none());16 assert!(chain.next_back().is_none());17}1819#[test]20fn test_rev() {21 let e = error();22 let mut chain = e.chain().rev();23 assert_eq!("0", chain.next().unwrap().to_string());24 assert_eq!("1", chain.next().unwrap().to_string());25 assert_eq!("2", chain.next().unwrap().to_string());26 assert_eq!("3", chain.next().unwrap().to_string());27 assert!(chain.next().is_none());28 assert!(chain.next_back().is_none());29}3031#[test]32fn test_len() {33 let e = error();34 let mut chain = e.chain();35 assert_eq!(4, chain.len());36 assert_eq!("3", chain.next().unwrap().to_string());37 assert_eq!(3, chain.len());38 assert_eq!("0", chain.next_back().unwrap().to_string());39 assert_eq!(2, chain.len());40 assert_eq!("2", chain.next().unwrap().to_string());41 assert_eq!(1, chain.len());42 assert_eq!("1", chain.next_back().unwrap().to_string());43 assert_eq!(0, chain.len());44 assert!(chain.next().is_none());45}