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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
use std::fmt;

use crate::shim::cell::UnsafeCell;
use crate::init::Init;

/// An init-once cell for global access to a value.
///
/// A `InitCell` instance can hold a single value in a global context. A
/// `InitCell` instance begins without a value and must be initialized via the
/// [`set()`](#method.set) method. Once a value has been set, it can be
/// retrieved at any time and in any thread via the [`get()`](#method.get)
/// method. The [`try_get()`](#method.try_get) can be used to determine whether
/// the `InitCell` has been initialized before attempting to retrieve the value.
///
/// # Example
///
/// The following example uses `InitCell` to hold a global instance of a
/// `HashMap` which can be modified at will:
///
/// ```rust
/// use std::collections::HashMap;
/// use std::sync::Mutex;
/// use std::thread;
///
/// use state::InitCell;
///
/// static GLOBAL_MAP: InitCell<Mutex<HashMap<String, String>>> = InitCell::new();
///
/// fn run_program() {
///     let mut map = GLOBAL_MAP.get().lock().unwrap();
///     map.insert("another_key".into(), "another_value".into());
/// }
///
/// fn main() {
///     // Create the initial map and store it in `GLOBAL_MAP`.
///     let mut initial_map = HashMap::new();
///     initial_map.insert("key".into(), "value".into());
///     GLOBAL_MAP.set(Mutex::new(initial_map));
///
///     // For illustration, we spawn a new thread that modified the map.
///     thread::spawn(|| run_program()).join().expect("thread");
///
///     // Assert that the modification took place.
///     let map = GLOBAL_MAP.get().lock().unwrap();
///     assert_eq!(map.get("another_key").unwrap(), "another_value");
/// }
pub struct InitCell<T> {
    item: UnsafeCell<Option<T>>,
    init: Init
}

/// Defaults to [`InitCell::new()`].
impl<T> Default for InitCell<T> {
    fn default() -> Self {
        InitCell::new()
    }
}

impl<T> InitCell<T> {
    /// Create a new, uninitialized cell.
    ///
    /// To create a cell initializd with a value, use [`InitCell::from()`].
    ///
    /// # Example
    ///
    /// ```rust
    /// use state::InitCell;
    ///
    /// static MY_GLOBAL: InitCell<String> = InitCell::new();
    /// ```
    #[cfg(not(loom))]
    pub const fn new() -> InitCell<T> {
        InitCell {
            item: UnsafeCell::new(None),
            init: Init::new()
        }
    }

    /// New, for loom.
    #[cfg(loom)]
    pub fn new() -> InitCell<T> {
        InitCell {
            item: UnsafeCell::new(None),
            init: Init::new()
        }
    }

    /// Sets this cell's value to `value` if it is not already initialized.
    ///
    /// If there are multiple simultaneous callers, exactly one is guaranteed to
    /// receive `true`, indicating its value was set. All other callers receive
    /// `false`, indicating the value was ignored.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use state::InitCell;
    /// static MY_GLOBAL: InitCell<&'static str> = InitCell::new();
    ///
    /// assert_eq!(MY_GLOBAL.set("Hello, world!"), true);
    /// assert_eq!(MY_GLOBAL.set("Goodbye, world!"), false);
    /// ```
    pub fn set(&self, value: T) -> bool {
        if self.init.needed() {
            unsafe { self.item.with_mut(|ptr| *ptr = Some(value)); }
            self.init.mark_complete();
            return true;
        }

        false
    }

    /// Resets the cell to an uninitialized state.
    ///
    /// # Example
    ///
    /// ```rust
    /// use state::InitCell;
    ///
    /// let mut cell = InitCell::from(5);
    /// assert_eq!(cell.get(), &5);
    ///
    /// cell.reset();
    /// assert!(cell.try_get().is_none());
    /// ```
    pub fn reset(&mut self) {
        *self = Self::new();
    }

    /// If the cell is not initialized, it is set `f()`. Returns a borrow to the
    /// value in this cell.
    ///
    /// If `f()` panics during initialization, the cell is left uninitialized.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use state::InitCell;
    /// static MY_GLOBAL: InitCell<&'static str> = InitCell::new();
    ///
    /// assert_eq!(*MY_GLOBAL.get_or_init(|| "Hello, world!"), "Hello, world!");
    /// ```
    #[inline]
    pub fn get_or_init<F: FnOnce() -> T>(&self, f: F) -> &T {
        if let Some(value) = self.try_get() {
            value
        } else {
            self.set(f());
            self.try_get().expect("cell::get_or_init(): set() => get() ok")
        }
    }

    /// Waits (blocks) until the cell has a value and then borrows it.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use state::InitCell;
    /// static MY_GLOBAL: InitCell<&'static str> = InitCell::new();
    ///
    /// MY_GLOBAL.set("Hello, world!");
    /// assert_eq!(*MY_GLOBAL.get(), "Hello, world!");
    /// ```
    #[inline]
    pub fn wait(&self) -> &T {
        self.init.wait_until_complete();
        self.try_get().expect("cell::wait(): broken (init await complete w/o value)")
    }

