1use crate::layout_item::LayoutItem;
2
3#[derive(Default)]
9pub struct Slots {
10 items: Vec<(Option<&'static str>, Box<dyn LayoutItem>)>,
11}
12
13impl Slots {
14 pub fn new() -> Self {
15 Self::default()
16 }
17
18 pub fn push(&mut self, name: Option<&'static str>, item: Box<dyn LayoutItem>) {
19 self.items.push((name, item));
20 }
21
22 pub fn take_default(&mut self) -> Vec<Box<dyn LayoutItem>> {
24 self.take_matching(|n| n.is_none())
25 }
26
27 pub fn take(&mut self, name: &str) -> Vec<Box<dyn LayoutItem>> {
29 self.take_matching(|n| *n == Some(name))
30 }
31
32 fn take_matching(
33 &mut self,
34 pred: impl Fn(&Option<&'static str>) -> bool,
35 ) -> Vec<Box<dyn LayoutItem>> {
36 let mut taken = Vec::new();
37 let mut rest = Vec::new();
38 for (name, item) in std::mem::take(&mut self.items) {
39 if pred(&name) {
40 taken.push(item);
41 } else {
42 rest.push((name, item));
43 }
44 }
45 self.items = rest;
46 taken
47 }
48}