|
1 | | -//! This crate provides a [`Cell`](cell::Cell) implementation that works with types whose `Clone` |
2 | | -//! implementations are guaranteed not to mutate the `Cell` content using the `&self` |
3 | | -//! reference. This is enforced with the provided [`PureClone`] trait, which is a subtrait of |
4 | | -//! [`Clone`] (and a logical supertrait of [`Copy`]). It is only implemented for types with |
5 | | -//! compliant `clone` methods. |
| 1 | +//! This crate provides a [`Cell`](cell::Cell) implementation that works with types whose `clone` |
| 2 | +//! methods are guaranteed not to mutate the `Cell` content using the `&self` reference. This is |
| 3 | +//! enforced with the provided [`PureClone`] trait, which is a subtrait of [`Clone`] (and a logical |
| 4 | +//! supertrait of [`Copy`]). It is only implemented for types with a compliant `clone` method. |
6 | 5 | //! |
7 | 6 | //! See the [`cell`](module@cell) module documentation for more information on how to use it. |
8 | 7 | //! |
9 | 8 | //! # Background |
10 | 9 | //! |
11 | | -//! This crate was largely inspired by the Swift programming language's class properties (fields in |
12 | | -//! Rust speak), which have value semantics. In Swift, class types themselves have reference |
13 | | -//! semantics and are shared. But methods on class types are mutating. My observation is that the |
14 | | -//! Swift compiler is able to guarantee memory safety in a single-threaded context because copy |
15 | | -//! constructors are not defined by the user. Intead, the compiler automatically generates ones that |
16 | | -//! simply perform a field-wise clone. |
17 | | -//! |
18 | | -//! In Rust, to enable interiorly mutating methods on a `struct` stored in an [`Rc`](alloc::rc::Rc) |
19 | | -//! without the overhead of a [`RefCell`](core::cell::RefCell), we can wrap each of the fields in a |
20 | | -//! [`core::cell::Cell`]. But its [`get`](core::cell::Cell::get) method is only implemented for |
21 | | -//! types that are `Copy`. This is because if the `clone` method obtains a reference to the `Cell`'s |
22 | | -//! interior, it may be able to mutate its state. This can cause undefined behavior, as demonstrated |
23 | | -//! in this [example]. |
| 10 | +//! To enable interiorly mutating methods on a type stored in an [`Rc`](alloc::rc::Rc) without the |
| 11 | +//! overhead of a [`RefCell`](core::cell::RefCell), we can wrap each of its fields in a |
| 12 | +//! [`core::cell::Cell`]. But `Cell`'s [`get`](core::cell::Cell::get) method is only implemented for |
| 13 | +//! types that are `Copy`. This is because if the `clone` method obtains a reference to the |
| 14 | +//! containing `Cell`, it may be able to mutate its state. This can cause undefined behavior, as |
| 15 | +//! demonstrated in this [example]. |
24 | 16 | //! |
25 | 17 | //! By restricting ourselves to a checked subset of `Clone` implementations that do not exploit |
26 | 18 | //! interior mutability to mutate the `Cell` content, it becomes possible to provide a `Cell` with a |
27 | | -//! `get` method that does not require `Copy` types. |
| 19 | +//! `get` method that does not require the type to be `Copy`. |
28 | 20 | //! |
29 | 21 | //! See the documentation for [`PureClone`] for a list of implemented types and the [`clone`] module |
30 | 22 | //! documentation for more details. |
|
43 | 35 | //! ## Interaction with specialization |
44 | 36 | //! |
45 | 37 | //! The [`PureClone`](derive@clone::PureClone) proc macro generates: |
46 | | -//! 1. A non-`default` `Clone` impl with trait bounds that ensure any fields with generics are also |
47 | | -//! `Clone`; and |
| 38 | +//! 1. A non-`default` `Clone` impl with trait bounds that ensure any fields with generic parameters |
| 39 | +//! are also `Clone`; and |
48 | 40 | //! 1. A `PureClone` impl that only compiles if all fields are also `PureClone`. |
49 | 41 | //! |
50 | 42 | //! Item 1 is non-`default` and hence cannot be further specialized. |
|
0 commit comments