1#![deny(missing_docs)]
44
45pub trait Boolinator: Sized {
51 fn as_option(self) -> Option<()>;
55
56 fn as_some<T>(self, some: T) -> Option<T>;
60
61 fn as_some_from<T, F>(self, some: F) -> Option<T>
65 where F: FnOnce() -> T;
66
67 fn and_option<T>(self, opt: Option<T>) -> Option<T>;
71
72 fn and_option_from<T, F>(self, opt: F) -> Option<T>
76 where F: FnOnce() -> Option<T>;
77
78 fn as_result<T, E>(self, ok: T, err: E) -> Result<T, E>;
82
83 fn as_result_from<T, E, F, G>(self, ok: F, err: G) -> Result<T, E>
87 where F: FnOnce() -> T, G: FnOnce() -> E;
88
89 fn ok_or<E>(self, err: E) -> Result<(), E>;
93
94 fn ok_or_else<E, G>(self, err: G) -> Result<(), E>
98 where G: FnOnce() -> E;
99
100 fn expect(self, msg: &str);
104}
105
106impl Boolinator for bool {
107 #[inline]
108 fn as_option(self) -> Option<()> {
109 if self { Some(()) } else { None }
110 }
111
112 #[inline]
113 fn as_some<T>(self, some: T) -> Option<T> {
114 if self { Some(some) } else { None }
115 }
116
117 #[inline]
118 fn as_some_from<T, F>(self, some: F) -> Option<T>
119 where F: FnOnce() -> T {
120 if self { Some(some()) } else { None }
121 }
122
123 #[inline]
124 fn and_option<T>(self, opt: Option<T>) -> Option<T> {
125 if self { opt } else { None }
126 }
127
128 #[inline]
129 fn and_option_from<T, F>(self, opt: F) -> Option<T>
130 where F: FnOnce() -> Option<T> {
131 if self { opt() } else { None }
132 }
133
134 #[inline]
135 fn as_result<T, E>(self, ok: T, err: E) -> Result<T, E> {
136 if self { Ok(ok) } else { Err(err) }
137 }
138
139 #[inline]
140 fn as_result_from<T, E, F, G>(self, ok: F, err: G) -> Result<T, E>
141 where F: FnOnce() -> T, G: FnOnce() -> E {
142 if self { Ok(ok()) } else { Err(err()) }
143 }
144
145 #[inline]
146 fn ok_or<E>(self, err: E) -> Result<(), E> {
147 if self { Ok(()) } else { Err(err) }
148 }
149
150 #[inline]
151 fn ok_or_else<E, G>(self, err: G) -> Result<(), E>
152 where G: FnOnce() -> E {
153 if self { Ok(()) } else { Err(err()) }
154 }
155
156 #[inline]
157 fn expect(self, msg: &str) {
158 if self { () } else { panic!("{}", msg) }
159 }
160}
161
162#[cfg(test)]
166mod tests {
167 use super::Boolinator; #[test]
170 fn test_as_option() {
171 assert_eq!(true.as_option(), Some(()));
173 assert_eq!(false.as_option(), None);
174 }
175
176 #[test]
177 fn test_as_some() {
178 let love = true;
180 let everybody = love.as_some("body").expect("needs");
181 assert_eq!(everybody, "body");
182
183 assert_eq!((!love).as_some("money can buy"), None);
184 }
185
186 #[test]
187 fn test_as_some_from() {
188 let mothers = vec![true, false, false, true, false, true];
190 assert!(mothers.into_iter()
191 .map(|e| e.as_some_from(|| Some("em")))
192 .filter(Option::is_some)
193 .count() > 0);
194 }
195
196 #[test]
197 fn test_and_option() {
198 assert_eq!(true.and_option(Some("fries with that")), Some("fries with that"));
200 assert_eq!(false.and_option(Some("fries with that")), None);
201 assert_eq!(true.and_option(None), None::<()>);
202 assert_eq!(false.and_option(None), None::<()>);
203 }
204
205 #[test]
206 fn test_and_option_from() {
207 assert_eq!(true.and_option_from(|| Some("chips too, guv'")), Some("chips too, guv'"));
209 assert_eq!(false.and_option_from(|| Some("chips too, guv'")), None);
210 assert_eq!(true.and_option_from(|| None), None::<()>);
211 assert_eq!(false.and_option_from(|| None), None::<()>);
212 }
213
214 #[test]
215 fn test_as_result() {
216 assert_eq!(true.as_result("now; ", ", what?"), Ok("now; "));
218 assert_eq!(false.as_result("now; ", ", what?"), Err(", what?"));
219 }
220
221 #[test]
222 fn test_as_result_from() {
223 assert_eq!(true.as_result_from(|| "four space indent", || "anything else"), Ok("four space indent"));
225 assert_eq!(false.as_result_from(|| "four space indent", || "anything else"), Err("anything else"));
226 }
227
228 #[test]
229 fn test_ok_or() {
230 let mut annie = true;
232 assert_eq!(annie.ok_or("hit back"), Ok(()));
233 annie = false;
234 assert_eq!(annie.ok_or("hit back"), Err("hit back"));
235 }
236
237 #[test]
238 fn test_ok_or_else() {
239 let mut annie = true;
241 assert_eq!(annie.ok_or_else(|| "hit back"), Ok(()));
242 annie = false;
243 assert_eq!(annie.ok_or_else(|| "hit back"), Err("hit back"));
244 }
245
246 const DREAMS: &'static str = "love and financial security";
247
248 #[test]
249 fn test_expect() {
250 true.expect(DREAMS);
252 }
253
254 #[test]
255 #[should_panic]
256 fn test_expect_reality() {
257 false.expect(DREAMS);
259 }
260}