ctutils/ct_option.rs
1use crate::{Choice, CtAssign, CtAssignSlice, CtEq, CtEqSlice, CtSelect};
2use core::ops::{Deref, DerefMut};
3
4/// Helper macro for providing behavior like the [`CtOption::map`] combinator that works in
5/// `const fn` contexts.
6///
7/// Requires a provided `$mapper` function to convert from one type to another, e.g.
8///
9/// ```ignore
10/// const fn mapper(value: T) -> U
11/// ```
12#[macro_export]
13macro_rules! map {
14 ($opt:expr, $mapper:path) => {{ $crate::CtOption::new($mapper($opt.to_inner_unchecked()), $opt.is_some()) }};
15}
16
17/// Helper macro for providing behavior like the [`CtOption::unwrap_or`] combinator that works in
18/// `const fn` contexts.
19///
20/// Requires a provided selector function `$select` to perform constant-time selection which takes
21/// two `T` values by reference along with a [`Choice`], returning the first `T` for
22/// [`Choice::FALSE`], and the second for [`Choice::TRUE`], e.g.:
23///
24/// ```ignore
25/// const fn ct_select(a: &T, b: &T, condition: Choice) -> T
26/// ```
27#[macro_export]
28macro_rules! unwrap_or {
29 ($opt:expr, $default:expr, $select:path) => {
30 $select(&$default, $opt.as_inner_unchecked(), $opt.is_some())
31 };
32}
33
34/// Equivalent of [`Option`] but predicated on a [`Choice`] with combinators that allow for
35/// constant-time operations which always perform the same sequence of instructions regardless of
36/// the value of `is_some`.
37///
38/// Unlike [`Option`], [`CtOption`] always contains a value, and will use the contained value when
39/// e.g. evaluating the callbacks of combinator methods, which unlike `core` it does unconditionally
40/// in order to ensure constant-time operation. This approach stands in contrast to the lazy
41/// evaluation similar methods on [`Option`] provide.
42#[derive(Clone, Copy, Debug)]
43pub struct CtOption<T> {
44 value: T,
45 is_some: Choice,
46}
47
48impl<T> CtOption<T> {
49 /// Construct a new [`CtOption`], with a [`Choice`] parameter `is_some` as a stand-in for
50 /// `Some` or `None` enum variants of a typical [`Option`] type.
51 #[inline]
52 #[must_use]
53 pub const fn new(value: T, is_some: Choice) -> CtOption<T> {
54 Self { value, is_some }
55 }
56
57 /// Construct a new [`CtOption`] where `self.is_some()` is [`Choice::TRUE`].
58 #[inline]
59 #[must_use]
60 pub const fn some(value: T) -> CtOption<T> {
61 Self::new(value, Choice::TRUE)
62 }
63
64 /// Construct a new [`CtOption`] with the [`Default`] value, and where `self.is_some()` is
65 /// [`Choice::FALSE`].
66 #[inline]
67 #[must_use]
68 pub fn none() -> CtOption<T>
69 where
70 T: Default,
71 {
72 Self::new(Default::default(), Choice::FALSE)
73 }
74
75 /// Convert from a `&mut CtOption<T>` to `CtOption<&mut T>`.
76 #[inline]
77 #[must_use]
78 pub const fn as_mut(&mut self) -> CtOption<&mut T> {
79 CtOption {
80 value: &mut self.value,
81 is_some: self.is_some,
82 }
83 }
84
85 /// Convert from a `&CtOption<T>` to `CtOption<&T>`.
86 #[inline]
87 #[must_use]
88 pub const fn as_ref(&self) -> CtOption<&T> {
89 CtOption {
90 value: &self.value,
91 is_some: self.is_some,
92 }
93 }
94
95 /// Convert from `CtOption<T>` (or `&CtOption<T>`) to `CtOption<&T::Target>`, for types which
96 /// impl the [`Deref`] trait.
97 #[inline]
98 #[must_use]
99 pub fn as_deref(&self) -> CtOption<&T::Target>
100 where
101 T: Deref,
102 {
103 self.as_ref().map(Deref::deref)
104 }
105
106 /// Convert from `CtOption<T>` (or `&mut CtOption<T>`) to `CtOption<&mut T::Target>`, for types
107 /// which impl the [`DerefMut`] trait.
108 #[inline]
109 #[must_use]
110 pub fn as_deref_mut(&mut self) -> CtOption<&mut T::Target>
111 where
112 T: DerefMut,
113 {
114 self.as_mut().map(DerefMut::deref_mut)
115 }
116
117 /// Return the contained value, consuming the `self` value.
118 ///
119 /// # Panics
120 /// In the event `self.is_some()` is [`Choice::FALSE`], panics with a custom panic message
121 /// provided as the `msg` argument.
122 #[inline]
123 #[must_use]
124 #[track_caller]
125 pub fn expect(self, msg: &str) -> T {
126 assert!(self.is_some().to_bool(), "{}", msg);
127 self.value
128 }
129
130 /// Return the contained value, consuming the `self` value, with `const fn` support.
131 ///
132 /// Relies on a `Copy` bound which implies `!Drop` which is needed to be able to move out of
133 /// `self` in a `const fn` without `feature(const_precise_live_drops)`.
134 ///
135 /// # Panics
136 /// In the event `self.is_some()` is [`Choice::FALSE`], panics with a custom panic message
137 /// provided as the `msg` argument.
138 // TODO(tarcieri): get rid of this when we can make `expect` a `const fn`
139 // (needs `const_precise_live_drops`)
140 #[inline]
141 #[must_use]
142 #[track_caller]
143 pub const fn expect_copied(self, msg: &str) -> T
144 where
145 T: Copy,
146 {
147 *self.expect_ref(msg)
148 }
149
150 /// Borrow the contained value.
151 ///
152 /// # Panics
153 /// In the event `self.is_some()` is [`Choice::FALSE`], panics with a custom panic message
154 /// provided as the `msg` argument.
155 // TODO(tarcieri): get rid of this when we can make `expect` a `const fn`
156 // (needs `const_precise_live_drops`)
157 #[inline]
158 #[must_use]
159 #[track_caller]
160 pub const fn expect_ref(&self, msg: &str) -> &T {
161 // TODO(tarcieri): use `self.is_some().to_bool()` when MSRV is 1.86
162 assert!(self.is_some.to_bool_vartime(), "{}", msg);
163 self.as_inner_unchecked()
164 }
165
166 /// Inserts `value` into the [`CtOption`], then returns a mutable reference to it.
167 ///
168 /// If the option already contains a value, the old value is dropped.
169 pub fn insert(&mut self, value: T) -> &mut T {
170 self.value = value;
171 self.is_some = Choice::TRUE;
172 &mut self.value
173 }
174
175 /// Conditionally inserts `value` into the [`CtOption`] if the given condition holds.
176 pub fn insert_if(&mut self, value: &T, condition: Choice)
177 where
178 T: CtAssign,
179 {
180 self.value.ct_assign(value, condition);
181 self.is_some.ct_assign(&Choice::TRUE, condition);
182 }
183
184 /// Convert the [`CtOption`] wrapper into an [`Option`], depending on whether
185 /// [`CtOption::is_some`] is a truthy or falsy [`Choice`].
186 ///
187 /// This function exists to avoid ending up with ugly, verbose and/or bad handled conversions
188 /// from the [`CtOption`] wraps to an [`Option`] or [`Result`].
189 ///
190 /// It's equivalent to the corresponding [`From`] impl, however this version is friendlier for
191 /// type inference.
192 ///
193 /// <div class="warning">
194 /// <b>Warning: variable-time!</b>
195 ///
196 /// This implementation doesn't intend to be constant-time nor try to protect the leakage of the
197 /// `T` value since the [`Option`] will do it anyway.
198 /// </div>
199 #[inline]
200 pub fn into_option(self) -> Option<T> {
201 if self.is_some.to_bool() {
202 Some(self.value)
203 } else {
204 None
205 }
206 }
207
208 /// Convert the [`CtOption`] wrapper into an [`Option`] in a `const fn`-friendly manner.
209 ///
210 /// This is the equivalent of [`CtOption::into_option`] but is `const fn`-friendly by only
211 /// allowing `Copy` types which are implicitly `!Drop` and don't run into problems with
212 /// `const fn` and destructors.
213 ///
214 /// <div class="warning">
215 /// <b>Warning: variable-time!</b>
216 ///
217 /// This implementation doesn't intend to be constant-time nor try to protect the leakage of the
218 /// `T` value since the [`Option`] will do it anyway.
219 /// </div>
220 #[inline]
221 pub const fn into_option_copied(self) -> Option<T>
222 where
223 T: Copy,
224 {
225 // TODO(tarcieri): use `self.is_some().to_bool()` when MSRV is 1.86
226 if self.is_some.to_bool_vartime() {
227 Some(self.value)
228 } else {
229 None
230 }
231 }
232
233 /// Returns [`Choice::TRUE`] if the option is the equivalent of a `Some`.
234 #[inline]
235 #[must_use]
236 pub const fn is_some(&self) -> Choice {
237 self.is_some
238 }
239
240 /// Returns [`Choice::TRUE`] if the option is the equivalent of a `None`.
241 #[inline]
242 #[must_use]
243 pub const fn is_none(&self) -> Choice {
244 self.is_some.not()
245 }
246
247 /// Returns `optb` if `self.is_some()` is [`Choice::TRUE`], otherwise returns a [`CtOption`]
248 /// where `self.is_some()` is [`Choice::FALSE`].
249 #[inline]
250 #[must_use]
251 pub fn and<U>(self, mut optb: CtOption<U>) -> CtOption<U> {
252 optb.is_some &= self.is_some;
253 optb
254 }
255
256 /// Calls the provided callback with the wrapped inner value, returning the resulting
257 /// [`CtOption`] value in the event that `self.is_some()` is [`Choice::TRUE`], or if not
258 /// returns a [`CtOption`] with `self.is_none()`.
259 ///
260 /// Unlike [`Option`], the provided callback `f` is unconditionally evaluated to ensure
261 /// constant-time operation. This requires evaluating the function with "dummy" value of `T`
262 /// (e.g. if the [`CtOption`] was constructed with a supplied placeholder value and
263 /// [`Choice::FALSE`], the placeholder value will be provided).
264 #[inline]
265 #[must_use]
266 pub fn and_then<U, F>(self, f: F) -> CtOption<U>
267 where
268 F: FnOnce(T) -> CtOption<U>,
269 {
270 let mut ret = f(self.value);
271 ret.is_some &= self.is_some;
272 ret
273 }
274
275 /// Obtain a reference to the inner value without first checking that `self.is_some()` is
276 /// [`Choice::TRUE`].
277 ///
278 /// This method is primarily intended for use in `const fn` scenarios where it's not yet
279 /// possible to use the safe combinator methods, and returns a reference to avoid issues with
280 /// `const fn` destructors.
281 ///
282 /// <div class="warning">
283 /// <b>Use with care!</b>
284 ///
285 /// This method does not ensure the `value` is actually valid. Callers of this method should
286 /// take great care to ensure that `self.is_some()` is checked elsewhere.
287 /// </div>
288 #[inline]
289 #[must_use]
290 pub const fn as_inner_unchecked(&self) -> &T {
291 &self.value
292 }
293
294 /// Calls the provided callback with the wrapped inner value, which computes a [`Choice`],
295 /// and updates `self.is_some()`.
296 ///
297 /// It updates it to be [`Choice::FALSE`] in the event the returned choice is also false.
298 /// If it was [`Choice::FALSE`] to begin with, it will unconditionally remain that way.
299 #[inline]
300 #[must_use]
301 pub fn filter<P>(mut self, predicate: P) -> Self
302 where
303 P: FnOnce(&T) -> Choice,
304 {
305 self.is_some &= predicate(&self.value);
306 self
307 }
308
309 /// Apply an additional [`Choice`] requirement to `is_some`.
310 #[inline]
311 #[must_use]
312 pub const fn filter_by(mut self, is_some: Choice) -> Self {
313 self.is_some = self.is_some.and(is_some);
314 self
315 }
316
317 /// Maps a `CtOption<T>` to a `CtOption<U>` by unconditionally applying a function to the
318 /// contained `value`, but returning a new option value which inherits `self.is_some()`.
319 #[inline]
320 #[must_use]
321 pub fn map<U, F>(self, f: F) -> CtOption<U>
322 where
323 F: FnOnce(T) -> U,
324 {
325 CtOption::new(f(self.value), self.is_some)
326 }
327
328 /// Maps a `CtOption<T>` to a `U` value, eagerly evaluating the provided function, and returning
329 /// the supplied `default` in the event `self.is_some()` is [`Choice::FALSE`].
330 #[inline]
331 #[must_use = "if you don't need the returned value, use `if let` instead"]
332 pub fn map_or<U, F>(self, default: U, f: F) -> U
333 where
334 U: CtSelect,
335 F: FnOnce(T) -> U,
336 {
337 self.map(f).unwrap_or(default)
338 }
339
340 /// Maps a `CtOption<T>` to a `U` value, eagerly evaluating the provided function, precomputing
341 /// `U::default()` using the [`Default`] trait, and returning it in the event `self.is_some()`
342 /// is [`Choice::FALSE`].
343 #[inline]
344 #[must_use]
345 pub fn map_or_default<U, F>(self, f: F) -> U
346 where
347 U: CtSelect + Default,
348 F: FnOnce(T) -> U,
349 {
350 self.map_or(U::default(), f)
351 }
352
353 /// Transforms a `CtOption<T>` into a `Result<T, E>`, mapping to `Ok(T)` if `self.is_some()` is
354 /// [`Choice::TRUE`], or mapping to the provided `err` in the event `self.is_some()` is
355 /// [`Choice::FALSE`].
356 ///
357 /// <div class="warning">
358 /// <b>Warning: variable-time!</b>
359 ///
360 /// This implementation doesn't intend to be constant-time nor try to protect the leakage of the
361 /// `T` value since the [`Result`] will do it anyway.
362 /// </div>
363 #[inline]
364 pub fn ok_or<E>(self, err: E) -> Result<T, E> {
365 self.into_option().ok_or(err)
366 }
367
368 /// Transforms a `CtOption<T>` into a `Result<T, E>` by unconditionally calling the provided
369 /// callback value and using its result in the event `self.is_some()` is [`Choice::FALSE`].
370 ///
371 /// <div class="warning">
372 /// <b>Warning: variable-time!</b>
373 ///
374 /// This implementation doesn't intend to be constant-time nor try to protect the leakage of the
375 /// `T` value since the [`Result`] will do it anyway.
376 /// </div>
377 #[inline]
378 pub fn ok_or_else<E, F>(self, err: F) -> Result<T, E>
379 where
380 F: FnOnce() -> E,
381 {
382 self.ok_or(err())
383 }
384
385 /// Returns `self` if `self.is_some()` is [`Choice::TRUE`], otherwise returns `optb`.
386 #[inline]
387 #[must_use]
388 pub fn or(self, optb: CtOption<T>) -> CtOption<T>
389 where
390 T: CtSelect,
391 {
392 CtOption {
393 value: self.value.ct_select(&optb.value, self.is_none()),
394 is_some: self.is_some | optb.is_some,
395 }
396 }
397
398 /// Obtain a copy of the inner value without first checking that `self.is_some()` is
399 /// [`Choice::TRUE`].
400 ///
401 /// This method is primarily intended for use in `const fn` scenarios where it's not yet
402 /// possible to use the safe combinator methods, and uses a `Copy` bound to avoid issues with
403 /// `const fn` destructors.
404 ///
405 /// <div class="warning">
406 /// <b>Use with care!</b>
407 ///
408 /// This method does not ensure the `value` is actually valid. Callers of this method should
409 /// take great care to ensure that `self.is_some()` is checked elsewhere.
410 /// </div>
411 #[inline]
412 #[must_use]
413 pub const fn to_inner_unchecked(self) -> T
414 where
415 T: Copy,
416 {
417 self.value
418 }
419
420 /// Return the contained value, consuming the `self` value.
421 ///
422 /// Use of this function is discouraged due to panic potential. Instead, prefer non-panicking
423 /// alternatives such as `unwrap_or` or `unwrap_or_default` which operate in constant-time.
424 ///
425 /// As the final step of a sequence of constant-time operations, or in the event you are dealing
426 /// with a [`CtOption`] in a non-secret context where constant-time does not matter, you can
427 /// also convert to [`Option`] using `into_option` or the [`From`] impl on [`Option`]. Note
428 /// this introduces a branch and with it a small amount of timing variability. If possible try
429 /// to avoid this branch when writing constant-time code (e.g. use implicit rejection instead
430 /// of `Option`/`Result` to handle errors)
431 ///
432 /// # Panics
433 /// In the event `self.is_some()` is [`Choice::FALSE`].
434 #[inline]
435 #[must_use]
436 #[track_caller]
437 pub fn unwrap(self) -> T {
438 assert!(
439 self.is_some.to_bool(),
440 "called `CtOption::unwrap()` on a value with `is_some` set to `Choice::FALSE`"
441 );
442 self.value
443 }
444
445 /// Return the contained value in the event `self.is_some()` is [`Choice::TRUE`], or if not,
446 /// uses a provided default.
447 #[inline]
448 #[must_use]
449 pub fn unwrap_or(self, default: T) -> T
450 where
451 T: CtSelect,
452 {
453 default.ct_select(&self.value, self.is_some)
454 }
455
456 /// Unconditionally computes `T::default()` using the [`Default`] trait, then returns either
457 /// the contained value if `self.is_some()` is [`Choice::TRUE`], or if it's [`Choice::FALSE`]
458 /// returns the previously computed default.
459 #[inline]
460 #[must_use]
461 pub fn unwrap_or_default(self) -> T
462 where
463 T: CtSelect + Default,
464 {
465 self.unwrap_or(T::default())
466 }
467
468 /// Returns an "is some" [`CtOption`] with the contained value from either `self` or `optb` in
469 /// the event exactly one of them has `self.is_some()` set to [`Choice::TRUE`], or else returns
470 /// a [`CtOption`] with `self.is_some()` set to [`Choice::FALSE`].
471 #[inline]
472 #[must_use]
473 pub fn xor(self, optb: CtOption<T>) -> CtOption<T>
474 where
475 T: CtSelect,
476 {
477 CtOption {
478 value: self.value.ct_select(&optb.value, self.is_none()),
479 is_some: self.is_some ^ optb.is_some,
480 }
481 }
482
483 /// Zips `self` with another [`CtOption`].
484 ///
485 /// If `self.is_some() && other.is_some()`, this method returns a new [`CtOption`] for a 2-tuple
486 /// of their contents where `is_some()` is [`Choice::TRUE`].
487 ///
488 /// Otherwise, a [`CtOption`] where `is_some()` is [`Choice::FALSE`] is returned.
489 pub fn zip<U>(self, other: CtOption<U>) -> CtOption<(T, U)> {
490 CtOption {
491 value: (self.value, other.value),
492 is_some: self.is_some & other.is_some,
493 }
494 }
495
496 /// Zips `self` and another `CtOption` with function `f`.
497 ///
498 /// If `self.is_some() && other.is_some()`, this method returns a new [`CtOption`] for
499 /// the result of `f` applied to their inner values where `is_some()` is [`Choice::TRUE`].
500 ///
501 /// Otherwise, a [`CtOption`] where `is_some()` is [`Choice::FALSE`] is returned.
502 pub fn zip_with<U, F, R>(self, other: CtOption<U>, f: F) -> CtOption<R>
503 where
504 F: FnOnce(T, U) -> R,
505 {
506 self.zip(other).map(|(a, b)| f(a, b))
507 }
508}
509
510impl<T> CtOption<&T> {
511 /// Maps a `CtOption<&T>` to `CtOption<T>` by copying the contents of the option.
512 #[must_use = "`self` will be dropped if the result is not used"]
513 pub const fn copied(self) -> CtOption<T>
514 where
515 T: Copy,
516 {
517 CtOption {
518 value: *self.value,
519 is_some: self.is_some,
520 }
521 }
522
523 /// Maps a `CtOption<&T>` to `CtOption<T>` by cloning the contents of the option.
524 #[must_use = "`self` will be dropped if the result is not used"]
525 pub fn cloned(self) -> CtOption<T>
526 where
527 T: Clone,
528 {
529 CtOption {
530 value: self.value.clone(),
531 is_some: self.is_some,
532 }
533 }
534}
535
536impl<T> CtOption<&mut T> {
537 /// Maps a `CtOption<&mut T>` to `CtOption<T>` by copying the contents of the option.
538 #[must_use = "`self` will be dropped if the result is not used"]
539 pub const fn copied(self) -> CtOption<T>
540 where
541 T: Copy,
542 {
543 CtOption {
544 value: *self.value,
545 is_some: self.is_some,
546 }
547 }
548
549 /// Maps a `CtOption<&mut T>` to `CtOption<T>` by cloning the contents of the option.
550 #[must_use = "`self` will be dropped if the result is not used"]
551 pub fn cloned(self) -> CtOption<T>
552 where
553 T: Clone,
554 {
555 CtOption {
556 value: self.value.clone(),
557 is_some: self.is_some,
558 }
559 }
560}
561
562impl<T: CtAssign> CtAssign for CtOption<T> {
563 fn ct_assign(&mut self, other: &Self, choice: Choice) {
564 self.value.ct_assign(&other.value, choice);
565 self.is_some.ct_assign(&other.is_some, choice);
566 }
567}
568impl<T: CtAssign> CtAssignSlice for CtOption<T> {}
569
570impl<T: CtEq> CtEq for CtOption<T> {
571 #[inline]
572 fn ct_eq(&self, other: &CtOption<T>) -> Choice {
573 (self.is_some & other.is_some & self.value.ct_eq(&other.value))
574 | (self.is_none() & other.is_none())
575 }
576}
577
578impl<T: CtEq> CtEqSlice for CtOption<T> {}
579
580impl<T: CtSelect> CtSelect for CtOption<T> {
581 fn ct_select(&self, other: &Self, choice: Choice) -> Self {
582 Self {
583 value: self.value.ct_select(&other.value, choice),
584 is_some: self.is_some.ct_select(&other.is_some, choice),
585 }
586 }
587}
588
589impl<T: Default> Default for CtOption<T> {
590 fn default() -> Self {
591 Self::none()
592 }
593}
594
595/// Convert the [`CtOption`] wrapper into an [`Option`], depending on whether
596/// [`CtOption::is_some`] is a truthy or falsy [`Choice`].
597///
598/// <div class="warning">
599/// <b>Warning: variable-time!</b>
600///
601/// This implementation doesn't intend to be constant-time nor try to protect the leakage of the
602/// `T` value since the `Option` will do it anyway.
603/// </div>
604impl<T> From<CtOption<T>> for Option<T> {
605 fn from(src: CtOption<T>) -> Option<T> {
606 src.into_option()
607 }
608}
609
610/// NOTE: in order to be able to unwrap the `subtle::CtOption` we rely on a `Default` bound in
611/// order to have a placeholder value, and `ConditionallySelectable` to be able to use `unwrap_or`.
612#[cfg(feature = "subtle")]
613impl<T> From<subtle::CtOption<T>> for CtOption<T>
614where
615 T: subtle::ConditionallySelectable + Default,
616{
617 #[inline]
618 fn from(src: subtle::CtOption<T>) -> CtOption<T> {
619 let is_some = src.is_some();
620 CtOption {
621 value: src.unwrap_or(Default::default()),
622 is_some: is_some.into(),
623 }
624 }
625}
626
627#[cfg(feature = "subtle")]
628impl<T> From<CtOption<T>> for subtle::CtOption<T> {
629 #[inline]
630 fn from(src: CtOption<T>) -> subtle::CtOption<T> {
631 subtle::CtOption::new(src.value, src.is_some.into())
632 }
633}
634
635#[cfg(feature = "subtle")]
636impl<T> subtle::ConditionallySelectable for CtOption<T>
637where
638 T: Copy, // `ConditionallySelectable` supertrait bound
639 Self: CtSelect,
640{
641 #[inline]
642 fn conditional_select(a: &Self, b: &Self, choice: subtle::Choice) -> Self {
643 CtSelect::ct_select(a, b, choice.into())
644 }
645}
646
647#[cfg(feature = "subtle")]
648impl<T> subtle::ConstantTimeEq for CtOption<T>
649where
650 Self: CtEq,
651{
652 #[inline]
653 fn ct_eq(&self, other: &Self) -> subtle::Choice {
654 CtEq::ct_eq(self, other).into()
655 }
656}
657
658#[cfg(test)]
659mod tests {
660 use crate::{Choice, CtEq, CtOption, CtSelect};
661
662 /// Example wrapped value for testing
663 const VALUE: u8 = 42;
664
665 /// Example option which is like `Option::Some`
666 const SOME: CtOption<u8> = CtOption::new(VALUE, Choice::TRUE);
667
668 /// Example option which is like `Option::None`
669 const NONE: CtOption<u8> = CtOption::new(VALUE, Choice::FALSE);
670
671 /// Another option containing a different value
672 const OTHER: CtOption<u8> = CtOption::new(VALUE + 1, Choice::TRUE);
673
674 /// Dummy error type
675 #[derive(Debug, Eq, PartialEq)]
676 struct Error;
677
678 #[test]
679 fn map_macro() {
680 assert!(map!(NONE, u16::from).is_none().to_bool());
681 assert_eq!(map!(SOME, u16::from).unwrap(), u16::from(VALUE));
682 }
683
684 #[test]
685 fn unwrap_or_macro() {
686 // Don't actually use this! It's just a test function implemented in variable-time
687 const fn select_vartime(a: &u8, b: &u8, choice: Choice) -> u8 {
688 if choice.to_bool_vartime() { *b } else { *a }
689 }
690
691 assert_eq!(
692 unwrap_or!(NONE, OTHER.unwrap(), select_vartime),
693 OTHER.unwrap()
694 );
695 assert_eq!(unwrap_or!(SOME, OTHER.unwrap(), select_vartime), VALUE);
696 }
697
698 #[test]
699 fn ct_eq() {
700 assert!(NONE.ct_eq(&NONE).to_bool());
701 assert!(NONE.ct_ne(&SOME).to_bool());
702 assert!(SOME.ct_ne(&NONE).to_bool());
703 assert!(SOME.ct_eq(&SOME).to_bool());
704 assert!(SOME.ct_ne(&OTHER).to_bool());
705 }
706
707 #[test]
708 fn ct_select() {
709 assert!(NONE.ct_select(&SOME, Choice::FALSE).is_none().to_bool());
710 assert!(NONE.ct_select(&SOME, Choice::TRUE).ct_eq(&SOME).to_bool());
711 assert!(SOME.ct_select(&NONE, Choice::FALSE).ct_eq(&SOME).to_bool());
712 assert!(SOME.ct_select(&NONE, Choice::TRUE).is_none().to_bool());
713 }
714
715 #[test]
716 fn default() {
717 assert!(NONE.ct_eq(&CtOption::default()).to_bool());
718 }
719
720 #[test]
721 fn expect_some() {
722 assert_eq!(SOME.expect("should succeed"), VALUE);
723 }
724
725 #[test]
726 #[should_panic]
727 fn expect_none() {
728 let _ = NONE.expect("should panic");
729 }
730
731 #[test]
732 fn into_option() {
733 assert_eq!(SOME.into_option(), Some(VALUE));
734 assert_eq!(NONE.into_option(), None);
735 }
736
737 #[test]
738 fn into_option_copied() {
739 assert_eq!(SOME.into_option_copied(), Some(VALUE));
740 assert_eq!(NONE.into_option_copied(), None);
741 }
742
743 #[test]
744 fn is_some() {
745 assert!(SOME.is_some().to_bool());
746 assert!(!NONE.is_some().to_bool());
747 }
748
749 #[test]
750 fn is_none() {
751 assert!(!SOME.is_none().to_bool());
752 assert!(NONE.is_none().to_bool());
753 }
754
755 #[test]
756 fn and() {
757 assert!(SOME.and(NONE).is_none().to_bool());
758 assert_eq!(SOME.and(OTHER).unwrap(), OTHER.unwrap());
759 }
760
761 #[test]
762 fn and_then() {
763 assert!(NONE.and_then(|_| NONE).is_none().to_bool());
764 assert!(NONE.and_then(|_| SOME).is_none().to_bool());
765
766 let ret = SOME.and_then(|value| {
767 assert_eq!(VALUE, value);
768 OTHER
769 });
770 assert!(ret.ct_eq(&OTHER).to_bool());
771 }
772
773 #[test]
774 fn filter() {
775 assert!(NONE.filter(|_| Choice::TRUE).ct_eq(&NONE).to_bool());
776 assert!(NONE.filter(|_| Choice::FALSE).ct_eq(&NONE).to_bool());
777 assert!(SOME.filter(|_| Choice::FALSE).ct_eq(&NONE).to_bool());
778
779 let ret = SOME.filter(|&value| {
780 assert_eq!(VALUE, value);
781 Choice::TRUE
782 });
783 assert_eq!(ret.unwrap(), VALUE);
784 }
785
786 #[test]
787 fn filter_by() {
788 assert!(NONE.filter_by(Choice::FALSE).is_none().to_bool());
789 assert!(NONE.filter_by(Choice::TRUE).is_none().to_bool());
790 assert!(SOME.filter_by(Choice::FALSE).ct_eq(&NONE).to_bool());
791 assert_eq!(SOME.filter_by(Choice::TRUE).unwrap(), VALUE);
792 }
793
794 #[test]
795 fn insert() {
796 let mut example = NONE;
797 assert!(example.is_none().to_bool());
798
799 let ret = example.insert(42);
800 assert_eq!(ret, &42);
801 assert!(example.is_some().to_bool());
802 }
803
804 #[test]
805 fn insert_if() {
806 let mut example = NONE;
807 assert!(example.is_none().to_bool());
808
809 example.insert_if(&42, Choice::FALSE);
810 assert!(example.is_none().to_bool());
811
812 example.insert_if(&42, Choice::TRUE);
813 assert_eq!(example.unwrap(), 42);
814 }
815
816 #[test]
817 fn map() {
818 assert!(NONE.map(|value| value + 1).ct_eq(&NONE).to_bool());
819 assert!(SOME.map(|value| value + 1).ct_eq(&OTHER).to_bool());
820 }
821
822 #[test]
823 fn map_or() {
824 let example = 52;
825 assert_eq!(NONE.map_or(example, |value| value + 1), example);
826 assert_eq!(SOME.map_or(example, |value| value + 1), VALUE + 1);
827 }
828
829 #[test]
830 fn map_or_default() {
831 assert_eq!(NONE.map_or_default(|value| value + 1), Default::default());
832 assert_eq!(SOME.map_or_default(|value| value + 1), VALUE + 1);
833 }
834
835 #[test]
836 fn ok_or() {
837 assert_eq!(NONE.ok_or(Error), Err(Error));
838 assert_eq!(SOME.ok_or(Error), Ok(VALUE));
839 }
840
841 #[test]
842 fn ok_or_else() {
843 assert_eq!(NONE.ok_or_else(|| Error), Err(Error));
844 assert_eq!(SOME.ok_or_else(|| Error), Ok(VALUE));
845 }
846
847 #[test]
848 fn or() {
849 assert!(NONE.or(NONE).is_none().to_bool());
850 assert!(SOME.or(NONE).ct_eq(&SOME).to_bool());
851 assert!(NONE.or(SOME).ct_eq(&SOME).to_bool());
852 assert!(SOME.or(OTHER).ct_eq(&SOME).to_bool());
853 }
854
855 #[test]
856 fn some() {
857 assert!(CtOption::some(VALUE).ct_eq(&SOME).to_bool());
858 }
859
860 #[test]
861 fn unwrap_some() {
862 assert_eq!(SOME.unwrap(), VALUE);
863 }
864
865 #[test]
866 #[should_panic]
867 fn unwrap_none() {
868 let _ = NONE.unwrap();
869 }
870
871 #[test]
872 fn unwrap_or() {
873 let example = 52;
874 assert_eq!(NONE.unwrap_or(example), example);
875 assert_eq!(SOME.unwrap_or(example), VALUE);
876 }
877
878 #[test]
879 fn unwrap_or_default() {
880 assert_eq!(NONE.unwrap_or_default(), Default::default());
881 assert_eq!(SOME.unwrap_or_default(), VALUE);
882 }
883
884 #[test]
885 fn xor() {
886 assert!(NONE.xor(NONE).is_none().to_bool());
887 assert!(SOME.xor(NONE).ct_eq(&SOME).to_bool());
888 assert!(NONE.xor(SOME).ct_eq(&SOME).to_bool());
889 assert!(SOME.xor(OTHER).is_none().to_bool());
890 }
891
892 #[test]
893 fn zip() {
894 assert!(NONE.zip(NONE).is_none().to_bool());
895 assert!(NONE.zip(SOME).is_none().to_bool());
896 assert!(SOME.zip(NONE).is_none().to_bool());
897 assert_eq!(SOME.zip(OTHER).unwrap(), (SOME.unwrap(), OTHER.unwrap()));
898 }
899
900 #[test]
901 fn zip_with() {
902 assert!(NONE.zip_with(NONE, |a, b| a + b).is_none().to_bool());
903 assert!(NONE.zip_with(SOME, |a, b| a + b).is_none().to_bool());
904 assert!(SOME.zip_with(NONE, |a, b| a + b).is_none().to_bool());
905 assert_eq!(
906 SOME.zip_with(OTHER, |a, b| a + b).unwrap(),
907 SOME.unwrap() + OTHER.unwrap()
908 );
909 }
910}