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
//! This crate provides a container for a value, `DirtyStatic`, which
//! allows mutation in debug mode (via `UnsafeCell`), but not in
//! release mode.
//!
//! This allows you to tweak data while testing an application,
//! without having that data be mutable when the application is
//! released.
//!
//! There are also two features available:
//!
//! 1. `force-dynamic` which allows replacing the value of a
//!     `DirtyStatic` even in release mode.
//! 2. `force-static` which disallows replacing the value of a
//!     `DirtyStatic` even in debug mode.

#[cfg(all(feature = "force-static", feature = "force-dynamic"))]
compile_error!("dirty_static: Cannot enable both the force-static and force-dynamic features.");

pub use internal::DirtyStatic;

#[cfg(any(
    feature = "force-dynamic",
    all(not(feature = "force-static"), debug_assertions)
))]
mod internal {
    use std::cell::UnsafeCell;
    use std::ops::Deref;

    /// A container for a value which allows interior mutation
    /// only in debug mode. (Or when enabled via `force-dynamic`
    /// feature.)
    pub struct DirtyStatic<T>(UnsafeCell<T>);
    unsafe impl<T> Sync for DirtyStatic<T> where T: Sync {}

    impl<T> Deref for DirtyStatic<T> {
        type Target = T;

        fn deref(&self) -> &Self::Target {
            let ptr = self.0.get();
            unsafe { &*ptr }
        }
    }

    impl<T> DirtyStatic<T> {
        /// Create a new DirtyStatic with the given interior value.
        ///
        /// # Examples
        ///
        /// ```rust
        /// use dirty_static::DirtyStatic;
        ///
        /// let c = DirtyStatic::new(100);
        /// assert_eq!(*c, 100);
        /// ```
        pub const fn new(t: T) -> Self {
            DirtyStatic(UnsafeCell::new(t))
        }

        /// Replace the interior value of the DirtyStatic. Note that
        /// this will do nothing unless running in debug mode, or
        /// enabling the `force-dynamic` feature.
        ///
        /// # Examples
        ///
        /// ```rust,no_run
        /// // In debug mode
        /// use dirty_static::DirtyStatic;
        ///
        /// let c = DirtyStatic::new(100);
        /// unsafe {
        ///     c.replace(200);
        /// }
        ///
        /// assert_eq!(*c, 200);
        /// ```
        ///
        /// ```rust,no_run
        /// // In release mode
        /// use dirty_static::DirtyStatic;
        ///
        /// let c = DirtyStatic::new(100);
        /// unsafe {
        ///     c.replace(200);
        /// }
        ///
        /// assert_eq!(*c, 100);
        /// ```
        pub unsafe fn replace(&self, t: T) {
            let ptr = self.0.get();
            *ptr = t
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn create_value() {
            let text = "Hello".to_string();
            let c = DirtyStatic::new(text);

            assert_eq!(&*c, "Hello");
        }

        #[test]
        fn refresh_value() {
            let text = "Hello".to_string();
            let c = DirtyStatic::new(text);

            unsafe { c.replace("Replacement value".to_string()) };
            assert_eq!(&*c, "Replacement value");
        }
    }
}

#[cfg(any(
    feature = "force-static",
    all(not(feature = "force-dynamic"), not(debug_assertions))
))]
mod internal {
    use std::ops::Deref;

    pub struct DirtyStatic<T>(T);

    impl<T> Deref for DirtyStatic<T> {
        type Target = T;

        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    impl<T> DirtyStatic<T> {
        pub const fn new(t: T) -> Self {
            DirtyStatic(t)
        }

        pub unsafe fn replace(&self, _t: T) {
            eprintln!("WARNING: Can't replace in release mode!");
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn create_value() {
            let text = "Hello".to_string();
            let c = DirtyStatic::new(text);

            assert_eq!(&*c, "Hello");
        }

        #[test]
        fn refresh_value_does_nothing() {
            let text = "Hello".to_string();
            let c = DirtyStatic::new(text);

            unsafe { c.replace("Replacement value".to_string()) };
            assert_eq!(&*c, "Hello");
        }
    }
}

#[cfg(test)]
mod feature_tests {
    use super::DirtyStatic;

    fn _assert_static() {
        let c = DirtyStatic::new(10);
        unsafe { c.replace(20) };
        assert_eq!(*c, 10);
    }

    fn _assert_dynamic() {
        let c = DirtyStatic::new(10);
        unsafe { c.replace(20) };
        assert_eq!(*c, 20);
    }

    #[test]
    #[cfg(all(
        debug_assertions,
        not(any(feature = "force-static", feature = "force-dynamic"))
    ))]
    fn feature_test() {
        _assert_dynamic();
    }

    #[test]
    #[cfg(all(
        not(debug_assertions),
        not(any(feature = "force-static", feature = "force-dynamic"))
    ))]
    fn feature_test() {
        _assert_static();
    }

    #[test]
    #[cfg(all(debug_assertions, feature = "force-static"))]
    fn feature_test() {
        _assert_static();
    }

    #[test]
    #[cfg(all(debug_assertions, feature = "force-dynamic"))]
    fn feature_test() {
        _assert_dynamic();
    }

    #[test]
    #[cfg(all(not(debug_assertions), feature = "force-static"))]
    fn feature_test() {
        _assert_static();
    }

    #[test]
    #[cfg(all(not(debug_assertions), feature = "force-dynamic"))]
    fn feature_test() {
        _assert_dynamic();
    }
}