Skip to content
Merged
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
14 changes: 13 additions & 1 deletion hugr-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl SumType {
/// If a sum is an option of a single type, return the type.
pub fn as_unary_option(&self) -> Option<&TypeRV> {
self.as_option()
.and_then(|row| (row.len() == 1).then_some(&row[0]))
.and_then(|row| row.iter().exactly_one().ok())
}

/// Returns an iterator over the variants.
Expand Down Expand Up @@ -855,6 +855,18 @@ pub(crate) mod test {
let opt = option_type(usize_t());

assert_eq!(opt.as_unary_option().unwrap().clone(), usize_t());
assert_eq!(
Type::new_unit_sum(2).as_sum().unwrap().as_unary_option(),
None
);

assert_eq!(
Type::new_tuple(vec![usize_t()])
.as_sum()
.unwrap()
.as_option(),
None
);
}

#[test]
Expand Down
Loading