Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CtOption merging #132

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
Loading