Skip to main content

object_rainbow/
map_extra.rs

1use crate::*;
2
3#[derive(
4    Debug,
5    ToOutput,
6    InlineOutput,
7    Tagged,
8    ListHashes,
9    Topological,
10    Clone,
11    Copy,
12    Size,
13    MaybeHasNiche,
14    PartialEq,
15    Eq,
16    Default,
17)]
18pub struct MappedExtra<T, M = ()>(pub M, pub T);
19
20impl<T, M> Deref for MappedExtra<T, M> {
21    type Target = T;
22
23    fn deref(&self) -> &Self::Target {
24        &self.1
25    }
26}
27
28impl<T, M> DerefMut for MappedExtra<T, M> {
29    fn deref_mut(&mut self) -> &mut Self::Target {
30        &mut self.1
31    }
32}
33
34#[derive_for_wrapped]
35pub trait MapExtra<Extra: 'static + Clone = ()> {
36    type Mapped: 'static + Clone;
37    fn map_extra(&self, extra: Extra) -> Self::Mapped;
38}
39
40impl<
41    M: 'static + Send + Sync + Clone + ParseInline<I> + MapExtra<X, Mapped = E>,
42    E: 'static + Send + Sync + Clone,
43    X: 'static + Send + Sync + Clone,
44    T: Parse<J>,
45    I: PointInput<Extra = X, WithExtra<E> = J>,
46    J: ParseInput,
47> Parse<I> for MappedExtra<T, M>
48{
49    fn parse(mut input: I) -> crate::Result<Self> {
50        let m = input.parse_inline::<M>()?;
51        let x = input.extra().clone();
52        let t = input.parse_extra(m.map_extra(x))?;
53        Ok(Self(m, t))
54    }
55}
56
57impl<
58    M: 'static + Send + Sync + Clone + ParseInline<I> + MapExtra<X, Mapped = E>,
59    E: 'static + Send + Sync + Clone,
60    X: 'static + Send + Sync + Clone,
61    T: ParseInline<J>,
62    I: PointInput<Extra = X, WithExtra<E> = J>,
63    J: ParseInput,
64> ParseInline<I> for MappedExtra<T, M>
65{
66    fn parse_inline(input: &mut I) -> crate::Result<Self> {
67        let m = input.parse_inline::<M>()?;
68        let x = input.extra().clone();
69        let t = input.parse_inline_extra(m.map_extra(x))?;
70        Ok(Self(m, t))
71    }
72}