Use Iterator::size_hint to initialize Column and Row capacity
This commit is contained in:
parent
b40db569a2
commit
db4b03a659
3 changed files with 56 additions and 8 deletions
|
|
@ -33,11 +33,18 @@ where
|
||||||
Self::from_vec(Vec::new())
|
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.
|
/// Creates a [`Column`] with the given elements.
|
||||||
pub fn with_children(
|
pub fn with_children(
|
||||||
children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>,
|
children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>,
|
||||||
) -> Self {
|
) -> 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`].
|
/// Creates a [`Column`] from an already allocated [`Vec`].
|
||||||
|
|
|
||||||
|
|
@ -40,27 +40,49 @@ where
|
||||||
{
|
{
|
||||||
/// Creates an empty [`Column`].
|
/// Creates an empty [`Column`].
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Column {
|
Self::from_vecs(Vec::new(), Vec::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a [`Column`] from already allocated [`Vec`]s.
|
||||||
|
///
|
||||||
|
/// Keep in mind that the [`Column`] will not inspect the [`Vec`]s, which means
|
||||||
|
/// it won't automatically adapt to the sizing strategy of its contents.
|
||||||
|
///
|
||||||
|
/// If any of the children have a [`Length::Fill`] strategy, you will need to
|
||||||
|
/// call [`Column::width`] or [`Column::height`] accordingly.
|
||||||
|
pub fn from_vecs(
|
||||||
|
keys: Vec<Key>,
|
||||||
|
children: Vec<Element<'a, Message, Theme, Renderer>>,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
spacing: 0.0,
|
spacing: 0.0,
|
||||||
padding: Padding::ZERO,
|
padding: Padding::ZERO,
|
||||||
width: Length::Shrink,
|
width: Length::Shrink,
|
||||||
height: Length::Shrink,
|
height: Length::Shrink,
|
||||||
max_width: f32::INFINITY,
|
max_width: f32::INFINITY,
|
||||||
align_items: Alignment::Start,
|
align_items: Alignment::Start,
|
||||||
keys: Vec::new(),
|
keys,
|
||||||
children: Vec::new(),
|
children,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a [`Column`] with the given capacity.
|
||||||
|
pub fn with_capacity(capacity: usize) -> Self {
|
||||||
|
Self::from_vecs(
|
||||||
|
Vec::with_capacity(capacity),
|
||||||
|
Vec::with_capacity(capacity),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a [`Column`] with the given elements.
|
/// Creates a [`Column`] with the given elements.
|
||||||
pub fn with_children(
|
pub fn with_children(
|
||||||
children: impl IntoIterator<
|
children: impl IntoIterator<
|
||||||
Item = (Key, Element<'a, Message, Theme, Renderer>),
|
Item = (Key, Element<'a, Message, Theme, Renderer>),
|
||||||
>,
|
>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
children
|
let iterator = children.into_iter();
|
||||||
.into_iter()
|
|
||||||
.fold(Self::new(), |column, (key, child)| column.push(key, child))
|
Self::with_capacity(iterator.size_hint().0).extend(iterator)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the vertical spacing _between_ elements.
|
/// Sets the vertical spacing _between_ elements.
|
||||||
|
|
@ -132,6 +154,18 @@ where
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extends the [`Column`] with the given children.
|
||||||
|
pub fn extend(
|
||||||
|
self,
|
||||||
|
children: impl IntoIterator<
|
||||||
|
Item = (Key, Element<'a, Message, Theme, Renderer>),
|
||||||
|
>,
|
||||||
|
) -> Self {
|
||||||
|
children
|
||||||
|
.into_iter()
|
||||||
|
.fold(self, |column, (key, child)| column.push(key, child))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Key, Message, Renderer> Default for Column<'a, Key, Message, Renderer>
|
impl<'a, Key, Message, Renderer> Default for Column<'a, Key, Message, Renderer>
|
||||||
|
|
|
||||||
|
|
@ -31,11 +31,18 @@ where
|
||||||
Self::from_vec(Vec::new())
|
Self::from_vec(Vec::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a [`Row`] with the given capacity.
|
||||||
|
pub fn with_capacity(capacity: usize) -> Self {
|
||||||
|
Self::from_vec(Vec::with_capacity(capacity))
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a [`Row`] with the given elements.
|
/// Creates a [`Row`] with the given elements.
|
||||||
pub fn with_children(
|
pub fn with_children(
|
||||||
children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>,
|
children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self::new().extend(children)
|
let iterator = children.into_iter();
|
||||||
|
|
||||||
|
Self::with_capacity(iterator.size_hint().0).extend(iterator)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a [`Row`] from an already allocated [`Vec`].
|
/// Creates a [`Row`] from an already allocated [`Vec`].
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue