scsys_core/state/impls/
impl_wrapper.rs

1/*
2    Appellation: impl_state <module>
3    Contrib: @FL03
4*/
5use crate::state::{RawState, State};
6use core::mem::MaybeUninit;
7
8impl<Q> State<&Q>
9where
10    Q: RawState,
11{
12    /// returns a new state with a cloned inner value
13    pub fn cloned(&self) -> State<Q>
14    where
15        Q: Clone,
16    {
17        State(self.0.clone())
18    }
19    /// returns a new state with the inner value copied
20    pub fn copied(&self) -> State<Q>
21    where
22        Q: Copy,
23    {
24        State(*self.0)
25    }
26}
27
28impl<Q> State<&mut Q>
29where
30    Q: RawState,
31{
32    /// returns a new state with a cloned inner value
33    pub fn cloned(&self) -> State<Q>
34    where
35        Q: Clone,
36    {
37        State(self.0.clone())
38    }
39    /// returns a new state with the inner value copied
40    pub fn copied(&self) -> State<Q>
41    where
42        Q: Copy,
43    {
44        State(*self.0)
45    }
46}
47
48impl<Q> State<*const Q> where Q: RawState {}
49
50impl<Q> State<*mut Q> where Q: RawState {}
51
52impl<Q> State<MaybeUninit<Q>>
53where
54    Q: RawState,
55{
56    /// returns a new state with an [`uninitialized`](MaybeUninit::uninit) inner state
57    pub fn uninit() -> State<MaybeUninit<Q>> {
58        State(MaybeUninit::uninit())
59    }
60    /// returns a new state with an [`initialized`](MaybeUninit::new) inner state
61    pub fn init(value: Q) -> State<MaybeUninit<Q>> {
62        State(MaybeUninit::new(value))
63    }
64}
65
66impl<Q> State<Option<Q>>
67where
68    Q: RawState,
69{
70    /// returns a new state with a [`None`](Option::None) inner state
71    pub fn none() -> State<Option<Q>> {
72        State(None)
73    }
74    /// returns a new instance with some inner state
75    pub fn some(value: Q) -> State<Option<Q>> {
76        State(Some(value))
77    }
78    /// returns true if the inner state is [`None`](Option::None)
79    pub fn is_none(&self) -> bool {
80        self.get().is_none()
81    }
82    /// returns true if the inner state is [`Some`](Option::Some)
83    pub fn is_some(&self) -> bool {
84        self.get().is_some()
85    }
86}
87
88impl<Q> AsRef<Q> for State<Q>
89where
90    Q: RawState,
91{
92    fn as_ref(&self) -> &Q {
93        self.get()
94    }
95}
96
97impl<Q> AsMut<Q> for State<Q>
98where
99    Q: RawState,
100{
101    fn as_mut(&mut self) -> &mut Q {
102        self.get_mut()
103    }
104}
105
106impl<Q> core::borrow::Borrow<Q> for State<Q>
107where
108    Q: RawState,
109{
110    fn borrow(&self) -> &Q {
111        self.get()
112    }
113}
114
115impl<Q> core::borrow::BorrowMut<Q> for State<Q>
116where
117    Q: RawState,
118{
119    fn borrow_mut(&mut self) -> &mut Q {
120        self.get_mut()
121    }
122}
123
124impl<Q> core::ops::Deref for State<Q>
125where
126    Q: RawState,
127{
128    type Target = Q;
129
130    fn deref(&self) -> &Self::Target {
131        self.get()
132    }
133}
134
135impl<Q> core::ops::DerefMut for State<Q>
136where
137    Q: RawState,
138{
139    fn deref_mut(&mut self) -> &mut Self::Target {
140        self.get_mut()
141    }
142}
143
144impl<Q> From<Q> for State<Q>
145where
146    Q: RawState,
147{
148    fn from(from: Q) -> Self {
149        State(from)
150    }
151}
152
153impl<Q> PartialEq<Q> for State<Q>
154where
155    Q: RawState + PartialEq,
156{
157    fn eq(&self, other: &Q) -> bool {
158        self.get() == other
159    }
160}
161
162impl<'a, Q> PartialEq<&'a Q> for State<Q>
163where
164    Q: RawState + PartialEq,
165{
166    fn eq(&self, other: &&'a Q) -> bool {
167        self.get() == *other
168    }
169}
170
171impl<'a, Q> PartialEq<&'a mut Q> for State<Q>
172where
173    Q: RawState + PartialEq,
174{
175    fn eq(&self, other: &&'a mut Q) -> bool {
176        *self.get() == **other
177    }
178}
179
180impl<Q> PartialOrd<Q> for State<Q>
181where
182    Q: RawState + PartialOrd,
183{
184    fn partial_cmp(&self, other: &Q) -> Option<core::cmp::Ordering> {
185        self.get().partial_cmp(other)
186    }
187}
188
189fmt_wrapper! {
190    State<Q>(Binary, Debug, Display, LowerExp, UpperExp, LowerHex, UpperHex, Octal, Pointer)
191}