diff --git a/src/bindgen/transparent.rs b/src/bindgen/transparent.rs index 660e41ae..c948ecd8 100644 --- a/src/bindgen/transparent.rs +++ b/src/bindgen/transparent.rs @@ -8,37 +8,36 @@ use crate::bindgen::ir::{ use crate::bindgen::library::Library; /// Helper trait that makes it easier to work with `Cow` in iterators -pub trait IterCow: Iterator { - /// Maps from `&T` to `Cow<'a, T>` using the provided function. If the function returns `Some` +// NOTE: 'i is the lifetime of the iterator itself, and 't is the lifetime of references it returns +pub trait IterCow<'i, 't: 'i, T: Clone + 't>: Iterator + 'i { + /// Maps from `&T` to `Cow<'t, T>` using the provided function. If the function returns `Some` /// result, it is returned as `Cow::Owned`; otherwise, return the item as `Cow::Borrowed`. - fn cow_map<'a, F, T>(self, f: F) -> impl Iterator> + // NOTE: MSRV 1.70 requires us to return `Box` because support for returning `impl + // Iterator` from trait methods didn't land until 1.75. Which in turn requires the lifetime 'i. + fn cow_map(self, f: F) -> Box> + 'i> where - F: FnMut(&T) -> Option, - T: Clone + 'a, - Self: Iterator; + F: FnMut(&T) -> Option + 'i, + Self: Iterator; /// True if any item is `Cow::Owned` - fn any_owned<'i, 'a: 'i, T>(self) -> bool + fn any_owned(self) -> bool where - T: Clone + 'a, - Self: Iterator>; + Self: Iterator>; } // Blanket implementation for all iterators -impl IterCow for I { - fn cow_map<'a, F, T>(self, mut f: F) -> impl Iterator> +impl<'i, 't: 'i, T: Clone + 't, I: Iterator + 'i> IterCow<'i, 't, T> for I { + fn cow_map(self, mut f: F) -> Box> + 'i> where - F: FnMut(&T) -> Option, - T: Clone + 'a, - Self: Iterator, + F: FnMut(&T) -> Option + 'i, + Self: Iterator, { - self.map(move |item| f(item).map_or_else(|| Cow::Borrowed(item), Cow::Owned)) + Box::new(self.map(move |item| f(item).map_or_else(|| Cow::Borrowed(item), Cow::Owned))) } - fn any_owned<'i, 'a: 'i, T>(mut self) -> bool + fn any_owned(mut self) -> bool where - T: Clone + 'a, - Self: Iterator>, + Self: Iterator>, { self.any(|item| matches!(item, Cow::Owned(_))) }