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
use crate::view::{IntoBoxedView, View, ViewWrapper};
use std::ops::{Deref, DerefMut};

/// A boxed `View`.
///
/// It derefs to the wrapped view.
pub struct BoxedView {
    view: Box<dyn View>,
}

impl BoxedView {
    /// Creates a new `BoxedView` around the given boxed view.
    pub fn new(view: Box<dyn View>) -> Self {
        BoxedView { view }
    }

    /// Box the given view
    pub fn boxed<T>(view: T) -> Self
    where
        T: IntoBoxedView,
    {
        BoxedView::new(view.into_boxed_view())
    }

    /// Returns the inner boxed view.
    pub fn unwrap(self) -> Box<dyn View> {
        self.view
    }
}

impl Deref for BoxedView {
    type Target = dyn View;

    fn deref(&self) -> &dyn View {
        &*self.view
    }
}

impl DerefMut for BoxedView {
    fn deref_mut(&mut self) -> &mut dyn View {
        &mut *self.view
    }
}

impl ViewWrapper for BoxedView {
    type V = dyn View;

    fn with_view<F, R>(&self, f: F) -> Option<R>
    where
        F: FnOnce(&Self::V) -> R,
    {
        Some(f(&*self.view))
    }

    fn with_view_mut<F, R>(&mut self, f: F) -> Option<R>
    where
        F: FnOnce(&mut Self::V) -> R,
    {
        Some(f(&mut *self.view))
    }
}