ori_core/context/
contexts.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::{any::Any, mem};

/// A context for a view.
#[derive(Debug, Default)]
pub struct Contexts {
    contexts: Vec<Box<dyn Any>>,
}

impl Contexts {
    /// Create a new context.
    pub fn new() -> Self {
        Self::default()
    }

    #[inline(always)]
    fn index_of<T: Any>(&self) -> Option<usize> {
        self.contexts
            .iter()
            .enumerate()
            .find(|(_, c)| c.as_ref().is::<T>())
            .map(|(i, _)| i)
    }

    /// Get the number of contexts.
    #[inline(always)]
    pub fn len(&self) -> usize {
        self.contexts.len()
    }

    /// Check if there are no contexts.
    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.contexts.is_empty()
    }

    /// Check if the context is present.
    #[inline(always)]
    pub fn contains<T: Any>(&self) -> bool {
        self.index_of::<T>().is_some()
    }

    /// Push a context.
    #[inline(always)]
    pub fn insert<T: Any>(&mut self, mut context: T) -> Option<T> {
        if let Some(index) = self.get_mut::<T>() {
            mem::swap(index, &mut context);
            return Some(context);
        }

        self.contexts.push(Box::new(context));

        None
    }

    /// Pop a context.
    #[inline(always)]
    pub fn remove<T: Any>(&mut self) -> Option<T> {
        let index = self.index_of::<T>()?;

        let context = self.contexts.remove(index);
        Some(*context.downcast::<T>().expect("downcast failed"))
    }

    /// Get a context.
    #[inline(always)]
    pub fn get<T: Any>(&self) -> Option<&T> {
        let index = self.index_of::<T>()?;
        self.contexts[index].downcast_ref::<T>()
    }

    /// Get a mutable context.
    #[inline(always)]
    pub fn get_mut<T: Any>(&mut self) -> Option<&mut T> {
        let index = self.index_of::<T>()?;
        self.contexts[index].downcast_mut::<T>()
    }

    /// Get a context or insert a `default`.
    #[inline(always)]
    pub fn get_or_default<T: Any + Default>(&mut self) -> &mut T {
        if !self.contains::<T>() {
            self.insert(T::default());
        }

        self.get_mut::<T>().unwrap()
    }
}