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