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