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)]
17pub struct MappedExtra<T, M = ()>(pub M, pub T);
18
19impl<T, M> Deref for MappedExtra<T, M> {
20    type Target = T;
21
22    fn deref(&self) -> &Self::Target {
23        &self.1
24    }
25}
26
27impl<T, M> DerefMut for MappedExtra<T, M> {
28    fn deref_mut(&mut self) -> &mut Self::Target {
29        &mut self.1
30    }
31}
32
33#[derive_for_wrapped]
34pub trait MapExtra<Extra: 'static + Clone = ()> {
35    type Mapped: 'static + Clone;
36    fn map_extra(&self, extra: Extra) -> Self::Mapped;
37}
38
39impl<
40    M: 'static + Send + Sync + Clone + ParseInline<I> + MapExtra<X, Mapped = E>,
41    E: 'static + Send + Sync + Clone,
42    X: 'static + Send + Sync + Clone,
43    T: Parse<J>,
44    I: PointInput<Extra = X, WithExtra<E> = J>,
45    J: ParseInput,
46> Parse<I> for MappedExtra<T, M>
47{
48    fn parse(mut input: I) -> crate::Result<Self> {
49        let m = input.parse_inline::<M>()?;
50        let x = input.extra().clone();
51        let t = input.parse_extra(m.map_extra(x))?;
52        Ok(Self(m, t))
53    }
54}
55
56impl<
57    M: 'static + Send + Sync + Clone + ParseInline<I> + MapExtra<X, Mapped = E>,
58    E: 'static + Send + Sync + Clone,
59    X: 'static + Send + Sync + Clone,
60    T: ParseInline<J>,
61    I: PointInput<Extra = X, WithExtra<E> = J>,
62    J: ParseInput,
63> ParseInline<I> for MappedExtra<T, M>
64{
65    fn parse_inline(input: &mut I) -> crate::Result<Self> {
66        let m = input.parse_inline::<M>()?;
67        let x = input.extra().clone();
68        let t = input.parse_inline_extra(m.map_extra(x))?;
69        Ok(Self(m, t))
70    }
71}