choose_from/
choice.rs

1use std::ops::Deref;
2
3#[derive(Debug)]
4pub(crate) struct Guard;
5
6/// A specific choice, passed to closure by [`Selector::with`](crate::Selector::with) or [`SelectorFixed::with`](crate::SelectorFixed::with).
7#[derive(Debug)]
8#[repr(transparent)]
9pub struct Choice<'guard, T> {
10    value: T,
11    _guard: std::marker::PhantomData<&'guard Guard>,
12}
13
14// This type is good to implement Deref because Choice is a transparent wrapper around T
15impl<'a, T> Deref for Choice<'a, T> {
16    type Target = T;
17
18    fn deref(&self) -> &Self::Target {
19        &self.value
20    }
21}
22
23impl<'guard, T> Choice<'guard, T> {
24    pub(crate) fn with_guard(value: T, _guard: &'guard Guard) -> Choice<'guard, T> {
25        Choice {
26            value,
27            _guard: std::marker::PhantomData,
28        }
29    }
30
31    pub(crate) fn into_inner(self) -> T {
32        self.value
33    }
34}
35
36pub(crate) fn to_values<T>(choices: Vec<Choice<'_, T>>) -> Vec<T> {
37    // TODO: check optimization. This is probably optimized well since
38    // choices should have the same size and alignment as T so the collection
39    // may not need to reallocate
40    choices.into_iter().map(Choice::into_inner).collect()
41}