result_like/
lib.rs

1//! # OptionLike and ResultLike
2//!
3//! Define your own Option-like and Result-like enum types.
4//! Avoid to reimplement everything of [std::option::Option] and [std::result::Result] for your own enums.
5//!
6//! Option example
7//! ```rust
8//! use result_like::OptionLike;
9//!
10//! // Simple case with single argument name to use Some and None
11//! #[derive(OptionLike, Clone, Copy)]
12//! enum MyOption {
13//!     Some(u32),
14//!     None
15//! }
16//!
17//! let v = MyOption::Some(1);
18//! // every option utilities are possible including unwrap, map, and, or etc.
19//! assert_eq!(v.unwrap(), 1);
20//!
21//! // convertable to option
22//! let opt = v.into_option();
23//! assert_eq!(opt, Some(1));
24//! ```
25//!
26//! Result example in same way
27//! ```rust
28//! use result_like::ResultLike;
29//!
30//! #[derive(ResultLike)]
31//! enum Trial<T, E> {
32//!     Success(T),
33//!     Failure(E),
34//! }
35//! ```
36//!
37//! # BoolLike
38//!
39//! BoolLike is comparably simpler than OptionLike and ResultLike.
40//!
41//! ```rust
42//! use result_like::BoolLike;
43//!
44//! #[derive(BoolLike, Clone, Copy, Debug, PartialEq, Eq)]
45//! enum MyBool {
46//!     Enabled,
47//!     Disabled,
48//! }
49//!
50//! let v = MyBool::Enabled;
51//! assert!(v.to_bool());
52//! assert!(!MyBool::Disabled.to_bool());
53//! assert_eq!(MyBool::from_bool(true), MyBool::Enabled);
54//! assert_eq!(v.then(|| 1), Some(1));
55//! assert_eq!(v.then_some(1), Some(1));
56//!
57//! if MyBool::Enabled.into() {
58//!     // bool-like usage
59//! }
60//!
61//! #[derive(BoolLike)]
62//! enum ValuedBool {
63//!     Something = 50,
64//!     Nothing = 10,
65//! }
66//! assert!(ValuedBool::Something.to_bool());
67//! assert!(ValuedBool::Something as u8 == 50);
68//! ```
69
70extern crate result_like_derive;
71
72pub use result_like_derive::*;
73
74pub trait BoolLike
75where
76    Self: Sized,
77{
78}
79
80pub trait OptionLike
81where
82    Self: Sized,
83{
84    type SomeType;
85
86    // fn from_value(value: Self::SomeType) -> Self;
87    // fn from_option(option: Option<Self::SomeType>) -> Self;
88    // fn into_option(self) -> Option<Self::SomeType>;
89    // fn as_option(&self) -> Option<&Self::SomeType>;
90    // fn as_option_mut(&mut self) -> Option<&mut Self::SomeType>;
91    // fn get_or_insert_with<_Function: FnOnce() -> Self::SomeType>(
92    //     &mut self,
93    //     f: _Function,
94    // ) -> &mut Self::SomeType;
95
96    // #[inline]
97    // fn expect(self, msg: &str) -> Self::SomeType where {
98    //     self.into_option().expect(msg)
99    // }
100
101    // #[inline]
102    // fn unwrap(self) -> Self::SomeType {
103    //     self.into_option().unwrap()
104    // }
105
106    // #[inline]
107    // fn unwrap_or(self, default: Self::SomeType) -> Self::SomeType {
108    //     self.into_option().unwrap_or(default)
109    // }
110
111    // #[inline]
112    // fn unwrap_or_else<_Function: FnOnce() -> Self::SomeType>(self, f: _Function) -> Self::SomeType {
113    //     self.into_option().unwrap_or_else(f)
114    // }
115
116    // #[inline]
117    // fn ok_or<_Error>(self, err: _Error) -> Result<Self::SomeType, _Error> {
118    //     self.into_option().ok_or(err)
119    // }
120
121    // #[inline]
122    // fn ok_or_else<_Error, _Function: FnOnce() -> _Error>(
123    //     self,
124    //     err: _Function,
125    // ) -> Result<Self::SomeType, _Error> {
126    //     self.into_option().ok_or_else(err)
127    // }
128
129    // #[inline]
130    // fn filter<P: FnOnce(&Self::SomeType) -> bool>(self, predicate: P) -> Self {
131    //     Self::from_option(self.into_option().filter(predicate))
132    // }
133
134    // #[inline]
135    // fn or(self, optb: Self) -> Self {
136    //     Self::from_option(self.into_option().or(optb.into_option()))
137    // }
138
139    // #[inline]
140    // fn or_else<_Function: FnOnce() -> Self>(self, f: _Function) -> Self {
141    //     Self::from_option(self.into_option().or_else(|| f().into_option()))
142    // }
143
144    // #[inline]
145    // fn map_or<_Other, _Function: FnOnce(Self::SomeType) -> _Other>(
146    //     self,
147    //     default: _Other,
148    //     f: _Function,
149    // ) -> _Other {
150    //     self.into_option().map_or(default, f)
151    // }
152
153    // #[inline]
154    // fn xor(self, optb: Self) -> Self {
155    //     Self::from_option(self.into_option().xor(optb.into_option()))
156    // }
157
158    // #[inline]
159    // fn get_or_insert(&mut self, v: Self::SomeType) -> &mut Self::SomeType {
160    //     self.get_or_insert_with(|| v)
161    // }
162
163    // #[inline]
164    // fn take(&mut self) -> Self
165    // where
166    //     Self: Default,
167    // {
168    //     std::mem::take(self)
169    // }
170
171    // #[inline]
172    // fn replace(&mut self, value: Self::SomeType) -> Self {
173    //     std::mem::replace(self, Self::from_value(value))
174    // }
175
176    // #[inline]
177    // fn unwrap_or_default(self) -> Self::SomeType
178    // where
179    //     Self::SomeType: Default,
180    // {
181    //     self.into_option().unwrap_or_default()
182    // }
183}
184
185pub trait ResultLike {
186    type OkType;
187    type ErrType;
188}