result_like/lib.rs
1//! # OptionLike and ResultLike
2//!
3//! Define your own Option-like and Result-like enum types.
4//! Avoid reimplementing the entire APIs 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
70#![no_std]
71extern crate alloc;
72
73extern crate result_like_derive;
74
75pub use result_like_derive::*;
76
77pub trait BoolLike
78where
79 Self: Sized,
80{
81}
82
83pub trait OptionLike
84where
85 Self: Sized,
86{
87 type SomeType;
88
89 // fn from_value(value: Self::SomeType) -> Self;
90 // fn from_option(option: Option<Self::SomeType>) -> Self;
91 // fn into_option(self) -> Option<Self::SomeType>;
92 // fn as_option(&self) -> Option<&Self::SomeType>;
93 // fn as_option_mut(&mut self) -> Option<&mut Self::SomeType>;
94 // fn get_or_insert_with<_Function: FnOnce() -> Self::SomeType>(
95 // &mut self,
96 // f: _Function,
97 // ) -> &mut Self::SomeType;
98
99 // #[inline]
100 // fn expect(self, msg: &str) -> Self::SomeType where {
101 // self.into_option().expect(msg)
102 // }
103
104 // #[inline]
105 // fn unwrap(self) -> Self::SomeType {
106 // self.into_option().unwrap()
107 // }
108
109 // #[inline]
110 // fn unwrap_or(self, default: Self::SomeType) -> Self::SomeType {
111 // self.into_option().unwrap_or(default)
112 // }
113
114 // #[inline]
115 // fn unwrap_or_else<_Function: FnOnce() -> Self::SomeType>(self, f: _Function) -> Self::SomeType {
116 // self.into_option().unwrap_or_else(f)
117 // }
118
119 // #[inline]
120 // fn ok_or<_Error>(self, err: _Error) -> Result<Self::SomeType, _Error> {
121 // self.into_option().ok_or(err)
122 // }
123
124 // #[inline]
125 // fn ok_or_else<_Error, _Function: FnOnce() -> _Error>(
126 // self,
127 // err: _Function,
128 // ) -> Result<Self::SomeType, _Error> {
129 // self.into_option().ok_or_else(err)
130 // }
131
132 // #[inline]
133 // fn filter<P: FnOnce(&Self::SomeType) -> bool>(self, predicate: P) -> Self {
134 // Self::from_option(self.into_option().filter(predicate))
135 // }
136
137 // #[inline]
138 // fn or(self, optb: Self) -> Self {
139 // Self::from_option(self.into_option().or(optb.into_option()))
140 // }
141
142 // #[inline]
143 // fn or_else<_Function: FnOnce() -> Self>(self, f: _Function) -> Self {
144 // Self::from_option(self.into_option().or_else(|| f().into_option()))
145 // }
146
147 // #[inline]
148 // fn map_or<_Other, _Function: FnOnce(Self::SomeType) -> _Other>(
149 // self,
150 // default: _Other,
151 // f: _Function,
152 // ) -> _Other {
153 // self.into_option().map_or(default, f)
154 // }
155
156 // #[inline]
157 // fn xor(self, optb: Self) -> Self {
158 // Self::from_option(self.into_option().xor(optb.into_option()))
159 // }
160
161 // #[inline]
162 // fn get_or_insert(&mut self, v: Self::SomeType) -> &mut Self::SomeType {
163 // self.get_or_insert_with(|| v)
164 // }
165
166 // #[inline]
167 // fn take(&mut self) -> Self
168 // where
169 // Self: Default,
170 // {
171 // core::mem::take(self)
172 // }
173
174 // #[inline]
175 // fn replace(&mut self, value: Self::SomeType) -> Self {
176 // core::mem::replace(self, Self::from_value(value))
177 // }
178
179 // #[inline]
180 // fn unwrap_or_default(self) -> Self::SomeType
181 // where
182 // Self::SomeType: Default,
183 // {
184 // self.into_option().unwrap_or_default()
185 // }
186}
187
188pub trait ResultLike {
189 type OkType;
190 type ErrType;
191}