Use Iterator::size_hint to initialize Column and Row capacity

This commit is contained in:
Héctor Ramón Jiménez 2024-04-01 16:08:14 +02:00
parent b40db569a2
commit db4b03a659
No known key found for this signature in database
GPG key ID: 4C07CEC81AFA161F
3 changed files with 56 additions and 8 deletions

View file

@ -33,11 +33,18 @@ where
Self::from_vec(Vec::new())
}
/// Creates a [`Column`] with the given capacity.
pub fn with_capacity(capacity: usize) -> Self {
Self::from_vec(Vec::with_capacity(capacity))
}
/// Creates a [`Column`] with the given elements.
pub fn with_children(
children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>,
) -> Self {
Self::new().extend(children)
let iterator = children.into_iter();
Self::with_capacity(iterator.size_hint().0).extend(iterator)
}
/// Creates a [`Column`] from an already allocated [`Vec`].