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
use crate::;
/// A reader-writer lock that only syncs when the `sync` method is called.
///
/// This is similar to a [`RwLock`], except that writes are not instantly
/// realized by parallel readers. Instead, writes are only realized when the
/// `sync` method is called. This is useful for cases where you want to write to a value, but you don't
/// want to block readers while you do so.
///
/// This type only implements [`Sync`] when `T` is [`Send`]. Syncing and the non-const [`new`](Self::new)
/// method require `T` to implement [`Clone`]. Values are provided through the [`Deref`](std::ops::Deref)
/// and [`DerefMut`](std::ops::DerefMut) implementations on the [`ReadGuard`] and [`Guard`] types.
///
/// # Examples
/// ```rust
/// use no_std_async::SwapLock;
/// # fn main() { pollster::block_on(foo()); }
///
/// async fn foo() {
/// let lock = SwapLock::new(42);
/// let read = lock.read().await;
/// assert_eq!(*read, 42);
///
/// let mut write = lock.write().await;
/// *write += 1;
/// assert_eq!(*write, 43); // All writers will read the new value.
/// assert_eq!(*read, 42); // The read value is not updated until `sync` is called.
///
/// drop(read);
/// drop(write);
///
/// lock.sync().await;
/// let read = lock.read().await;
/// assert_eq!(*read, 43); // The value has now been updated.
/// }
unsafe
unsafe