rusty_parser/wrapper/boxed/
slice_cloned.rs

1use std::ops::Deref;
2use std::ops::DerefMut;
3
4use crate::core::into_parser::IntoParser;
5use crate::core::parser::Parser;
6use crate::core::result::ParseResult;
7use crate::core::tuple::Tuple;
8
9use crate::leaf::panic::Panic;
10
11pub struct DynBoxSlice<Output, T>
12where
13    Output: Tuple,
14    T: Clone,
15{
16    parser: std::boxed::Box<
17        dyn for<'a> Parser<std::iter::Cloned<std::slice::Iter<'a, T>>, Output = Output>,
18    >,
19}
20
21impl<Output, T> DynBoxSlice<Output, T>
22where
23    Output: Tuple,
24    T: Clone,
25{
26    pub fn new<ParserType: IntoParser>(parser: ParserType) -> Self
27    where
28        ParserType::Into:
29            for<'a> Parser<std::iter::Cloned<std::slice::Iter<'a, T>>, Output = Output> + 'static,
30    {
31        Self {
32            parser: std::boxed::Box::new(parser.into_parser()),
33        }
34    }
35    pub fn assign<ParserType: IntoParser>(&mut self, parser: ParserType)
36    where
37        ParserType::Into:
38            for<'a> Parser<std::iter::Cloned<std::slice::Iter<'a, T>>, Output = Output> + 'static,
39    {
40        self.parser = std::boxed::Box::new(parser.into_parser());
41    }
42}
43/// default to dummy parser that always panic
44impl<Output: Tuple + 'static, T: Clone + 'static> Default for DynBoxSlice<Output, T> {
45    fn default() -> Self {
46        Self::new(Panic::new())
47    }
48}
49
50impl<'a, Output, T> Parser<std::iter::Cloned<std::slice::Iter<'a, T>>> for DynBoxSlice<Output, T>
51where
52    Output: Tuple,
53    T: Clone,
54{
55    type Output = Output;
56
57    fn parse(
58        &self,
59        it: std::iter::Cloned<std::slice::Iter<'a, T>>,
60    ) -> ParseResult<Self::Output, std::iter::Cloned<std::slice::Iter<'a, T>>> {
61        self.parser.parse(it)
62    }
63
64    fn match_pattern(
65        &self,
66        it: std::iter::Cloned<std::slice::Iter<'a, T>>,
67    ) -> ParseResult<(), std::iter::Cloned<std::slice::Iter<'a, T>>> {
68        self.parser.match_pattern(it)
69    }
70}
71
72impl<Output, T> Deref for DynBoxSlice<Output, T>
73where
74    Output: Tuple,
75    T: Clone,
76{
77    type Target = std::boxed::Box<
78        dyn for<'a> Parser<std::iter::Cloned<std::slice::Iter<'a, T>>, Output = Output>,
79    >;
80
81    fn deref(&self) -> &Self::Target {
82        &self.parser
83    }
84}
85impl<Output, T> DerefMut for DynBoxSlice<Output, T>
86where
87    Output: Tuple,
88    T: Clone,
89{
90    fn deref_mut(&mut self) -> &mut Self::Target {
91        &mut self.parser
92    }
93}
94impl<Output, T> IntoParser for DynBoxSlice<Output, T>
95where
96    Output: Tuple,
97    T: Clone,
98{
99    type Into = Self;
100    fn into_parser(self) -> Self::Into {
101        self
102    }
103
104    // TODO no boxed here
105}
106
107#[cfg(test)]
108mod test {
109    use super::*;
110    #[test]
111    #[should_panic]
112    fn panic_test2() {
113        let boxed: DynBoxSlice<(i32,), i32> = Default::default();
114        boxed.parse((&[1, 2, 3]).iter().cloned());
115        boxed.match_pattern((&[1, 2, 3]).iter().cloned());
116    }
117}