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
//! A little crate providing a type for creating a [`Arc`][`std::sync::Arc`] on the stack.
use MaybeUninit;
use ;
use NonNull;
/// A type with the same layout as the allocated value of [`Arc`] that it's initialized
/// once you convert a mutable reference to it.
///
/// This type consist of the strong,weak count of the [`Arc`] and a [`MaybeUninit`] holding the
/// data.The strong it's set to zero until you cast to the first [`Arc`] with [`to_arc`] or
/// [`to_arc_with`] thus upgrading the count.
///
/// # Examples
///
/// The first use-case of this type is lazy-init an mutable static across threads and drop it when
/// all ceases to use it.
///
/// ```
/// use stackarc::ArcInner;
/// use std::thread::{sleep, spawn};
/// use std::time::Duration;
///
/// static mut A: ArcInner<String> = ArcInner::uninit();
///
/// fn main() {
/// let x = spawn(|| unsafe {
/// let _a = A.to_arc_with(|| format!("foobar"));
/// sleep(Duration::from_secs(2));
/// });
///
/// let y = spawn(|| unsafe {
/// let _a = A.to_arc_with(|| format!("barfoo"));
/// sleep(Duration::from_secs(2));
/// });
///
/// // wait one second to wait for the threads to initialize the value
/// // which in turn wait another to maintain their reference alive
/// // and don't drop the count until create another
/// sleep(Duration::from_secs(1));
/// let z = unsafe { A.to_arc_with(|| format!("baz")) };
///
/// assert_ne!(*z, "baz");
///
/// drop(z);
/// x.join().unwrap();
/// y.join().unwrap();
/// let z = unsafe { A.to_arc_with(|| format!("foo")) };
///
/// assert_eq!(*z, "foo");
/// }
/// ```
///
/// [`to_arc`]: #method.to_arc
/// [`to_arc_with`]: #method.to_arc_with