keepcalm 0.5.0

Simple shared types for multi-threaded programs
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
use crate::implementation::*;
use crate::locks::*;
use crate::projection::*;
use crate::synchronizer::SynchronizerType;
use crate::synchronizer::SynchronizerUnsized;
use crate::Shared;
use std::sync::Arc;

/// The [`SharedMut`] object hides the complexity of managing `Arc<Mutex<T>>` or `Arc<RwLock<T>>` behind a single interface:
///
/// ```rust
/// # use keepcalm::*;
/// let object = "123".to_string();
/// let shared = SharedMut::new(object);
/// shared.read();
/// ```
///
/// By default, a [`SharedMut`] object uses `Arc<RwLock<T>>` under the hood, but you can choose the synchronization primitive at
/// construction time. The [`SharedMut`] object *erases* the underlying primitive and you can use them interchangeably:
///
/// ```rust
/// # use keepcalm::*;
/// fn use_shared(shared: SharedMut<String>) {
///     shared.read();
/// }
///
/// let shared = SharedMut::new("123".to_string());
/// use_shared(shared);
/// let shared = SharedMut::new_mutex("123".to_string());
/// use_shared(shared);
/// ```
///
/// Managing the poison state of synchronization primitives can be challenging as well. Rust will poison a `Mutex` or `RwLock` if you
/// hold a lock while a `panic!` occurs.
///
/// The `SharedMut` type allows you to specify a [`PoisonPolicy`] at construction time. By default, if a synchronization
/// primitive is poisoned, the `SharedMut` will `panic!` on access. This can be configured so that poisoning is ignored:
///
/// ```rust
/// # use keepcalm::*;
/// let shared = SharedMut::new_with_policy("123".to_string(), PoisonPolicy::Ignore);
/// ```
#[repr(transparent)]
pub struct SharedMut<T: ?Sized> {
    pub(crate) inner_impl: SharedImpl<T>,
}

impl<T: ?Sized> std::panic::RefUnwindSafe for SharedMut<T> {}

// UNSAFETY: The construction and projection of SharedMut requires Send + Sync, so we can guarantee that
// all instances of SharedMut are Send + Sync.
unsafe impl<T: ?Sized> Send for SharedMut<T> {}
unsafe impl<T: ?Sized> Sync for SharedMut<T> {}

// UNSAFETY: Requires the caller to pass something that's Send + Sync in U to avoid unsafely constructing a SharedMut from a non-Send/non-Sync type.
fn make_shared_rw_value<T: ?Sized>(inner_impl: SharedImpl<T>) -> SharedMut<T> {
    SharedMut { inner_impl }
}

// UNSAFETY: Projections are always Send + Sync safe.
fn make_shared_rw_projection<T: ?Sized>(inner_impl: SharedImpl<T>) -> SharedMut<T> {
    SharedMut { inner_impl }
}

// UNSAFETY: Safe to clone an object that is considered safe.
impl<T: ?Sized> Clone for SharedMut<T> {
    fn clone(&self) -> Self {
        Self {
            inner_impl: self.inner_impl.clone(),
        }
    }
}

impl<T: ?Sized + std::fmt::Debug> std::fmt::Debug for SharedMut<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.inner_impl.fmt(f)
    }
}

impl<T: ?Sized> SharedMut<T> {
    pub fn from_box(value: Box<T>) -> Self
    where
        Box<T>: Send + Sync + 'static,
    {
        make_shared_rw_value::<T>(SharedImpl::Box(SynchronizerUnsized::new(
            LockMetadata::poison_panic(),
            SynchronizerType::RwLock,
            value,
        )))
    }

    pub(crate) const fn from_inner(inner_impl: SharedImpl<T>) -> Self {
        Self { inner_impl }
    }
}

#[cfg(feature = "serde")]
use serde::Serialize;

#[cfg(feature = "serde")]
impl<'a, T: Serialize> Serialize for SharedMut<T>
where
    &'a T: Serialize + 'static,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.read().serialize(serializer)
    }
}

impl<T: ?Sized, P: ?Sized> SharedMutProjection<P> for (SharedMut<T>, ProjectorRW<T, P>) {
    fn lock_read(&self) -> SharedReadLock<P> {
        struct HiddenLock<'a, T: ?Sized, P: ?Sized> {
            lock: SharedReadLock<'a, T>,
            projector: &'a ProjectorRW<T, P>,
        }

        impl<'a, T: ?Sized, P: ?Sized> std::ops::Deref for HiddenLock<'a, T, P> {
            type Target = P;
            fn deref(&self) -> &Self::Target {
                (self.projector).project(&*self.lock)
            }
        }

