Skip to content

Commit 00c0c31

Browse files
authored
Rollup merge of rust-lang#65557 - haraldh:error_iter_rename, r=sfackler
rename Error::iter_chain() and remove Error::iter_sources() ~~Rename~~ * ~~Error::iter_chain() -> Error::chained()~~ * ~~Error::iter_sources() -> Error::ancestors()~~ * ~~ErrorIter -> Chained and Ancestors~~ according to rust-lang#58520 (comment) Tracker: rust-lang#58520 Edit: Rename * Error::iter_chain() -> Error::chained() * ErrorIter -> Chain So, it seems, that even Path::ancestors() includes itself. So, to avoid confusion and simplify it more, I reduced PR rust-lang#65557 to only have `chained` and `Chain`. Rationale: 1. Such iterators are helpful. They should better be stabilized sooner than later. 1. self should be included. It is easy to .skip(1) it. Not including self is harmful because it is harder to add self to the iterator than to remove it. 1. The chosen name should be telling and reflect the fact that self is included. `.chained()` was chosen in honor of error-chain and because the iterator iterates over the chain of errors that is somehow included in self. 1. The resulting iterator is named `Chain` because the `error::Chain` is what we want to have.
2 parents 9e8c4e6 + 7b9d50d commit 00c0c31

File tree

1 file changed

+12
-80
lines changed

1 file changed

+12
-80
lines changed

src/libstd/error.rs

+12-80
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,9 @@ impl dyn Error {
725725
/// Returns an iterator starting with the current error and continuing with
726726
/// recursively calling [`source`].
727727
///
728+
/// If you want to omit the current error and only use its sources,
729+
/// use `skip(1)`.
730+
///
728731
/// # Examples
729732
///
730733
/// ```
@@ -763,7 +766,7 @@ impl dyn Error {
763766
/// // let err : Box<Error> = b.into(); // or
764767
/// let err = &b as &(dyn Error);
765768
///
766-
/// let mut iter = err.iter_chain();
769+
/// let mut iter = err.chain();
767770
///
768771
/// assert_eq!("B".to_string(), iter.next().unwrap().to_string());
769772
/// assert_eq!("A".to_string(), iter.next().unwrap().to_string());
@@ -774,98 +777,27 @@ impl dyn Error {
774777
/// [`source`]: trait.Error.html#method.source
775778
#[unstable(feature = "error_iter", issue = "58520")]
776779
#[inline]
777-
pub fn iter_chain(&self) -> ErrorIter<'_> {
778-
ErrorIter {
780+
pub fn chain(&self) -> Chain<'_> {
781+
Chain {
779782
current: Some(self),
780783
}
781784
}
782-
783-
/// Returns an iterator starting with the [`source`] of this error
784-
/// and continuing with recursively calling [`source`].
785-
///
786-
/// # Examples
787-
///
788-
/// ```
789-
/// #![feature(error_iter)]
790-
/// use std::error::Error;
791-
/// use std::fmt;
792-
///
793-
/// #[derive(Debug)]
794-
/// struct A;
795-
///
796-
/// #[derive(Debug)]
797-
/// struct B(Option<Box<dyn Error + 'static>>);
798-
///
799-
/// #[derive(Debug)]
800-
/// struct C(Option<Box<dyn Error + 'static>>);
801-
///
802-
/// impl fmt::Display for A {
803-
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
804-
/// write!(f, "A")
805-
/// }
806-
/// }
807-
///
808-
/// impl fmt::Display for B {
809-
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
810-
/// write!(f, "B")
811-
/// }
812-
/// }
813-
///
814-
/// impl fmt::Display for C {
815-
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
816-
/// write!(f, "C")
817-
/// }
818-
/// }
819-
///
820-
/// impl Error for A {}
821-
///
822-
/// impl Error for B {
823-
/// fn source(&self) -> Option<&(dyn Error + 'static)> {
824-
/// self.0.as_ref().map(|e| e.as_ref())
825-
/// }
826-
/// }
827-
///
828-
/// impl Error for C {
829-
/// fn source(&self) -> Option<&(dyn Error + 'static)> {
830-
/// self.0.as_ref().map(|e| e.as_ref())
831-
/// }
832-
/// }
833-
///
834-
/// let b = B(Some(Box::new(A)));
835-
/// let c = C(Some(Box::new(b)));
836-
///
837-
/// // let err : Box<Error> = c.into(); // or
838-
/// let err = &c as &(dyn Error);
839-
///
840-
/// let mut iter = err.iter_sources();
841-
///
842-
/// assert_eq!("B".to_string(), iter.next().unwrap().to_string());
843-
/// assert_eq!("A".to_string(), iter.next().unwrap().to_string());
844-
/// assert!(iter.next().is_none());
845-
/// assert!(iter.next().is_none());
846-
/// ```
847-
///
848-
/// [`source`]: trait.Error.html#method.source
849-
#[inline]
850-
#[unstable(feature = "error_iter", issue = "58520")]
851-
pub fn iter_sources(&self) -> ErrorIter<'_> {
852-
ErrorIter {
853-
current: self.source(),
854-
}
855-
}
856785
}
857786

858-
/// An iterator over [`Error`]
787+
/// An iterator over an [`Error`] and its sources.
788+
///
789+
/// If you want to omit the initial error and only process
790+
/// its sources, use `skip(1)`.
859791
///
860792
/// [`Error`]: trait.Error.html
861793
#[unstable(feature = "error_iter", issue = "58520")]
862794
#[derive(Copy, Clone, Debug)]
863-
pub struct ErrorIter<'a> {
795+
pub struct Chain<'a> {
864796
current: Option<&'a (dyn Error + 'static)>,
865797
}
866798

867799
#[unstable(feature = "error_iter", issue = "58520")]
868-
impl<'a> Iterator for ErrorIter<'a> {
800+
impl<'a> Iterator for Chain<'a> {
869801
type Item = &'a (dyn Error + 'static);
870802

871803
fn next(&mut self) -> Option<Self::Item> {

0 commit comments

Comments
 (0)