devela/code/result/own/
general.rs

1// devela::code::result::own::general
2//
3//!
4//
5// methods: 1 + 25 + 8 = 34
6
7use crate::{iif, Own};
8
9/* constructors (2) */
10
11impl<S> Own<S, ()> {
12    /// A constructor with the given `state` and an empty value.
13    pub const fn empty(state: S) -> Own<S, ()> {
14        Own::new(state, ())
15    }
16}
17
18/// # General methods.
19impl<S, V> Own<S, V> {
20    /// A constructor with the given `state` and `value`.
21    pub const fn new(state: S, value: V) -> Self {
22        Own { s: state, v: value }
23    }
24
25    /* destructors (3) */
26
27    /// Returns both `state` and `value` as a tuple.
28    #[rustfmt::skip]
29    pub fn sv(self) -> (S, V) {
30        (self.s, self.v)
31    }
32
33    /// Returns shared references to both `state` and `value`.
34    #[rustfmt::skip]
35    pub const fn sv_ref(&self) -> (&S, &V) {
36        (&self.s, &self.v)
37    }
38
39    /// Returns exclusive references to both `state` and `value`.
40    #[rustfmt::skip]
41    pub fn sv_mut(&mut self) -> (&mut S, &mut V) {
42        (&mut self.s, &mut self.v)
43    }
44
45    /* replace (3) */
46
47    /// Replaces the existing `state` with a `new_state`.
48    pub fn s_replace(self, new_state: S) -> Self {
49        Self::new(new_state, self.v)
50    }
51    /// Replaces the `value` with a `new_value`.
52    pub fn v_replace(self, new_value: V) -> Self {
53        Self::new(self.s, new_value)
54    }
55    /// Replaces the existing `state` and `value` with `new_state` and `new_value`, respectively.
56    pub fn sv_replace(self, new_state: S, new_value: V) -> Self {
57        Self::new(new_state, new_value)
58    }
59
60    /* wrap (4) */
61
62    /// Wraps the `state` field into [`Ok`].
63    pub fn s_wrap_ok<E>(self) -> Own<Result<S, E>, V> {
64        Own::new(Ok(self.s), self.v)
65    }
66    /// Wraps the `state` field into [`Some`].
67    pub fn s_wrap_some(self) -> Own<Option<S>, V> {
68        Own::new(Some(self.s), self.v)
69    }
70
71    /// Wraps the `value` field into [`Ok`].
72    pub fn v_wrap_ok<E>(self) -> Own<S, Result<V, E>> {
73        Own::new(self.s, Ok(self.v))
74    }
75    /// Wraps the `value` field into [`Some`].
76    pub fn v_wrap_some(self) -> Own<S, Option<V>> {
77        Own::new(self.s, Some(self.v))
78    }
79
80    /* map (3) */
81
82    /// Applies a mapping function to the state.
83    #[rustfmt::skip]
84    pub fn s_map<T, F: FnOnce(S) -> T>(self, f: F) -> Own<T, V> {
85        Own::new(f(self.s), self.v)
86    }
87
88    /// Applies a mapping function to the value.
89    #[rustfmt::skip]
90    pub fn v_map<W, F: FnOnce(V) -> W>(self, f: F) -> Own<S, W> {
91        Own::new(self.s, f(self.v))
92    }
93
94    /// Applies applies a separate mapping function to the state and value.
95    #[rustfmt::skip]
96    pub fn sv_map<F, G, T, W>(self, sf: F, vf: G) -> Own<T, W>
97    where F: FnOnce(S) -> T, G: FnOnce(V) -> W {
98        Own::new(sf(self.s), vf(self.v))
99    }
100
101    /* equality test (3) */
102
103    /// Returns `true` if the current `state` equals the given `expected` state.
104    #[must_use] #[rustfmt::skip]
105    pub fn s_eq(&self, expected: &S) -> bool where S: PartialEq {
106        self.s == *expected
107    }
108    /// Returns `true` if the current `value` equals the given `expected` value.
109    #[must_use] #[rustfmt::skip]
110    pub fn v_eq(&self, expected: &V) -> bool where V: PartialEq {
111        self.v == *expected
112    }
113    /// Returns `true` if the current `state` and `value` equals the corresponding expected ones.
114    #[must_use] #[rustfmt::skip]
115    pub fn sv_eq(&self, expected_state: &S, expected_value: &V) -> bool
116    where S: PartialEq, V: PartialEq {
117        self.s == *expected_state && self.v == *expected_value
118    }
119
120    /* assert (or, eq, eq_or) (12) */
121
122    /// Asserts the `state` matches the `predicate`, otherwise panics.
123    /// # Panics
124    /// Panics if the predicate returns `false`.
125    #[rustfmt::skip]
126    pub fn s_assert<F: FnOnce(&S) -> bool>(self, predicate: F) -> Self {
127        iif![predicate(&self.s); self; panic![]]
128    }
129    /// Asserts the `state` matches the `predicate`, otherwise panics with `message`.
130    /// # Panics
131    /// Panics if the predicate returns `false`.
132    #[rustfmt::skip]
133    pub fn s_assert_or<F: FnOnce(&S) -> bool>(self, predicate: F, message: &str) -> Self {
134        iif![predicate(&self.s); self; panic!["{}", message]]
135    }
136    /// Asserts the `state` equals `expected` and returns `self`, otherwise panics.
137    /// # Panics
138    /// Panics if the `state` doesn't equal the `expected` state.
139    #[rustfmt::skip]
140    pub fn s_assert_eq(self, expected_state: &S) -> Self where S: PartialEq {
141        iif![self.s == *expected_state; self; panic![]]
142    }
143    /// Asserts the `state` equals `expected` and returns `self`, otherwise panics with `message`.
144    /// # Panics
145    /// Panics if the `state` doesn't equal the `expected` state.
146    #[rustfmt::skip]
147    pub fn s_assert_eq_or(self, expected_state: &S, message: &str) -> Self where S: PartialEq {
148        iif![self.s == *expected_state; self; panic!["{}", message]]
149    }
150
151    /// Asserts the `value` matches the `predicate`, otherwise panics.
152    /// # Panics
153    /// Panics if the predicate returns `false`.
154    #[rustfmt::skip]
155    pub fn v_assert<F: FnOnce(&V) -> bool>(self, predicate: F) -> Self {
156        iif![predicate(&self.v); self; panic![]]
157    }
158    /// Asserts the `value` matches the `predicate`, otherwise panics with `message`.
159    /// # Panics
160    /// Panics if the predicate returns `false`.
161    #[rustfmt::skip]
162    pub fn v_assert_or<F: FnOnce(&V) -> bool>(self, predicate: F, message: &str) -> Self {
163        iif![predicate(&self.v); self; panic!["{}", message]]
164    }
165    /// Asserts the `value` equals `expected` and returns `self`, otherwise panics.
166    /// # Panics
167    /// Panics if the `value` doesn't equal the `expected` value.
168    #[rustfmt::skip]
169    pub fn v_assert_eq(self, expected_value: &V) -> Self where V: PartialEq {
170        iif![self.v == *expected_value; self; panic![]]
171    }
172    /// Asserts the `value` equals `expected` and returns `self`, otherwise panics with `message`.
173    /// # Panics
174    /// Panics if the `value` doesn't equal the `expected` value.
175    #[rustfmt::skip]
176    pub fn v_assert_eq_or(self, expected_value: &V, message: &str) -> Self where V: PartialEq {
177        iif![self.v == *expected_value; self; panic!["{}", message]]
178    }
179
180    /// Asserts both the `state` and `value` matches the corresponding predicates,
181    /// otherwise panics.
182    /// # Panics
183    /// Panics if any predicate returns `false`.
184    #[rustfmt::skip]
185    pub fn sv_assert<F, G>(self, s_predicate: F, v_predicate: G) -> Self
186    where F: FnOnce(&S) -> bool, G: FnOnce(&V) -> bool {
187        iif![s_predicate(&self.s) && v_predicate(&self.v); self; panic![]]
188    }
189    /// Asserts both the `state` and `value` matches the corresponding predicates,
190    /// otherwise panics with `message`.
191    /// # Panics
192    /// Panics if any predicate returns `false`.
193    #[rustfmt::skip]
194    pub fn sv_assert_or<F, G>(self, s_predicate: F, v_predicate: G, message: &str) -> Self
195    where F: FnOnce(&S) -> bool, G: FnOnce(&V) -> bool {
196        iif![s_predicate(&self.s) && v_predicate(&self.v); self; panic!["{}", message]]
197    }
198    /// Asserts the `state` and `value` equals the corresponding expected ones,
199    /// and returns `self`, otherwise panics.
200    /// # Panics
201    /// Panics if either the `state` or the `value` are not the expected ones.
202    #[rustfmt::skip]
203    pub fn sv_assert_eq(self, expected_state: &S, expected_value: &V) -> Self
204    where S: PartialEq, V: PartialEq {
205        iif![self.s == *expected_state && self.v == *expected_value; self; panic![]]
206    }
207    /// Asserts the `state` and `value` equals the corresponding expected ones,
208    /// and returns `self`, otherwise panics with `message`
209    /// # Panics
210    /// Panics if either the `state` or the `value` are not the expected ones.
211    #[rustfmt::skip]
212    pub fn sv_assert_eq_or(self, expected_state: &S, expected_value: &V, message: &str) -> Self
213    where S: PartialEq, V: PartialEq {
214        iif![self.s == *expected_state && self.v == *expected_value; self; panic!["{}", message]]
215    }
216}
217
218/// # Additional *const* methods for when everything is `Copy`.
219impl<S: Copy, V: Copy> Own<S, V> {
220    // (7)
221    /// Transforms itself into a tuple, in compile-time.
222    #[must_use] #[rustfmt::skip]
223    pub const fn sv_const(self) -> (S, V) {
224        (self.s, self.v)
225    }
226
227    /// Replaces the `state` self with a `new_state`, in compile-time.
228    pub const fn s_const_replace(self, s: S) -> Self {
229        Self::new(s, self.v)
230    }
231    /// Replaces the `value` with a `new_value`, in compile-time.
232    pub const fn v_const_replace(self, v: V) -> Self {
233        Self::new(self.s, v)
234    }
235    /// Replaces the `state` self with a `new_state` and the `value` with a `new_value`,
236    /// in compile-time.
237    pub const fn sv_const_replace(self, new_state: S, new_value: V) -> Self {
238        Self::new(new_state, new_value)
239    }
240
241    /// Wraps the `state` field into a [`Result`], in compile-time.
242    pub const fn s_const_wrap_ok<E>(self) -> Own<Result<S, E>, V> {
243        Own::new(Ok(self.s), self.v)
244    }
245    /// Wraps the `state` field into an [`Option`], in compile-time.
246    pub const fn s_const_wrap_some(self) -> Own<Option<S>, V> {
247        Own::new(Some(self.s), self.v)
248    }
249
250    /// Wraps the `value` field into a [`Result`], in compile-time.
251    pub const fn v_const_wrap_ok<E>(self) -> Own<S, Result<V, E>> {
252        Own::new(self.s, Ok(self.v))
253    }
254    /// Wraps the `value` field into an [`Option`], in compile-time.
255    pub const fn v_const_wrap_some(self) -> Own<S, Option<V>> {
256        Own::new(self.s, Some(self.v))
257    }
258}