        let lock = HiddenLock {
            lock: self.0.read(),
            projector: &self.1,
        };

        SharedReadLock::from_projection(Box::new(lock))
    }

    fn try_lock_read(&self) -> Option<SharedReadLock<P>> {
        let lock = self.0.try_read();
        if let Some(lock) = lock {
            struct HiddenLock<'a, T: ?Sized, P: ?Sized> {
                lock: SharedReadLock<'a, T>,
                projector: &'a ProjectorRW<T, P>,
            }

            impl<'a, T: ?Sized, P: ?Sized> std::ops::Deref for HiddenLock<'a, T, P> {
                type Target = P;
                fn deref(&self) -> &Self::Target {
                    (self.projector).project(&*self.lock)
                }
            }

            let lock = HiddenLock {
                lock,
                projector: &self.1,
            };

            Some(SharedReadLock::from_projection(Box::new(lock)))
        } else {
            None
        }
    }

    fn lock_write(&self) -> SharedWriteLock<P> {
        struct HiddenLock<'a, T: ?Sized, P: ?Sized> {
            lock: SharedWriteLock<'a, T>,
            projector: &'a ProjectorRW<T, P>,
        }

        impl<'a, T: ?Sized, P: ?Sized> std::ops::Deref for HiddenLock<'a, T, P> {
            type Target = P;
            fn deref(&self) -> &Self::Target {
                (self.projector).project(&*self.lock)
            }
        }

        impl<'a, T: ?Sized, P: ?Sized> std::ops::DerefMut for HiddenLock<'a, T, P> {
            fn deref_mut(&mut self) -> &mut Self::Target {
                (self.projector).project_mut(&mut *self.lock)
            }
        }

        let lock = HiddenLock {
            lock: self.0.write(),
            projector: &self.1,
        };

        SharedWriteLock::from_projection(Box::new(lock))
    }

    fn try_lock_write(&self) -> Option<SharedWriteLock<P>> {
        let lock = self.0.try_write();
        if let Some(lock) = lock {
            struct HiddenLock<'a, T: ?Sized, P: ?Sized> {
                lock: SharedWriteLock<'a, T>,
                projector: &'a ProjectorRW<T, P>,
            }

            impl<'a, T: ?Sized, P: ?Sized> std::ops::Deref for HiddenLock<'a, T, P> {
                type Target = P;
                fn deref(&self) -> &Self::Target {
                    (self.projector).project(&*self.lock)
                }
            }

            impl<'a, T: ?Sized, P: ?Sized> std::ops::DerefMut for HiddenLock<'a, T, P> {
                fn deref_mut(&mut self) -> &mut Self::Target {
                    (self.projector).project_mut(&mut *self.lock)
                }
            }

            let lock = HiddenLock {
                lock,
                projector: &self.1,
            };

            Some(SharedWriteLock::from_projection(Box::new(lock)))
        } else {
            None
        }
    }
}

// UNSAFETY: All construction functions are gated behind T: Send + Sync to ensure that we cannot
// construct a SharedMut without the underlying object being thread-safe.
impl<T: Send + Sync + 'static> SharedMut<T> {
    /// Create a new [`SharedMut`], backed by a `RwLock` and poisoning on panic.
    pub fn new(t: T) -> SharedMut<T> {
        Self::new_with_type(t, ImplementationMut::RwLock)
    }

    /// Create a new [`SharedMut`], backed by a `Mutex` and poisoning on panic.
    pub fn new_mutex(t: T) -> SharedMut<T> {
        Self::new_with_type(t, ImplementationMut::Mutex)
    }

    fn new_with_type(t: T, implementation: ImplementationMut) -> Self {
        Self::new_with_type_and_policy(t, implementation, PoisonPolicy::Panic)
    }

    /// Create a new [`SharedMut`], backed by a `RwLock` and optionally poisoning on panic.
    pub fn new_with_policy(t: T, policy: PoisonPolicy) -> Self {
        Self::new_with_type_and_policy(t, ImplementationMut::RwLock, policy)
    }

    /// Create a new [`SharedMut`], backed by a `Mutex` and optionally poisoning on panic.
    pub fn new_mutex_with_policy(t: T, policy: PoisonPolicy) -> Self {
        Self::new_with_type_and_policy(t, ImplementationMut::Mutex, policy)
    }

    fn new_with_type_and_policy(
        t: T,
        implementation: ImplementationMut,
        policy: PoisonPolicy,
    ) -> Self {
        let synchronizer_type = match implementation {
            ImplementationMut::Mutex => SynchronizerType::Mutex,
            ImplementationMut::Rcu => unimplemented!("Use SharedMut::new_rcu instead"),
            ImplementationMut::RwLock => SynchronizerType::RwLock,
        };

        make_shared_rw_value::<T>(SharedImpl::Value(SynchronizerUnsized::new(
            LockMetadata::poison_policy(policy),
            synchronizer_type,
            t,
        )))
    }
}

