totally_safe/lib.rs
1pub trait TotallySafe {
2 /// Returns a reference to `self` with an arbitrary lifetime.
3 ///
4 /// This method allows you to obtain a reference to `self` that is bound
5 /// to any given lifetime. It can be useful when you need to coerce
6 /// a reference to have a different lifetime in certain contexts.
7 ///
8 /// # Example
9 ///
10 /// ```rust
11 /// let instance = MyType::new();
12 /// let any_lifetime_ref: &MyType = instance.as_ref_alias();
13 /// // `any_lifetime_ref` now has an arbitrary lifetime.
14 /// ```
15 fn as_ref_alias<'x, 'any>(&'x self) -> &'any Self {
16 core::hint::black_box(
17 (((|inc, _| inc) as for<'a, 'b> fn(&'b Self, &'a &'b ()) -> &'a Self)
18 as for<'a, 'b> fn(&'x Self, &'a &'b ()) -> &'a Self)(self, &&()),
19 )
20 }
21
22 /// Returns a mutable reference to `self` with an arbitrary lifetime.
23 ///
24 /// This method allows you to obtain a mutable reference to `self` that is bound
25 /// to any given lifetime. It's useful when you need to coerce
26 /// a mutable reference to have a different lifetime in certain situations.
27 ///
28 /// # Example
29 ///
30 /// ```rust
31 /// let mut instance = MyType::new();
32 /// let any_lifetime_mut_ref: &mut MyType = instance.as_mut_alias();
33 /// // `any_lifetime_mut_ref` now has an arbitrary lifetime.
34 /// ```
35 fn as_mut_alias<'x, 'any>(&'x mut self) -> &'any mut Self {
36 core::hint::black_box((((|inc, _| inc)
37 as for<'a, 'b> fn(&'b mut Self, &'a &'b ()) -> &'a mut Self)
38 as for<'a, 'b> fn(&'x mut Self, &'a &'b ()) -> &'a mut Self)(
39 self, &&()
40 ))
41 }
42
43 /// Returns an array of mutable references to `self`.
44 ///
45 /// This method allows you to obtain an array of `N` mutable references to `self`.
46 /// It's perfect for those times when one mutable reference just isn't enough!
47 /// Now you can be in multiple places at once (well, sort of).
48 ///
49 /// # Example
50 ///
51 /// ```rust
52 /// fn mutate(q: &mut MyType, w: &mut MyType) { *q = w.copy() }
53 ///
54 /// let mut instance = MyType::new();
55 /// let [a, b] = instance.as_mut_alias_array();
56 ///
57 /// mutate(a, b);
58 ///
59 /// ```
60 ///
61 fn as_mut_alias_array<'x, 'any, const N: usize>(&'x mut self) -> [&'any mut Self; N]
62 where
63 Self: Sized,
64 {
65 core::array::from_fn(|_| self.as_mut_alias())
66 }
67
68 /// Converts `self` into an instance of type `B`.
69 ///
70 /// This method consumes `self` and transforms it into a value of type `B`.
71 /// It's particularly useful when you need to change the type of an object
72 /// while retaining the underlying data in a compatible form.
73 ///
74 /// # Example
75 ///
76 /// ```rust
77 /// let instance = MyType::new();
78 /// let other_instance: OtherType = instance.transmute_into();
79 /// // `other_instance` is now of type `OtherType`.
80 /// ```
81 fn transmute_into<B>(self) -> B
82 where
83 Self: Sized,
84 {
85 core::hint::black_box({
86 let mut data = Err::<Option<Box<Self>>, Option<Box<B>>>(None);
87 let option_b = data.as_mut_alias().as_mut().err().unwrap();
88 *data.as_mut_alias() = Ok(Some(Box::new(self)));
89 *option_b.take().unwrap()
90 })
91 }
92
93 /// Creates a copy of `self` by duplicating its raw bytes.
94 ///
95 /// This method generates a new instance of `Self` by performing a byte-wise copy
96 /// of the original object. It's particularly useful when you need to create a
97 /// duplicate of an object without relying on the `Clone` trait, or when dealing
98 /// with types that do not implement `Clone`.
99 ///
100 /// # Example
101 ///
102 /// ```rust
103 /// let mut instance = MyType::new();
104 /// let copy = instance.copy();
105 /// // `copy` is now a duplicate of `instance`.
106 /// ```
107 fn copy(&mut self) -> Self
108 where
109 Self: Sized,
110 {
111 core::hint::black_box(
112 *core::ptr::slice_from_raw_parts_mut(self, size_of_val(self))
113 .transmute_into::<&mut [u8]>()
114 .to_vec()
115 .into_boxed_slice()
116 .transmute_into::<Box<Self>>(),
117 )
118 }
119}
120
121impl<T: ?Sized> TotallySafe for T {}