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
//! Snarc provides a Sendable Non-Atomically Reference-Counted smart-pointer.
//!
//! [![crates.io][crates.io-badge]][crates.io-url]
//! [![docs.rs][docs.rs-badge]][docs.rs-url]
//! [![MIT licensed][mit-badge]][mit-url]
//!
//! [crates.io-badge]: https://img.shields.io/crates/v/snarc.svg
//! [crates.io-url]: https://crates.io/crates/snarc
//! [docs.rs-badge]: https://docs.rs/snarc/badge.svg
//! [docs.rs-url]: https://docs.rs/snarc
//! [mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
//! [mit-url]: https://github.com/benschulz/snarc/blob/master/LICENSE
//!
//! # How does it work
//!
//! In order to be both sendable _and_ non-atomically reference counted,
//! trade-offs must be made. Those trade-offs are as follows.
//!
//! - There is only one strong/owning reference and arbitrarily many weak
//! references. By invoking the `enter` method of the strong/owning reference
//! its value may be temporarily bound to the current thread.
//!
//! - Weak references may only be created and dropped within the `enter` context
//! of a strong/owning reference. This ensures that the required counter
//! increments and decrements are race-free.
//!
//! - Calling the `get` method on a weak reference returns an `Option<&T>`, that
//! is `Some(&t)` iff called from within the `enter` context of a strong
//! reference.
//!
//! # What is it good for?
//!
//! The use case that motivated the implementation of snarc is quite niche. It
//! looks something like the following.
//!
//! ```
//! # use std::future::Future;
//! # use futures::stream::{FuturesUnordered, StreamExt};
//! # struct X {}
//! # impl X { fn method(&mut self) -> impl Future<Output = ()> { async {} } }
//! #
//! # let mut x = X {};
//! #
//! // We have an async task.
//! let task = async {
//! // This task is creating and executing subtasks.
//! let subtasks = FuturesUnordered::new();
//!
//! // `x.method()` is returning 'static Futures that share mutable state
//! subtasks.push(x.method());
//! subtasks.push(x.method());
//!
//! // Somewhere within the same task, the subtasks are executed.
//! subtasks.for_each(|x| async { /* ... */ });
//! };
//! ```
//!
//! Because the futures returned by `x.method()` share mutable state, that state
//! must be wrapped in a `RefCell`. And because the futures also have a
//! `'static` lifetime, that `RefCell` must be wrapped by a reference counted
//! smart pointer.
//!
//! ## Alternatives
//!
//! Given the problem statement above, here are the alternative solutions.
//!
//! ### Use `&RefCell<T>` after all
//!
//! This isn't really a solution to the problem statement, but maybe you can
//! relax your requirements? Maybe you don't need the returned futures to have a
//! `'static` lifetime?
//!
//! **Advantages**
//! - no overhead/maximally efficient
//!
//! **Drawbacks**
//! - `task` will be `!Send`
//! - addresses a different problem
//!
//! ### Use `Rc<RefCell<T>>`
//!
//! **Advantages**
//! - highly efficient, minor overhead of reference counting
//!
//! **Drawbacks**
//! - `task` will be `!Send`
//!
//! ### Use `Arc<Mutex<T>>`
//!
//! **Advantages**
//! - `task` will be `Send`
//! - highly ergonomical
//! - subtasks can even be turned into tasks of their own and executed on a
//! different thread
//!
//! **Drawbacks**
//! - inefficient, due to locking overhead
//!
//! ### Use `Snarc<RefCell<T>` and `SnarcRef<RefCell<T>>`
//!
//! **Advantages**
//! - highly efficient, minor overhead of reference counting
//! - `task` can be `Send`
//!
//! **Drawbacks**
//! - the ergonomics are iffy
//
// Lint configuration
pub use *;
/// A `Snarc<T>` whose type argument was erased.
///
/// `ErasedSnarc`s cannot be used to access the (type-erased) inner value. They
/// can only be used to temporarily bind the value to the current thread using
/// the [`enter`][ErasedSnarc::enter()] method.
unsafe
unsafe
/// A unsendable `Snarc<T>` whose type argument was erased.
///
/// `ErasedNarc`s cannot be used to access the (type-erased) inner value.