impl<T: Send + Sync + Clone + 'static> SharedMut<T> {
    /// Creates a [`SharedMut`] that updates only when the write lock is dropped. This style of synchronization trades off expensive write operations (one clone, plus
    /// the final memory copy on commit) for extremely short lock times. In addition, a steady volume of writes with a steady volume of reads could result in large
    /// memory usage as multiple copies of the object may be kept in memory.
    ///
    /// Neither readers nor writers hold long-term locks on the underlying data, making the contention on this structure much lower than other styles.
    pub fn new_rcu(t: T) -> SharedMut<T> {
        Self::new_cloneable_with_type_and_policy(t, ImplementationMut::Rcu, PoisonPolicy::Ignore)
    }

    fn new_cloneable_with_type_and_policy(
        t: T,
        implementation: ImplementationMut,
        policy: PoisonPolicy,
    ) -> Self {
        let synchronizer_type = match implementation {
            ImplementationMut::Mutex => SynchronizerType::Mutex,
            ImplementationMut::Rcu => SynchronizerType::Rcu,
            ImplementationMut::RwLock => SynchronizerType::RwLock,
        };

        make_shared_rw_value::<T>(SharedImpl::Value(SynchronizerUnsized::new_cloneable(
            LockMetadata::poison_policy(policy),
            synchronizer_type,
            t,
        )))
    }
}

impl<T> SharedMut<T> {
    /// Attempt to unwrap this synchronized object if we are the only holder of its value.
    pub fn try_unwrap(self) -> Result<T, Self> {
        match self.inner_impl.try_unwrap() {
            Ok(x) => Ok(x),
            Err(inner_impl) => Err(Self { inner_impl }),
        }
    }
}

