Skip to content

Commit 855cdd4

Browse files
committed
Add CtOption::into_option
A small wrapper for `impl CtOption<T> for Option<T>` which is friendlier for type inference purposes. Using `Option::from(ct_option)` often doesn't work because the compiler is unable to infer `T` for `Option<T>`, so you have to manually annotate `Option::<T>::from`. In practice this often winds up looking like: Option::<T>::from(T::constructor(x))` which feels quite redundant. Using an inherent method instead fixes type inference so the above can be: T::constructor(x).into_option()
1 parent 6b6a81a commit 855cdd4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,22 @@ impl<T> CtOption<T> {
801801

802802
Self::conditional_select(&self, &f, is_none)
803803
}
804+
805+
/// Convert the `CtOption<T>` wrapper into an `Option<T>`, depending on whether
806+
/// the underlying `is_some` `Choice` was a `0` or a `1` once unwrapped.
807+
///
808+
/// # Note
809+
///
810+
/// This function exists to avoid ending up with ugly, verbose and/or bad handled
811+
/// conversions from the `CtOption<T>` wraps to an `Option<T>` or `Result<T, E>`.
812+
/// This implementation doesn't intend to be constant-time nor try to protect the
813+
/// leakage of the `T` since the `Option<T>` will do it anyways.
814+
///
815+
/// It's equivalent to the corresponding `From` impl, however this version is is
816+
/// friendlier for type inference.
817+
pub fn into_option(self) -> Option<T> {
818+
self.into()
819+
}
804820
}
805821

806822
impl<T: ConditionallySelectable> ConditionallySelectable for CtOption<T> {

0 commit comments

Comments
 (0)