Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,18 @@ impl<T> CtOption<T> {
pub fn into_option(self) -> Option<T> {
self.into()
}

/// Take a `CtOption<T>` and merge it with a `CtOption<U>` to produce a
/// `CtOption<(T, U)>`.
///
/// The result contains each option's value, and is `Some` if and only if each
/// of the options is `Some`.
pub fn merge<U>(self, other: CtOption<U>) -> CtOption<(T, U)> {
CtOption {
value: (self.value, other.value),
is_some: self.is_some & other.is_some,
}
}
}

impl<T: ConditionallySelectable> ConditionallySelectable for CtOption<T> {
Expand Down
6 changes: 6 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ fn test_ctoption() {
assert!(CtOption::new(1, Choice::from(1)).ct_eq(&CtOption::new(2, Choice::from(1))).unwrap_u8() == 0);
assert!(CtOption::new(1, Choice::from(1)).ct_eq(&CtOption::new(1, Choice::from(1))).unwrap_u8() == 1);
assert!(CtOption::new(1, Choice::from(1)).ct_eq(&CtOption::new(1, Choice::from(1))).unwrap_u8() == 1);

// Test merging
assert!(CtOption::new(1u8, Choice::from(0)).merge(CtOption::new(0u32, Choice::from(0))).into_option().is_none());
assert!(CtOption::new(1u8, Choice::from(1)).merge(CtOption::new(0u32, Choice::from(0))).into_option().is_none());
assert!(CtOption::new(1u8, Choice::from(0)).merge(CtOption::new(0u32, Choice::from(1))).into_option().is_none());
assert_eq!(CtOption::new(1u8, Choice::from(1)).merge(CtOption::new(0u32, Choice::from(1))).into_option(), Some((1u8, 0u32)));
}

#[test]
Expand Down