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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::sync::Arc;
use std::any::TypeId;
use std::fmt;

use super::Component;

#[derive(Clone)]
pub enum ViewKind {
    String(String),
    Component(Arc<Component>),
}

unsafe impl Sync for ViewKind {}
unsafe impl Send for ViewKind {}

impl<'a> From<&'a str> for ViewKind {
    #[inline(always)]
    fn from(string: &'a str) -> Self {
        ViewKind::String(string.to_owned())
    }
}

impl From<String> for ViewKind {
    #[inline(always)]
    fn from(string: String) -> Self {
        ViewKind::String(string)
    }
}

impl From<Arc<Component>> for ViewKind {
    #[inline]
    fn from(component: Arc<Component>) -> Self {
        ViewKind::Component(component)
    }
}

impl<T> From<T> for ViewKind
where
    T: Component,
{
    #[inline]
    fn from(component: T) -> Self {
        ViewKind::Component(Arc::new(component))
    }
}

impl PartialEq for ViewKind {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        match self {
            &ViewKind::String(ref a) => match other {
                &ViewKind::String(ref b) => a == b,
                &ViewKind::Component(_) => false,
            },
            &ViewKind::Component(ref a) => match other {
                &ViewKind::String(_) => false,
                &ViewKind::Component(ref b) => a.get_type_id() == b.get_type_id(),
            },
        }
    }
}
impl PartialEq<str> for ViewKind {
    #[inline]
    fn eq(&self, other: &str) -> bool {
        match self {
            &ViewKind::String(ref a) => a == other,
            &ViewKind::Component(_) => false,
        }
    }
}

impl fmt::Debug for ViewKind {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &ViewKind::String(ref string) => f.write_str(string),
            &ViewKind::Component(ref component) => write!(f, "Component({})", component.name()),
        }
    }
}

impl fmt::Display for ViewKind {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl ViewKind {
    #[inline]
    pub fn is_string(&self) -> bool {
        match self {
            &ViewKind::String(_) => true,
            &ViewKind::Component(_) => false,
        }
    }
    #[inline]
    pub fn is_component(&self) -> bool {
        match self {
            &ViewKind::String(_) => false,
            &ViewKind::Component(_) => true,
        }
    }
    #[inline]
    pub fn type_id(&self) -> TypeId {
        match self {
            &ViewKind::String(_) => TypeId::of::<String>(),
            &ViewKind::Component(ref component) => (&**component).get_type_id(),
        }
    }
    #[inline]
    pub fn take_string(self) -> String {
        match self {
            ViewKind::String(string) => string,
            ViewKind::Component(_) => panic!("ViewKind::Component can not be a String"),
        }
    }
}