cgp_field/traits/
extract_field.rs

1use core::convert::Infallible;
2use core::marker::PhantomData;
3
4use crate::types::Void;
5
6pub trait HasExtractor {
7    type Extractor;
8
9    fn to_extractor(self) -> Self::Extractor;
10
11    fn from_extractor(extractor: Self::Extractor) -> Self;
12}
13
14pub trait HasExtractorRef {
15    type ExtractorRef<'a>
16    where
17        Self: 'a;
18
19    fn extractor_ref(&self) -> Self::ExtractorRef<'_>;
20}
21
22pub trait HasExtractorMut {
23    type ExtractorMut<'a>
24    where
25        Self: 'a;
26
27    fn extractor_mut(&mut self) -> Self::ExtractorMut<'_>;
28}
29
30pub trait ExtractField<Tag> {
31    type Value;
32
33    type Remainder;
34
35    fn extract_field(self, _tag: PhantomData<Tag>) -> Result<Self::Value, Self::Remainder>;
36}
37
38pub trait FinalizeExtract {
39    fn finalize_extract<T>(self) -> T;
40}
41
42impl FinalizeExtract for Void {
43    fn finalize_extract<T>(self) -> T {
44        match self {}
45    }
46}
47
48impl FinalizeExtract for Infallible {
49    fn finalize_extract<T>(self) -> T {
50        match self {}
51    }
52}
53
54pub trait FinalizeExtractResult {
55    type Output;
56
57    fn finalize_extract_result(self) -> Self::Output;
58}
59
60impl<T, E> FinalizeExtractResult for Result<T, E>
61where
62    E: FinalizeExtract,
63{
64    type Output = T;
65
66    fn finalize_extract_result(self) -> T {
67        match self {
68            Ok(value) => value,
69            Err(remainder) => remainder.finalize_extract(),
70        }
71    }
72}