Minor atoms improvements (#7145)

Improve some lifetime bounds and add some convenience constructors
This commit is contained in:
Lucas Meurer 2025-06-16 08:42:17 +02:00 committed by GitHub
parent 06760e1b08
commit 5194c0df3e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 39 additions and 9 deletions

View File

@ -54,15 +54,15 @@ impl<'a> Atoms<'a> {
string string
} }
pub fn iter_kinds(&'a self) -> impl Iterator<Item = &'a AtomKind<'a>> { pub fn iter_kinds(&self) -> impl Iterator<Item = &AtomKind<'a>> {
self.0.iter().map(|atom| &atom.kind) self.0.iter().map(|atom| &atom.kind)
} }
pub fn iter_kinds_mut(&'a mut self) -> impl Iterator<Item = &'a mut AtomKind<'a>> { pub fn iter_kinds_mut(&mut self) -> impl Iterator<Item = &mut AtomKind<'a>> {
self.0.iter_mut().map(|atom| &mut atom.kind) self.0.iter_mut().map(|atom| &mut atom.kind)
} }
pub fn iter_images(&'a self) -> impl Iterator<Item = &'a Image<'a>> { pub fn iter_images(&self) -> impl Iterator<Item = &Image<'a>> {
self.iter_kinds().filter_map(|kind| { self.iter_kinds().filter_map(|kind| {
if let AtomKind::Image(image) = kind { if let AtomKind::Image(image) = kind {
Some(image) Some(image)
@ -72,7 +72,7 @@ impl<'a> Atoms<'a> {
}) })
} }
pub fn iter_images_mut(&'a mut self) -> impl Iterator<Item = &'a mut Image<'a>> { pub fn iter_images_mut(&mut self) -> impl Iterator<Item = &mut Image<'a>> {
self.iter_kinds_mut().filter_map(|kind| { self.iter_kinds_mut().filter_map(|kind| {
if let AtomKind::Image(image) = kind { if let AtomKind::Image(image) = kind {
Some(image) Some(image)
@ -82,7 +82,7 @@ impl<'a> Atoms<'a> {
}) })
} }
pub fn iter_texts(&'a self) -> impl Iterator<Item = &'a WidgetText> { pub fn iter_texts(&self) -> impl Iterator<Item = &WidgetText> + use<'_, 'a> {
self.iter_kinds().filter_map(|kind| { self.iter_kinds().filter_map(|kind| {
if let AtomKind::Text(text) = kind { if let AtomKind::Text(text) = kind {
Some(text) Some(text)
@ -92,7 +92,7 @@ impl<'a> Atoms<'a> {
}) })
} }
pub fn iter_texts_mut(&'a mut self) -> impl Iterator<Item = &'a mut WidgetText> { pub fn iter_texts_mut(&mut self) -> impl Iterator<Item = &mut WidgetText> + use<'a, '_> {
self.iter_kinds_mut().filter_map(|kind| { self.iter_kinds_mut().filter_map(|kind| {
if let AtomKind::Text(text) = kind { if let AtomKind::Text(text) = kind {
Some(text) Some(text)
@ -107,7 +107,7 @@ impl<'a> Atoms<'a> {
.for_each(|atom| *atom = f(std::mem::take(atom))); .for_each(|atom| *atom = f(std::mem::take(atom)));
} }
pub fn map_kind<F>(&'a mut self, mut f: F) pub fn map_kind<F>(&mut self, mut f: F)
where where
F: FnMut(AtomKind<'a>) -> AtomKind<'a>, F: FnMut(AtomKind<'a>) -> AtomKind<'a>,
{ {
@ -116,7 +116,7 @@ impl<'a> Atoms<'a> {
} }
} }
pub fn map_images<F>(&'a mut self, mut f: F) pub fn map_images<F>(&mut self, mut f: F)
where where
F: FnMut(Image<'a>) -> Image<'a>, F: FnMut(Image<'a>) -> Image<'a>,
{ {
@ -129,7 +129,7 @@ impl<'a> Atoms<'a> {
}); });
} }
pub fn map_texts<F>(&'a mut self, mut f: F) pub fn map_texts<F>(&mut self, mut f: F)
where where
F: FnMut(WidgetText) -> WidgetText, F: FnMut(WidgetText) -> WidgetText,
{ {
@ -227,3 +227,33 @@ impl DerefMut for Atoms<'_> {
&mut self.0 &mut self.0
} }
} }
impl<'a, T: Into<Atom<'a>>> From<Vec<T>> for Atoms<'a> {
fn from(vec: Vec<T>) -> Self {
Atoms(vec.into_iter().map(Into::into).collect())
}
}
impl<'a, T: Into<Atom<'a>> + Clone> From<&[T]> for Atoms<'a> {
fn from(slice: &[T]) -> Self {
Atoms(slice.iter().cloned().map(Into::into).collect())
}
}
impl<'a, Item: Into<Atom<'a>>> FromIterator<Item> for Atoms<'a> {
fn from_iter<T: IntoIterator<Item = Item>>(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());
}
}