impl<T: ?Sized> SharedMut<T> {
    pub fn project<P: ?Sized + 'static, I: Into<ProjectorRW<T, P>>>(
        &self,
        projector: I,
    ) -> SharedMut<P>
    where
        T: 'static,
    {
        let projectable = Arc::new((self.clone(), projector.into()));
        make_shared_rw_projection(SharedImpl::Projection(projectable))
    }

    pub fn project_fn<
        P: ?Sized + 'static,
        RO: (Fn(&T) -> &P) + Send + Sync + 'static,
        RW: (Fn(&mut T) -> &mut P) + Send + Sync + 'static,
    >(
        &self,
        ro: RO,
        rw: RW,
    ) -> SharedMut<P>
    where
        T: 'static,
    {
        let projectable = Arc::new((self.clone(), ProjectorRW::new(ro, rw)));
        make_shared_rw_projection(SharedImpl::Projection(projectable))
    }

    /// Transmutate this [`SharedMut`] into a [`Shared`]. The underlying lock stays the same.
    pub fn shared_copy(&self) -> Shared<T> {
        Shared::from(self.inner_impl.clone())
    }

    /// Consume and transmutate this [`SharedMut`] into a [`Shared`]. The underlying lock may be optimized if
    /// there are no other outstanding writeable references.
    pub fn shared(self) -> Shared<T> {
        Shared::from(self.inner_impl)
    }

    /// Get a read lock for this [`SharedMut`].
    pub fn read(&self) -> SharedReadLock<T> {
        self.inner_impl.lock_read()
    }

    /// Try to get a read lock for this [`SharedMut`], or return [`None`] if we couldn't.
    pub fn try_read(&self) -> Option<SharedReadLock<T>> {
        self.inner_impl.try_lock_read()
    }

    #[allow(unused)]
    pub(crate) fn read_owned(&self) -> SharedReadLockOwned<T> {
        self.inner_impl.lock_read_owned()
    }

    /// Get a write lock for this [`SharedMut`].
    pub fn write(&self) -> SharedWriteLock<T> {
        self.inner_impl.lock_write()
    }

    /// Try to get a write lock for this [`SharedMut`], or return [`None`] if we couldn't.
    pub fn try_write(&self) -> Option<SharedWriteLock<T>> {
        self.inner_impl.try_lock_write()
    }

    /// Atomically set the value of this [`SharedMut`] to a new value.
    pub fn set(&self, t: T)
    where
        T: Sized,
    {
        *self.write() = t;
    }

    /// Cast this [`SharedMut`] to a new type.
    ///
    /// See [`Castable`] for more information.
    pub fn cast<U>(&self) -> SharedMut<U>
    where
        T: Castable<U> + 'static,
        U: ?Sized + 'static,
    {
        let projector = crate::ProjectorRW {
            ro: Box::new(()),
            rw: Box::new(()),
        };
        let projectable = Arc::new((self.clone(), projector));
        make_shared_rw_projection(SharedImpl::Projection(projectable))
    }

    /// Attempt to unwrap this synchronized object if we are the only holder of its value.
    ///
    /// This is the unsized counterpart of [`SharedMut::try_unwrap`]: it only succeeds for
    /// containers constructed via [`SharedMut::from_box`].
    pub fn try_unwrap_box(self) -> Result<Box<T>, Self> {
        match self.inner_impl.try_unwrap_box() {
            Ok(x) => Ok(x),
            Err(inner_impl) => Err(Self { inner_impl }),
        }
    }


    #[allow(unused)]
    pub(crate) fn write_owned(&self) -> SharedWriteLockOwned<T> {
        self.inner_impl.lock_write_owned()
    }

    #[cfg(feature = "async_experimental")]
    pub async fn read_async(&self, spawner: &crate::Spawner) -> SharedReadLock<T>
    where
        T: 'static,
    {
        if let Some(lock) = self.try_read() {
            return lock;
        }
        let lock = spawner
            .spawn_blocking_map(self.clone(), |lock| lock.read_owned())
            .await;
        return unsafe { lock.unsafe_reattach(&self.inner_impl) };
    }

    #[cfg(feature = "async_experimental")]
    pub async fn write_async(&self, spawner: &crate::Spawner) -> SharedWriteLock<T>
    where
        T: 'static,
    {
        if let Some(lock) = self.try_write() {
            return lock;
        }
        let lock = spawner
            .spawn_blocking_map(self.clone(), |lock| lock.write_owned())
            .await;
        return unsafe { lock.unsafe_reattach(&self.inner_impl) };
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::{project, project_cast, PoisonPolicy};
    use paste::paste;

    #[test]
    pub fn test_shared_rw() {
        let shared = SharedMut::new(1);
        *shared.write() += 1;
        assert_eq!(shared.read(), 2);
    }

    #[test]
    pub fn test_shared_rw_mutex() {
        let shared = SharedMut::new_mutex(1);
        *shared.write() += 1;
        assert_eq!(shared.read(), 2);
    }

    #[test]
    pub fn test_shared_rw_projection() {
        let shared = SharedMut::new((1, 1));
        let shared_1 = shared.project_fn(|x| &x.0, |x| &mut (x.0));
        let shared_2 = shared.project_fn(|x| &x.1, |x| &mut (x.1));

        *shared_1.write() += 1;
        *shared_2.write() += 10;

        assert_eq!(shared.read(), (2, 11));
    }

    #[test]
    pub fn test_nested_rw_projection() {
        let shared = SharedMut::new((1, (2, (3, 4))));

        let projection = project!(x: (i32, (i32, (i32, i32))), x.1);
        let shared2 = shared.project(projection);
        let projection2 = project!(x: (i32, (i32, i32)), x.1 .1);
        let shared3 = shared2.project(projection2);
        *shared3.write() += 10;
        (shared2.write().0) += 100;
        _ = shared2.try_read().expect("Should be able to read");
        _ = shared2.try_write().expect("Should be able to write");
        _ = shared3.try_read().expect("Should be able to read");
        _ = shared3.try_write().expect("Should be able to write");

        assert_eq!(shared.read(), (1, (102, (3, 14))));
    }

    #[test]
    pub fn test_unsized_rw() {
        let shared =
            SharedMut::new("123".to_owned()).project(project_cast!(x: String => dyn AsRef<str>));
        assert_eq!(shared.read().as_ref(), "123");

        let boxed = Box::new([1, 2, 3]) as Box<[i32]>;
        let shared: SharedMut<[i32]> = SharedMut::from_box(boxed);
        assert_eq!(shared.read()[0], 1);
        shared.write()[0] += 10;
        assert_eq!(shared.read()[0], 11);
    }

    #[test]
    pub fn test_unsized_slice_cast() {
        let shared: SharedMut<[i32]> = SharedMut::new([1, 2, 3]).cast();
        assert_eq!(shared.read().as_ref(), [1, 2, 3]);
    }

    #[test]
    pub fn test_unsized_rw_cast() {
        let shared: SharedMut<dyn AsRef<str>> = SharedMut::new("123".to_owned()).cast();
        assert_eq!(shared.read().as_ref(), "123");
    }

    #[test]
    pub fn test_unwrap() {
        let shared = SharedMut::new(1);
        let res = shared.try_unwrap().expect("Expected to unwrap");
        assert_eq!(res, 1);

        let shared = SharedMut::new(1);
        let shared2 = shared.clone();
        // We can't unwrap with multiple references
        assert!(matches!(shared.try_unwrap(), Err(_)));
        // We can now unwrap
        assert!(matches!(shared2.try_unwrap(), Ok(_)));
    }

    #[test]
    pub fn test_clone_commit() {
        let shared = SharedMut::new_rcu((1, 2));
        let read = shared.read();

        let mut write = shared.write();
        assert_eq!(write.0, 1);
        write.0 += 10;
        assert_eq!(write.0, 11);

        assert_eq!(read.0, 1);
        assert_eq!(shared.read().0, 1);
        drop(write);
        assert_eq!(read.0, 1);
        assert_eq!(shared.read().0, 11);
    }

    macro_rules! test_poison_policy {
        ($name:ident , $imp:ident , Ignore) => {
            paste! {
                #[test]
                pub fn [< $name _read >]() {
                    test_poison_policy!(= read, $imp, Ignore);
                }

                #[test]
                pub fn [< $name _write >]() {
                    test_poison_policy!(= write, $imp, Ignore);
                }
            }
        };
        ($name:ident , $imp:ident , Panic) => {
            paste! {
                #[test]
                #[should_panic]
                pub fn [< $name _read >]() {
                    test_poison_policy!(= read, $imp, Panic);
                }

                #[test]
                #[should_panic]
                pub fn [< $name _write >]() {
                    test_poison_policy!(= write, $imp, Panic);
                }
            }
        };
        (= $dir:ident, $imp:ident , $policy:ident) => {
            let shared = SharedMut::new_cloneable_with_type_and_policy(
                1_usize,
                ImplementationMut::$imp,
                PoisonPolicy::$policy,
            );
            let res = std::panic::catch_unwind(|| {
                let _lock = shared.$dir();
                panic!("This is a real panic, expect a backtrace!");
            });
            assert!(res.is_err());
            assert_eq!(shared.$dir(), 1);
        };
    }

    test_poison_policy!(test_poison_policy_mutex_ignore, Mutex, Ignore);
    test_poison_policy!(test_poison_policy_rwlock_ignore, RwLock, Ignore);
    test_poison_policy!(test_poison_policy_rcu_ignore, Rcu, Ignore);
    test_poison_policy!(test_poison_policy_mutex_panic, Mutex, Panic);
    test_poison_policy!(test_poison_policy_rwlock_panic, RwLock, Panic);
    test_poison_policy!(test_poison_policy_rcu_panic, Rcu, Panic);

    #[test]
    pub fn test_try_lock_mutex() {
        let shared = SharedMut::new_mutex(1);
        let read = shared.try_read().expect("Should get us a lock");
        assert!(shared.try_read().is_none());
        assert!(shared.try_write().is_none());
        drop(read);
    }

    #[test]
    pub fn test_try_lock_rwlock() {
        let shared = SharedMut::new(1);
        let read = shared.try_read().expect("Should get us a lock");
        assert!(shared.try_read().is_some());
        assert!(shared.try_write().is_none());
        drop(read);
        assert!(shared.try_write().is_some());
    }

    /// Test that RCU locks always succeed.
    #[test]
    pub fn test_try_lock_rcu() {
        let shared = SharedMut::new_rcu(1);
        let read = shared.try_read().expect("Should get us a lock");
        let write = shared.try_write().expect("Should get us a lock");
        assert!(shared.try_read().is_some());
        assert!(shared.try_write().is_some());
        drop(read);
        drop(write);
    }

    #[cfg(feature = "serde")]
    #[test]
    pub fn test_serde() {
        fn serialize<S: serde::Serialize>(_: S) {}
        let shared = SharedMut::new((1, 2, 3));
        serialize(shared);
    }

    #[test]
    pub fn test_rcu_and_projection() {
        let shared = SharedMut::new_rcu((1, 2, 3));
        let shared2 = shared.project(project!(x: (i32, i32, i32), x.0));
        assert_eq!(shared2.read(), 1);
        *shared2.write() = 10;
        assert_eq!(shared.read(), (10, 2, 3));
        assert_eq!(shared2.read(), 10);
    }
}