snarc 0.1.0

Sendable Non-Atomically Reference Counted
Documentation
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#[doc(hidden)]
pub use scopeguard;

/// Defines three structs, each named by one parameter.
///
///  - `$send`, a sendable owning/strong reference,
///  - `$unsend`, an unsendable owning/strong reference, and
///  - `$ref`, a sendable weak reference.
///
/// Note that this macro declares a `thread_local!` that is is shared by all
/// instances. That means that instances cannot bind their value to the current
/// thread at the same time. If this is too restrictive, enable the
/// `thread-local` feature and use the structs defined in the `thread_local`
/// module.
#[macro_export]
macro_rules! snarc {
    ($send:ident, $unsend:ident, $ref:ident $(, $expect:literal)?) => {
        pub use _snarc_impl::$send;
        pub use _snarc_impl::$unsend;
        pub use _snarc_impl::$ref;

        mod _snarc_impl {
            use std::alloc;
            use std::ops::Deref;
            use std::ops::DerefMut;
            use std::ptr;

            use $crate::Context;
            use $crate::ErasedNarc;
            use $crate::ErasedSnarc;
            use $crate::State;

            thread_local!(static THREAD_LOCAL: std::cell::Cell<State> = Default::default());

            struct SnarcBox<T> {
                count: std::cell::Cell<usize>,
                value: T,
            }

            impl<T> SnarcBox<T> {
                fn new_ptr(value: T) -> *mut Self {
                    Box::leak(Box::new(Self {
                        count: std::cell::Cell::new(0),
                        value,
                    }))
                }
            }

            pub struct $send<T> {
                ptr: *mut SnarcBox<T>,
                phantom: std::marker::PhantomData<SnarcBox<T>>,
            }

            unsafe impl<T: Send> Send for $send<T> {}
            unsafe impl<T: Sync> Sync for $send<T> {}

            impl<T> $send<T> {
                /// Creates a new `
                #[doc = stringify!($send)]
                /// ` with the given inner `value`.
                pub fn new(value: T) -> Self {
                    Self {
                        ptr: SnarcBox::new_ptr(value),
                        phantom: std::marker::PhantomData,
                    }
                }

                /// Turn this `
                #[doc = stringify!($send)]
                /// ` into the `!Send` version `
                #[doc = stringify!($unsend)]
                /// `.
                pub fn into_unsend(mut self) -> $unsend<T> {
                    let narc = $unsend {
                        ptr: self.ptr,
                        phantom: self.phantom,
                    };

                    self.ptr = ptr::null_mut();

                    narc
                }

                /// Turn this parameterized `
                #[doc = stringify!($send)]
                /// ` the unparameterized `ErasedSnarc`.
                pub fn into_erased(self) -> ErasedSnarc
                where
                    T: Send + 'static,
                {
                    let snarc: Box<dyn Context + Send + 'static> = Box::new(self);
                    ErasedSnarc::from(snarc)
                }

                #[inline(always)]
                fn inner(&self) -> &SnarcBox<T> {
                    unsafe { &*self.ptr }
                }

                #[inline]
                unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {
                    &mut (*this.ptr).value
                }

                /// Creates a new non-owning reference to the inner value.
                pub fn new_ref(&self) -> $ref<T> {
                    let inner = self.inner();

                    inner.count.set(inner.count.get() + 1);

                    $ref {
                        ptr: self.ptr,
                        phantom: Default::default(),
                    }
                }

                /// Temporarily bind the inner value to this thread and evaluate `f`
                /// within that context.
                pub fn enter<F, R>(&mut self, f: F) -> R
                where
                    F: FnOnce(&T) -> R,
                {
                    THREAD_LOCAL.with(|c| {
                        if c.get() == State::Entered {
                            panic!(concat!(
                                "Another ",
                                stringify!($send),
                                " is already entered."
                            ))
                        }

                        c.set(State::Entered);
                    });

                    let _guard = $crate::scopeguard::guard((), |_| {
                        THREAD_LOCAL.with(|c| c.set(State::Default));
                    });

                    f(&self.inner().value)
                }
            }

            impl<T: Send + 'static> From<$send<T>> for ErasedSnarc {
                fn from(snarc: $send<T>) -> Self {
                    snarc.into_erased()
                }
            }

            impl<T: Send + 'static> From<$send<T>> for ErasedNarc {
                fn from(snarc: $send<T>) -> Self {
                    snarc.into_unsend().into_erased()
                }
            }

            impl<T> Context for $send<T> {
                fn set(&mut self, v: State) {
                    THREAD_LOCAL.with(|c| {
                        if v == State::Entered && c.get() == State::Entered {
                            panic!(concat!(
                                "Another ",
                                stringify!($send),
                                " is already entered."
                            ))
                        }

                        c.set(v);
                    });
                }
            }

            impl<T> Deref for $send<T> {
                type Target = T;

                #[inline(always)]
                fn deref(&self) -> &Self::Target {
                    &self.inner().value
                }
            }

            impl<T> DerefMut for $send<T> {
                #[inline(always)]
                fn deref_mut(&mut self) -> &mut Self::Target {
                    unsafe { Self::get_mut_unchecked(self) }
                }
            }

            impl<T> Drop for $send<T> {
                fn drop(&mut self) {
                    if !self.ptr.is_null() {
                        THREAD_LOCAL.with(|c| {
                            if c.get() == State::Entered {
                                panic!(concat!(
                                    "Another ",
                                    stringify!($send),
                                    " is already entered."
                                ))
                            }

                            c.set(State::Entered)
                        });

                        let _guard = $crate::scopeguard::guard((), |_| {
                            THREAD_LOCAL.with(|c| c.set(State::Default));
                        });

                        unsafe {
                            // destroy the contained object
                            ptr::drop_in_place(Self::get_mut_unchecked(self));
                        }

                        if self.inner().count.get() == 0 {
                            unsafe {
                                ptr::addr_of_mut!((*self.ptr).count).drop_in_place();
                                let layout = alloc::Layout::for_value(&*self.ptr);
                                alloc::dealloc(self.ptr.cast(), layout);
                            }
                        }
                    }
                }
            }

            /// A non-sendable, owning reference-counted pointer to a `T`.
            ///
            /// When `
            #[doc = stringify!($unsend)]
            /// ` is used exclusively, i.e., never `
            #[doc = stringify!($send)]
            /// `, then `Rc` should
            /// likely be used instead.
            pub struct $unsend<T> {
                ptr: *mut SnarcBox<T>,
                phantom: std::marker::PhantomData<SnarcBox<T>>,
            }

            unsafe impl<T: Sync> Sync for $unsend<T> {}

            impl<T> $unsend<T> {
                /// Creates a new `
                #[doc = stringify!($unsend)]
                /// ` with the given inner `value`.
                pub fn new(value: T) -> Self {
                    Self {
                        ptr: SnarcBox::new_ptr(value),
                        phantom: std::marker::PhantomData,
                    }
                }

                /// Turn this `
                #[doc = stringify!($unsend)]
                /// ` into the `Send` version `
                #[doc = stringify!($send)]
                /// `.
                pub fn into_send(mut self) -> $send<T> {
                    let snarc = $send {
                        ptr: self.ptr,
                        phantom: self.phantom,
                    };

                    self.ptr = ptr::null_mut();

                    snarc
                }

                /// Turn this parameterized `
                #[doc = stringify!($unsend)]
                /// ` the unparameterized `ErasedNarc`.
                pub fn into_erased(self) -> ErasedNarc
                where
                    T: Send + 'static,
                {
                    self.into_send().into_erased().into_unsend()
                }

                #[inline(always)]
                fn inner(&self) -> &SnarcBox<T> {
                    unsafe { &*self.ptr }
                }

                #[inline]
                unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {
                    &mut (*this.ptr).value
                }

                /// Creates a new non-owning reference to the inner value.
                pub fn new_ref(&self) -> $ref<T> {
                    let inner = self.inner();

                    inner.count.set(inner.count.get() + 1);

                    $ref {
                        ptr: self.ptr,
                        phantom: Default::default(),
                    }
                }
            }

            impl<T: Send + 'static> From<$unsend<T>> for ErasedSnarc {
                fn from(narc: $unsend<T>) -> Self {
                    narc.into_send().into_erased()
                }
            }

            impl<T: Send + 'static> From<$unsend<T>> for ErasedNarc {
                fn from(narc: $unsend<T>) -> Self {
                    narc.into_erased()
                }
            }

            impl<T> Deref for $unsend<T> {
                type Target = T;

                #[inline(always)]
                fn deref(&self) -> &Self::Target {
                    &self.inner().value
                }
            }

            impl<T> DerefMut for $unsend<T> {
                #[inline(always)]
                fn deref_mut(&mut self) -> &mut Self::Target {
                    unsafe { Self::get_mut_unchecked(self) }
                }
            }

            impl<T> Drop for $unsend<T> {
                fn drop(&mut self) {
                    if !self.ptr.is_null() {
                        THREAD_LOCAL.with(|c| {
                            if c.get() == State::Entered {
                                panic!(concat!(
                                    "Another ",
                                    stringify!($send),
                                    " is already entered."
                                ))
                            }

                            c.set(State::Entered)
                        });

                        let _guard = $crate::scopeguard::guard((), |_| {
                            THREAD_LOCAL.with(|c| c.set(State::Default));
                        });

                        unsafe {
                            // destroy the contained object
                            ptr::drop_in_place(Self::get_mut_unchecked(self));
                        }

                        if self.inner().count.get() == 0 {
                            unsafe {
                                ptr::addr_of_mut!((*self.ptr).count).drop_in_place();
                                let layout = alloc::Layout::for_value(&*self.ptr);
                                alloc::dealloc(self.ptr.cast(), layout);
                            }
                        }
                    }
                }
            }

            pub struct $ref<T> {
                ptr: *mut SnarcBox<T>,
                phantom: std::marker::PhantomData<SnarcBox<T>>,
            }

            unsafe impl<T> Send for $ref<T> {}
            unsafe impl<T> Sync for $ref<T> {}

            impl<T> $ref<T> {
                #[inline(always)]
                fn inner(&self) -> &SnarcBox<T> {
                    unsafe { &*self.ptr }
                }

                pub fn get(&self) -> Option<&T> {
                    let inner = self.inner();

                    if THREAD_LOCAL.with(|c| c.get().is_set()) {
                        Some(&inner.value)
                    } else {
                        None
                    }
                }

                $(
                    pub fn expect(&self) -> &T {
                        self.get().expect($expect)
                    }
                )?
            }

            impl<T> Clone for $ref<T> {
                fn clone(&self) -> Self {
                    if THREAD_LOCAL.with(|c| c.get().is_set()) {
                        let inner = self.inner();

                        inner.count.set(inner.count.get() + 1);

                        Self {
                            ptr: self.ptr,
                            phantom: Default::default(),
                        }
                    } else {
                        panic!(concat!(
                            stringify!($ref),
                            "::clone() outside of ",
                            stringify!($send),
                            "::enter(…)"
                        ))
                    }
                }
            }

            impl<T> Drop for $ref<T> {
                fn drop(&mut self) {
                    if THREAD_LOCAL.with(|c| c.get().is_set()) {
                        let inner = self.inner();

                        inner.count.set(inner.count.get() - 1);
                    } else {
                        #[cfg(debug_assertions)]
                        panic!(concat!(
                            stringify!($ref),
                            "::drop() outside of ",
                            stringify!($send),
                            "::enter(…)"
                        ))
                    }
                }
            }
        }
    };
}

#[cfg(test)]
mod tests {
    crate::snarc!(Snarc, Narc, SnarcRef, "expectation");

    crate::tests::tests!(Snarc, Narc, SnarcRef);
}