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
use std::marker::{PhantomData};

pub trait Toggle<T> {
  fn as_ref(&self) -> Option<&T>;
  fn as_mut(&mut self) -> Option<&mut T>;
}

pub struct Disabled<T> {
  _marker:  PhantomData<T>,
}

impl<T> Disabled<T> {
  #[inline]
  pub fn new() -> Disabled<T> {
    Disabled{_marker: PhantomData}
  }
}

impl<T> Toggle<T> for Disabled<T> {
  #[inline]
  fn as_ref(&self) -> Option<&T> {
    None
  }

  #[inline]
  fn as_mut(&mut self) -> Option<&mut T> {
    None
  }
}

pub struct Enabled<T> {
  value:    T,
}

impl<T> Enabled<T> {
  #[inline]
  pub fn new(value: T) -> Enabled<T> {
    Enabled{value: value}
  }
}

impl<T> Toggle<T> for Enabled<T> {
  #[inline]
  fn as_ref(&self) -> Option<&T> {
    Some(&self.value)
  }

  #[inline]
  fn as_mut(&mut self) -> Option<&mut T> {
    Some(&mut self.value)
  }
}

#[cfg(test)]
mod tests {

  use super::{Toggle, Disabled, Enabled};

  struct Hello<Tg> where Tg: Toggle<String> {
    t: Tg,
  }

  #[test]
  fn test_disabled() {
    let hello = Hello{t: Disabled::new()};
    assert!(hello.t.as_ref().is_none());
  }

  #[test]
  fn test_disabled_mut() {
    let mut hello = Hello{t: Disabled::new()};
    assert!(hello.t.as_mut().is_none());
  }

  #[test]
  fn test_enabled() {
    let hello = Hello{t: Enabled::new(format!("Hello, world!"))};
    assert!(hello.t.as_ref().is_some());
  }

  #[test]
  fn test_enabled_mut() {
    let mut hello = Hello{t: Enabled::new(format!("Hello, world!"))};
    *hello.t.as_mut().unwrap() = format!("Goodbye, cruel world!");
    assert_eq!("Goodbye, cruel world!", hello.t.as_ref().unwrap());
  }

}