Skip to content

Commit 5194c0d

Browse files
authored
Minor atoms improvements (#7145)
Improve some lifetime bounds and add some convenience constructors
1 parent 06760e1 commit 5194c0d

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

crates/egui/src/atomics/atoms.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ impl<'a> Atoms<'a> {
5454
string
5555
}
5656

57-
pub fn iter_kinds(&'a self) -> impl Iterator<Item = &'a AtomKind<'a>> {
57+
pub fn iter_kinds(&self) -> impl Iterator<Item = &AtomKind<'a>> {
5858
self.0.iter().map(|atom| &atom.kind)
5959
}
6060

61-
pub fn iter_kinds_mut(&'a mut self) -> impl Iterator<Item = &'a mut AtomKind<'a>> {
61+
pub fn iter_kinds_mut(&mut self) -> impl Iterator<Item = &mut AtomKind<'a>> {
6262
self.0.iter_mut().map(|atom| &mut atom.kind)
6363
}
6464

65-
pub fn iter_images(&'a self) -> impl Iterator<Item = &'a Image<'a>> {
65+
pub fn iter_images(&self) -> impl Iterator<Item = &Image<'a>> {
6666
self.iter_kinds().filter_map(|kind| {
6767
if let AtomKind::Image(image) = kind {
6868
Some(image)
@@ -72,7 +72,7 @@ impl<'a> Atoms<'a> {
7272
})
7373
}
7474

75-
pub fn iter_images_mut(&'a mut self) -> impl Iterator<Item = &'a mut Image<'a>> {
75+
pub fn iter_images_mut(&mut self) -> impl Iterator<Item = &mut Image<'a>> {
7676
self.iter_kinds_mut().filter_map(|kind| {
7777
if let AtomKind::Image(image) = kind {
7878
Some(image)
@@ -82,7 +82,7 @@ impl<'a> Atoms<'a> {
8282
})
8383
}
8484

85-
pub fn iter_texts(&'a self) -> impl Iterator<Item = &'a WidgetText> {
85+
pub fn iter_texts(&self) -> impl Iterator<Item = &WidgetText> + use<'_, 'a> {
8686
self.iter_kinds().filter_map(|kind| {
8787
if let AtomKind::Text(text) = kind {
8888
Some(text)
@@ -92,7 +92,7 @@ impl<'a> Atoms<'a> {
9292
})
9393
}
9494

95-
pub fn iter_texts_mut(&'a mut self) -> impl Iterator<Item = &'a mut WidgetText> {
95+
pub fn iter_texts_mut(&mut self) -> impl Iterator<Item = &mut WidgetText> + use<'a, '_> {
9696
self.iter_kinds_mut().filter_map(|kind| {
9797
if let AtomKind::Text(text) = kind {
9898
Some(text)
@@ -107,7 +107,7 @@ impl<'a> Atoms<'a> {
107107
.for_each(|atom| *atom = f(std::mem::take(atom)));
108108
}
109109

110-
pub fn map_kind<F>(&'a mut self, mut f: F)
110+
pub fn map_kind<F>(&mut self, mut f: F)
111111
where
112112
F: FnMut(AtomKind<'a>) -> AtomKind<'a>,
113113
{
@@ -116,7 +116,7 @@ impl<'a> Atoms<'a> {
116116
}
117117
}
118118

119-
pub fn map_images<F>(&'a mut self, mut f: F)
119+
pub fn map_images<F>(&mut self, mut f: F)
120120
where
121121
F: FnMut(Image<'a>) -> Image<'a>,
122122
{
@@ -129,7 +129,7 @@ impl<'a> Atoms<'a> {
129129
});
130130
}
131131

132-
pub fn map_texts<F>(&'a mut self, mut f: F)
132+
pub fn map_texts<F>(&mut self, mut f: F)
133133
where
134134
F: FnMut(WidgetText) -> WidgetText,
135135
{
@@ -227,3 +227,33 @@ impl DerefMut for Atoms<'_> {
227227
&mut self.0
228228
}
229229
}
230+
231+
impl<'a, T: Into<Atom<'a>>> From<Vec<T>> for Atoms<'a> {
232+
fn from(vec: Vec<T>) -> Self {
233+
Atoms(vec.into_iter().map(Into::into).collect())
234+
}
235+
}
236+
237+
impl<'a, T: Into<Atom<'a>> + Clone> From<&[T]> for Atoms<'a> {
238+
fn from(slice: &[T]) -> Self {
239+
Atoms(slice.iter().cloned().map(Into::into).collect())
240+
}
241+
}
242+
243+
impl<'a, Item: Into<Atom<'a>>> FromIterator<Item> for Atoms<'a> {
244+
fn from_iter<T: IntoIterator<Item = Item>>(iter: T) -> Self {
245+
Atoms(iter.into_iter().map(Into::into).collect())
246+
}
247+
}
248+
249+
#[cfg(test)]
250+
mod tests {
251+
use crate::Atoms;
252+
253+
#[test]
254+
fn collect_atoms() {
255+
let _: Atoms<'_> = ["Hello", "World"].into_iter().collect();
256+
let _ = Atoms::from(vec!["Hi"]);
257+
let _ = Atoms::from(["Hi"].as_slice());
258+
}
259+
}

0 commit comments

Comments
 (0)