diff --git a/crates/egui/src/atomics/atoms.rs b/crates/egui/src/atomics/atoms.rs index 635b2a13..4b19c9e2 100644 --- a/crates/egui/src/atomics/atoms.rs +++ b/crates/egui/src/atomics/atoms.rs @@ -54,15 +54,15 @@ impl<'a> Atoms<'a> { string } - pub fn iter_kinds(&'a self) -> impl Iterator> { + pub fn iter_kinds(&self) -> impl Iterator> { self.0.iter().map(|atom| &atom.kind) } - pub fn iter_kinds_mut(&'a mut self) -> impl Iterator> { + pub fn iter_kinds_mut(&mut self) -> impl Iterator> { self.0.iter_mut().map(|atom| &mut atom.kind) } - pub fn iter_images(&'a self) -> impl Iterator> { + pub fn iter_images(&self) -> impl Iterator> { self.iter_kinds().filter_map(|kind| { if let AtomKind::Image(image) = kind { Some(image) @@ -72,7 +72,7 @@ impl<'a> Atoms<'a> { }) } - pub fn iter_images_mut(&'a mut self) -> impl Iterator> { + pub fn iter_images_mut(&mut self) -> impl Iterator> { self.iter_kinds_mut().filter_map(|kind| { if let AtomKind::Image(image) = kind { Some(image) @@ -82,7 +82,7 @@ impl<'a> Atoms<'a> { }) } - pub fn iter_texts(&'a self) -> impl Iterator { + pub fn iter_texts(&self) -> impl Iterator + use<'_, 'a> { self.iter_kinds().filter_map(|kind| { if let AtomKind::Text(text) = kind { Some(text) @@ -92,7 +92,7 @@ impl<'a> Atoms<'a> { }) } - pub fn iter_texts_mut(&'a mut self) -> impl Iterator { + pub fn iter_texts_mut(&mut self) -> impl Iterator + use<'a, '_> { self.iter_kinds_mut().filter_map(|kind| { if let AtomKind::Text(text) = kind { Some(text) @@ -107,7 +107,7 @@ impl<'a> Atoms<'a> { .for_each(|atom| *atom = f(std::mem::take(atom))); } - pub fn map_kind(&'a mut self, mut f: F) + pub fn map_kind(&mut self, mut f: F) where F: FnMut(AtomKind<'a>) -> AtomKind<'a>, { @@ -116,7 +116,7 @@ impl<'a> Atoms<'a> { } } - pub fn map_images(&'a mut self, mut f: F) + pub fn map_images(&mut self, mut f: F) where F: FnMut(Image<'a>) -> Image<'a>, { @@ -129,7 +129,7 @@ impl<'a> Atoms<'a> { }); } - pub fn map_texts(&'a mut self, mut f: F) + pub fn map_texts(&mut self, mut f: F) where F: FnMut(WidgetText) -> WidgetText, { @@ -227,3 +227,33 @@ impl DerefMut for Atoms<'_> { &mut self.0 } } + +impl<'a, T: Into>> From> for Atoms<'a> { + fn from(vec: Vec) -> Self { + Atoms(vec.into_iter().map(Into::into).collect()) + } +} + +impl<'a, T: Into> + Clone> From<&[T]> for Atoms<'a> { + fn from(slice: &[T]) -> Self { + Atoms(slice.iter().cloned().map(Into::into).collect()) + } +} + +impl<'a, Item: Into>> FromIterator for Atoms<'a> { + fn from_iter>(iter: T) -> Self { + Atoms(iter.into_iter().map(Into::into).collect()) + } +} + +#[cfg(test)] +mod tests { + use crate::Atoms; + + #[test] + fn collect_atoms() { + let _: Atoms<'_> = ["Hello", "World"].into_iter().collect(); + let _ = Atoms::from(vec!["Hi"]); + let _ = Atoms::from(["Hi"].as_slice()); + } +}