orx_self_or/self_or_mut.rs
1use crate::sealed::Sealed;
2
3/// A representation of an instance of a type that is either the instance itself,
4/// or a mutable reference to the instance.
5///
6/// Notice that, either variant of the instance is capable of:
7/// * producing a shared reference through `get_ref` method, and
8/// * a mutable reference through `get_mut` method.
9pub trait SoM<T>: Sealed {
10 /// Returns a reference to self.
11 fn get_ref(&self) -> &T;
12
13 /// Returns a mutable reference to self.
14 fn get_mut(&mut self) -> &mut T;
15}
16
17impl<T> SoM<T> for T {
18 #[inline(always)]
19 fn get_ref(&self) -> &T {
20 self
21 }
22
23 #[inline(always)]
24 fn get_mut(&mut self) -> &mut T {
25 self
26 }
27}
28
29impl<T> SoM<T> for &mut T {
30 #[inline(always)]
31 fn get_ref(&self) -> &T {
32 self
33 }
34
35 #[inline(always)]
36 fn get_mut(&mut self) -> &mut T {
37 self
38 }
39}