clockwork_tuples/traits/
has_one_of.rs

1use crate::{
2    index::{Here, Index, There},
3    traits::has::ConsHas,
4};
5
6/// Selects a single matching element from a cons-style tuple.
7///
8/// # Invariants
9/// - Exactly one element satisfies the query.
10/// - Selection order follows tuple order.
11pub trait ConsHasOne<Query, QueryIdx, Idx = Here> {
12    /// Selected element type.
13    type Has;
14
15    /// Gets the first element out of the query
16    fn cons_get_one(self) -> Self::Has;
17
18    /// Gets the first element out of the query
19    fn cons_get_one_ref(&self) -> &Self::Has;
20}
21
22impl<Query, QueryIdx, Head, Tail> ConsHasOne<Query, QueryIdx, Here> for (Head, Tail)
23where
24    Query: ConsHas<Head, QueryIdx>,
25{
26    type Has = Head;
27
28    fn cons_get_one(self) -> Self::Has { self.0 }
29    fn cons_get_one_ref(&self) -> &Self::Has { &self.0 }
30}
31
32impl<Query, QueryIdx, Head, Tail, TailIdx: Index> ConsHasOne<Query, QueryIdx, There<TailIdx>> for (Head, Tail)
33where
34    Tail: ConsHasOne<Query, QueryIdx, TailIdx>,
35{
36    type Has = <Tail as ConsHasOne<Query, QueryIdx, TailIdx>>::Has;
37
38    fn cons_get_one(self) -> Self::Has { self.1.cons_get_one() }
39    fn cons_get_one_ref(&self) -> &Self::Has { self.1.cons_get_one_ref() }
40}