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
//! Provides the [`PureClone`] trait, which is a restrictive form of `Clone` that does not mutate
//! the containing [`Cell`](crate::cell::Cell).
//!
//! Conceptually, the relationship between [`Copy`], [`Clone`], and `PureClone` can be thought of as
//! follows:
//! ```text
//! Copy: PureClone: Clone
//! ```
//!
//! `PureClone` is `unsafe` because the `clone` implementation must not mutate the content of `Cell`
//! through the `&self` reference it gets with interior mutability. See this [Stack Overflow answer]
//! and this [Rust forum thread] for details.
//!
//! When this [`crate`] is built with the `"derive"` feature, the [`PureClone`](derive@PureClone)
//! proc macro can be used to derive `PureClone` for user types.
//!
//! [Rust forum thread]:
//! https://users.rust-lang.org/t/why-does-cell-require-copy-instead-of-clone/5769/3
//! [Stack Overflow answer]:
//! https://stackoverflow.com/questions/39667868/why-can-cell-in-rust-only-be-used-for-copy-and-not-clone-types
/// A derive macro that generates impls of the traits [`PureClone`] and [`Clone`].
///
/// See the [crate#soundness] doc on why this macro also generates a `Clone` impl.
///
/// # Examples
///
/// ```
/// use std::rc::Rc;
/// use std::sync::Arc;
/// use clone_cell::{cell::Cell, clone::PureClone};
///
/// // Note: This also generates a `Clone` impl.
/// #[derive(PureClone)]
/// struct Foo<T> {
/// p: Rc<T>, // `Rc<T>` is always `PureClone`.
/// ap: Arc<T>, // `Arc<T>` is always `PureClone`.
/// t: Option<T>, // `Option<T>` is `PureClone` if `T` is.
/// x: i32, // `i32` is `PureClone`.
/// }
///
/// let p = Rc::new(-42);
/// let ap = Arc::new(-42);
/// let f = Cell::new(Foo {
/// p: p.clone(),
/// ap: ap.clone(),
/// t: Some(0),
/// x: 0,
/// });
///
/// f.set(Foo {
/// p,
/// ap,
/// t: Some(42),
/// x: 21,
/// });
///
/// assert_eq!(*f.get().p, -42);
/// assert_eq!(*f.get().ap, -42);
/// assert_eq!(f.get().t, Some(42));
/// assert_eq!(f.get().x, 21);
/// ```
pub use cratePureClone;
/// The `PureClone` trait, which is a subtrait of [`Clone`].
///
/// See the [module](self) documentation for more information.
pub unsafe
/// Implementations for types that are known to have compliant `clone` implementations.
)*
}
}
unsafe
impl_pure_clone!
impl_pure_clone_rc!
impl_pure_clone_generic!
impl_pure_clone_tuples!
}