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
use Deref;
/// A wrapper that provides read-only access to a value
///
/// <div class="warning">
///
/// This type is meant to prevent accidental modifications of values after construction.
/// It can not however, prevent *replacement* of the value itself. For example:
///
/// ```
/// use cut::Immutable;
///
/// let mut val = Immutable::new(10);
///
/// // later...
/// val = Immutable::new(11);
///
/// assert_eq!(*val, 11);
/// ```
///
/// ```
/// use cut::Immutable;
///
/// struct Foo {
/// bar: Immutable<bool>,
/// }
///
/// impl Foo {
/// pub fn new(bar: bool) -> Self {
/// Self {
/// bar: Immutable::new(bar),
/// }
/// }
///
/// pub fn bar(&mut self) -> bool {
/// // ERROR
/// //self.bar = !*self.bar;
///
/// // if From<T> was implemented for Immutable<T>, you might accidently do this:
/// //self.bar = (!*self.bar).into();
///
/// // uh oh...
/// // atleast you can explictly see that a new Immutable<T> is being assigned
/// self.bar = Immutable::new(!*self.bar);
///
/// *self.bar
/// }
/// }
///
/// let mut foo = Foo::new(false);
///
/// assert_eq!(foo.bar(), true);
/// assert_eq!(foo.bar(), false);
/// assert_eq!(foo.bar(), true);
/// ```
///
/// </div>
///
/// <div class="warning">
///
/// Types with interior mutability such as [`Cell`][core::cell::Cell] can still be modified when
/// wrapped. This includes nesting such as the following:
///
/// ```
/// use core::cell::Cell;
///
/// use cut::Immutable;
///
/// struct Foo {
/// bar: Bar,
/// }
///
/// struct Bar {
/// baz: Cell<bool>,
/// }
///
/// let foo = Immutable::new(Foo {
/// bar: Bar {
/// baz: Cell::new(false),
/// },
/// });
///
/// foo.bar.baz.set(true);
///
/// assert_eq!(foo.bar.baz.get(), true);
/// ```
///
/// </div>
;