Skip to content

Commit

Permalink
cxx-qen: impl IntoIterator for Error
Browse files Browse the repository at this point in the history
Like syn::Error, this allows Error to be "expanded" into multiple
contained errors.

In comparison to syn, this is only implemented on Error and not on
&Error, as not all variants of cxx::gen::Error implement Clone, which
would be required for this to work.
  • Loading branch information
LeonMatthesKDAB committed May 25, 2023
1 parent d122662 commit 2feee87
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions gen/lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,33 @@ impl StdError for Error {
self.err.source()
}
}

impl IntoIterator for Error {
type Item = Error;
type IntoIter = IntoIter;

fn into_iter(self) -> Self::IntoIter {
match self.err {
crate::gen::Error::Syn(err) => IntoIter::Syn(err.into_iter()),
_ => IntoIter::Other(std::iter::once(self)),
}
}
}

pub enum IntoIter {
Syn(<syn::Error as IntoIterator>::IntoIter),
Other(std::iter::Once<Error>),
}

impl Iterator for IntoIter {
type Item = Error;

fn next(&mut self) -> Option<Self::Item> {
match self {
IntoIter::Syn(ref mut iter) => iter
.next()
.map(|syn_err| Error::from(crate::gen::Error::Syn(syn_err))),
IntoIter::Other(ref mut iter) => iter.next(),
}
}
}

0 comments on commit 2feee87

Please sign in to comment.