Skip to content

Commit

Permalink
add extract for getting the value out of a 1-coproduct (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosefromthedead authored Oct 2, 2022
1 parent 573f44f commit bb4e8d5
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions core/src/coproduct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,30 @@ impl<Head, Tail> Coproduct<Head, Tail> {
}
}

impl<T> Coproduct<T, CNil> {
/// Extract the value from a coproduct with only one variant.
///
/// # Example
///
/// ```
/// # fn main() {
/// use frunk_core::Coprod;
///
/// type I32Only = Coprod!(i32);
/// let co = I32Only::inject(5);
///
/// assert_eq!(co.extract(), 5);
/// # }
/// ```
#[inline(always)]
pub fn extract(self) -> T {
match self {
Coproduct::Inl(v) => v,
Coproduct::Inr(never) => match never {},
}
}
}

/// Trait for instantiating a coproduct from an element
///
/// This trait is part of the implementation of the inherent static method
Expand Down

1 comment on commit bb4e8d5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Frunk Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: bb4e8d5 Previous: 573f44f Ratio
generic_conversion 1 ns/iter (± 0) 0 ns/iter (± 0) Infinity
labelled_conversion 1 ns/iter (± 0) 0 ns/iter (± 0) Infinity

This comment was automatically generated by workflow using github-action-benchmark.

CC: @lloydmeta

Please sign in to comment.