    /// Get a reference to the underlying value, if one is set.
    ///
    /// Returns `Some` if the state has previously been set via methods like
    /// [`InitCell::set()`] or [`InitCell::get_or_init()`]. Otherwise returns
    /// `None`.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use state::InitCell;
    /// static MY_GLOBAL: InitCell<&'static str> = InitCell::new();
    ///
    /// assert_eq!(MY_GLOBAL.try_get(), None);
    ///
    /// MY_GLOBAL.set("Hello, world!");
    ///
    /// assert_eq!(MY_GLOBAL.try_get(), Some(&"Hello, world!"));
    /// ```
    #[inline]
    pub fn try_get(&self) -> Option<&T> {
        if self.init.has_completed() {
            unsafe { self.item.with(|ptr| (*ptr).as_ref()) }
        } else {
            None
        }
    }

    /// Returns a mutable reference to the underlying data if any is set.
    ///
    /// This call borrows `InitCell` mutably (at compile-time) so there is no
    /// need for dynamic checks.
    ///
    /// # Example
    ///
    /// ```rust
    /// use state::InitCell;
    ///
    /// let mut cell = InitCell::from(5);
    /// *cell.try_get_mut().unwrap() += 1;
    ///
    /// let mut cell: InitCell<usize> = InitCell::new();
    /// assert!(cell.try_get_mut().is_none());
    /// ```
    pub fn try_get_mut(&mut self) -> Option<&mut T> {
        self.item.get_mut().as_mut()
    }

    /// Borrows the value in this cell, panicking if there is no value.
    ///
    /// # Panics
    ///
    /// Panics if a value has not previously been [`set()`](#method.set). Use
    /// [`try_get()`](#method.try_get) for a non-panicking version.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use state::InitCell;
    /// static MY_GLOBAL: InitCell<&'static str> = InitCell::new();
    ///
    /// MY_GLOBAL.set("Hello, world!");
    /// assert_eq!(*MY_GLOBAL.get(), "Hello, world!");
    /// ```
    #[inline]
    pub fn get(&self) -> &T {
        self.try_get().expect("cell::get(): called get() before set()")
    }

    /// Resets the cell to an uninitialized state and returns the inner value if
    /// any was set.
    ///
    /// # Example
    ///
    /// ```rust
    /// use state::InitCell;
    ///
    /// let mut cell = InitCell::from(5);
    /// assert_eq!(cell.get(), &5);
    /// assert_eq!(cell.get(), &5);
    ///
    /// assert_eq!(cell.take(), Some(5));
    /// assert_eq!(cell.take(), None);
    /// ```
    pub fn take(&mut self) -> Option<T> {
        std::mem::replace(self, Self::new()).into_inner()
    }

    /// Returns the inner value if any is set.
    ///
    /// # Example
    ///
    /// ```rust
    /// use state::InitCell;
    ///
    /// let cell = InitCell::from(5);
    /// assert_eq!(cell.into_inner().unwrap(), 5);
    ///
    /// let cell: InitCell<usize> = InitCell::new();
    /// assert!(cell.into_inner().is_none());
    /// ```
    pub fn into_inner(self) -> Option<T> {
        self.item.into_inner()
    }

    /// Applies the function `f` to the inner value, if there is any, leaving
    /// the updated value in the cell.
    ///
    /// If `f()` panics during updating, the cell is left uninitialized.
    ///
    /// # Example
    ///
    /// ```rust
    /// use state::InitCell;
    ///
    /// let mut cell = InitCell::from(5);
    /// cell.update(|v| v + 10);
    /// assert_eq!(cell.wait(), &15);
    ///
    /// let mut cell = InitCell::new();
    /// cell.update(|v: u8| v + 10);
    /// assert!(cell.try_get().is_none());
    /// ```
    pub fn update<F: FnOnce(T) -> T>(&mut self, f: F) {
        self.take().map(|v| self.set(f(v)));
    }

    /// Applies the function `f` to the inner value, if there is any, and
    /// returns a new `InitCell` with mapped value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use state::InitCell;
    ///
    /// let cell = InitCell::from(5);
    /// assert_eq!(cell.get(), &5);
    ///
    /// let cell = cell.map(|v| v + 10);
    /// assert_eq!(cell.get(), &15);
    /// ```
    pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> InitCell<U> {
        self.into_inner().map_or_else(InitCell::new, |v| InitCell::from(f(v)))
    }
}

unsafe impl<T: Send> Send for InitCell<T> {  }

unsafe impl<T: Send + Sync> Sync for InitCell<T> {  }

impl<T: fmt::Debug> fmt::Debug for InitCell<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match self.try_get() {
            Some(object) => object.fmt(f),
            None => write!(f, "[uninitialized cell]")
        }
    }
}

impl<T> From<T> for InitCell<T> {
    fn from(value: T) -> InitCell<T> {
        let cell = InitCell::new();
        assert!(cell.set(value));
        cell
    }
}

impl<T: Clone> Clone for InitCell<T> {
    fn clone(&self) -> InitCell<T> {
        match self.try_get() {
            Some(val) => InitCell::from(val.clone()),
            None => InitCell::new()
        }
    }
}