From 2feee87795332b84d15959835a2bde96c90b4c22 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Thu, 25 May 2023 11:55:57 +0200 Subject: [PATCH] cxx-qen: impl IntoIterator for Error 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. --- gen/lib/src/error.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/gen/lib/src/error.rs b/gen/lib/src/error.rs index 30d16879a..b9f7ca39e 100644 --- a/gen/lib/src/error.rs +++ b/gen/lib/src/error.rs @@ -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(::IntoIter), + Other(std::iter::Once), +} + +impl Iterator for IntoIter { + type Item = Error; + + fn next(&mut self) -> Option { + 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(), + } + } +}