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
use std::ops::Deref; /// This crate provides the `Final`, struct which guarantees the interior /// immutability of the value that it wraps. This is useful for /// preserving invariants on the fields of structures, whose 'safe' /// mutation would cause undefined behavior. // https://www.reddit.com/r/rust/comments/26jzm5/immutable_struct_members_in_rust/chs3ljz/ /// Wraps a value in the `Final` type, which does not give out mutable /// references. #[derive(Debug)] pub struct Final<T>(T); impl<T> Final<T> { /// Wrap a value `v` in a `Final` #[inline] pub fn new(v: T) -> Final<T> { Final(v) } } impl<T> Deref for Final<T> { type Target = T; fn deref(&self) -> &T { &self.0 } }