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
//! # `mutable-constant`
//!
//! This crate provides a smart pointer that allows
//! mutation on immutable values. In most cases, this
//! will behave like a normal smart pointer like `Box`,
//! but it allows mutation internally when Rust would
//! otherwise forbid it.
//!
//! Doing this is an unsafe operation. You will need
//! to use `unsafe` blocks to use this crate effectively.
//!
//! ## Example
//!
//! ```
//! use mutable_constant::Mc;
//!
//! let mc = Mc::new(42);
//! let mc_ref = mc.as_ref();
//!
//! assert_eq!(*mc_ref, 42);
//!
//! unsafe {
//! *mc.as_defiant_mut() = 43; // This would normally be a compile error
//! }
//!
//! assert_eq!(*mc_ref, 43);
//! ```
/// A smart pointer that allows mutation on immutable values.
///
/// This uses a `*mut T` internally, so most internal operations
/// are unsafe. However, thanks to Rust's borrowing guarantees,
/// many of these operations are safe when used publicly. The
/// only operation that is not safe is `as_defiant_mut`, which
/// creates a mutable reference in defiance of Rust's rules.
;