Skip to main content

object_rainbow/
map_extra.rs

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