Skip to content

Commit e81d863

Browse files
committed
Use a boxed arrays instead of a vectors for storing attributes, children and member expressions
Use box syntax because of rust-lang/rust#41831 Signed-off-by: Victor Porof <[email protected]>
1 parent 23846d9 commit e81d863

11 files changed

+229
-196
lines changed

src/parse_attributes.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn rsx_attributes<I>(input: I) -> ParseResult<RSXAttributes, I>
3535
where
3636
I: Stream<Item = char>
3737
{
38-
many1(parser(rsx_attribute).skip(parser(js_whitespace))).map(RSXAttributes).parse_stream(input)
38+
many1(parser(rsx_attribute).skip(parser(js_whitespace))).parse_stream(input)
3939
}
4040

4141
pub fn rsx_attribute<I>(input: I) -> ParseResult<RSXAttribute, I>
@@ -211,7 +211,7 @@ mod tests {
211211
.0;
212212

213213
let tokens = quote! {
214-
RSXAttributes(vec![
214+
RSXAttributes(box [
215215
RSXAttribute::Named(
216216
RSXAttributeName::Name(RSXIdentifier::from("foo")),
217217
RSXAttributeValue::Default
@@ -348,34 +348,34 @@ mod tests {
348348
RSXAttributeName::Name(RSXIdentifier::from("foo")),
349349
RSXAttributeValue::Element(RSXElement::SelfClosing(RSXSelfClosingElement(
350350
RSXElementName::Name(RSXIdentifier::from("bar")),
351-
RSXAttributes(vec![])
351+
RSXAttributes(box [])
352352
)))
353353
),
354354
RSXAttribute::Named(
355355
RSXAttributeName::Name(RSXIdentifier::from("foo")),
356356
RSXAttributeValue::CodeBlock(RSXArbitraryCodeRegion::RuntimeValue(
357-
RSXRuntimeValue(Box::new({
357+
RSXRuntimeValue(box {
358358
RSXElement::SelfClosing(RSXSelfClosingElement(
359359
RSXElementName::Name(RSXIdentifier::from("bar")),
360-
RSXAttributes(vec![])
360+
RSXAttributes(box [])
361361
))
362-
}))
362+
})
363363
))
364364
),
365365
RSXAttribute::Named(
366366
RSXAttributeName::Name(RSXIdentifier::from("foo")),
367367
RSXAttributeValue::CodeBlock(RSXArbitraryCodeRegion::RuntimeValue(
368-
RSXRuntimeValue(Box::new({ 1 + 2 + 3 }))
368+
RSXRuntimeValue(box { 1 + 2 + 3 })
369369
))
370370
),
371371
RSXAttribute::Named(
372372
RSXAttributeName::Name(RSXIdentifier::from("foo")),
373373
RSXAttributeValue::CodeBlock(RSXArbitraryCodeRegion::RuntimeValue(
374-
RSXRuntimeValue(Box::new({
374+
RSXRuntimeValue(box {
375375
{
376376
1 + { 2 + { 3 } }
377377
}
378-
}))
378+
})
379379
))
380380
),
381381
RSXAttribute::Named(
@@ -414,31 +414,31 @@ mod tests {
414414
assert_eq!(p(rsx_attributes).parse(" ").is_err(), true);
415415
assert_eq!(
416416
p(rsx_attributes).parse("foo").unwrap(),
417-
(RSXAttributes(vec![("foo", "true").into()]), "")
417+
(RSXAttributes::from(vec![("foo", "true").into()]), "")
418418
);
419419
assert_eq!(
420420
p(rsx_attributes).parse("foo bar").unwrap(),
421-
(RSXAttributes(vec![("foo", "true").into(), ("bar", "true").into()]), "")
421+
(RSXAttributes::from(vec![("foo", "true").into(), ("bar", "true").into()]), "")
422422
);
423423
assert_eq!(
424424
p(rsx_attributes).parse("foo = 'bar'").unwrap(),
425-
(RSXAttributes(vec![("foo", "bar").into()]), "")
425+
(RSXAttributes::from(vec![("foo", "bar").into()]), "")
426426
);
427427
assert_eq!(
428428
p(rsx_attributes).parse("foo='bar' baz").unwrap(),
429-
(RSXAttributes(vec![("foo", "bar").into(), ("baz", "true").into()]), "")
429+
(RSXAttributes::from(vec![("foo", "bar").into(), ("baz", "true").into()]), "")
430430
);
431431
assert_eq!(
432432
p(rsx_attributes).parse("foo = 'bar' baz").unwrap(),
433-
(RSXAttributes(vec![("foo", "bar").into(), ("baz", "true").into()]), "")
433+
(RSXAttributes::from(vec![("foo", "bar").into(), ("baz", "true").into()]), "")
434434
);
435435
assert_eq!(
436436
p(rsx_attributes).parse("foo='bar' bar='baz'").unwrap(),
437-
(RSXAttributes(vec![("foo", "bar").into(), ("bar", "baz").into()]), "")
437+
(RSXAttributes::from(vec![("foo", "bar").into(), ("bar", "baz").into()]), "")
438438
);
439439
assert_eq!(
440440
p(rsx_attributes).parse("foo = 'bar' bar='baz'").unwrap(),
441-
(RSXAttributes(vec![("foo", "bar").into(), ("bar", "baz").into()]), "")
441+
(RSXAttributes::from(vec![("foo", "bar").into(), ("bar", "baz").into()]), "")
442442
);
443443
}
444444

src/parse_attributes_types.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ CONDITIONS OF ANY KIND, either express or implied. See the License for the
99
specific language governing permissions and limitations under the License.
1010
*/
1111

12+
use std::iter::FromIterator;
13+
1214
#[cfg(feature = "tokenize")]
1315
use self_tokenize_macro::{DefaultQuote, SelfTokenize};
1416
#[cfg(feature = "tokenize")]
@@ -20,7 +22,19 @@ use parse_js_types::{JSBool, JSDoubleStringCharacters, JSNumber, JSSingleStringC
2022

2123
#[derive(Debug, PartialEq)]
2224
#[cfg_attr(feature = "tokenize", derive(SelfTokenize))]
23-
pub struct RSXAttributes(pub Vec<RSXAttribute>);
25+
pub struct RSXAttributes(pub Box<[RSXAttribute]>);
26+
27+
impl From<Vec<RSXAttribute>> for RSXAttributes {
28+
fn from(vec: Vec<RSXAttribute>) -> Self {
29+
RSXAttributes(vec.into_boxed_slice())
30+
}
31+
}
32+
33+
impl FromIterator<RSXAttribute> for RSXAttributes {
34+
fn from_iter<I: IntoIterator<Item = RSXAttribute>>(iter: I) -> Self {
35+
RSXAttributes::from(iter.into_iter().collect::<Vec<_>>())
36+
}
37+
}
2438

2539
#[derive(Debug, PartialEq)]
2640
#[cfg_attr(feature = "tokenize", derive(SelfTokenize))]

src/parse_children.rs

+26-24
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn rsx_children<I>(input: I) -> ParseResult<RSXChildren, I>
2222
where
2323
I: Stream<Item = char>
2424
{
25-
many1(parser(rsx_child).skip(parser(js_whitespace))).map(RSXChildren).parse_stream(input)
25+
many1(parser(rsx_child).skip(parser(js_whitespace))).parse_stream(input)
2626
}
2727

2828
pub fn rsx_child<I>(input: I) -> ParseResult<RSXChild, I>
@@ -79,29 +79,25 @@ mod tests {
7979
.0;
8080

8181
let tokens = quote! {
82-
RSXChildren(vec![
82+
RSXChildren(box [
8383
RSXChild::Text(RSXText::from(
8484
"foo\n foo bar baz\n 123"
8585
)),
8686
RSXChild::Element(RSXElement::SelfClosing(RSXSelfClosingElement(
8787
RSXElementName::Name(RSXIdentifier::from("foo")),
88-
RSXAttributes(vec![])
89-
))),
90-
RSXChild::CodeBlock(RSXArbitraryCodeRegion::RuntimeValue(RSXRuntimeValue(
91-
Box::new({
92-
RSXElement::SelfClosing(RSXSelfClosingElement(
93-
RSXElementName::Name(RSXIdentifier::from("foo")),
94-
RSXAttributes(vec![])
95-
))
96-
})
97-
))),
98-
RSXChild::CodeBlock(RSXArbitraryCodeRegion::RuntimeValue(RSXRuntimeValue(
99-
Box::new({
100-
{
101-
1 + { 2 + { 3 } }
102-
}
103-
})
88+
RSXAttributes(box [])
10489
))),
90+
RSXChild::CodeBlock(RSXArbitraryCodeRegion::RuntimeValue(RSXRuntimeValue(box {
91+
RSXElement::SelfClosing(RSXSelfClosingElement(
92+
RSXElementName::Name(RSXIdentifier::from("foo")),
93+
RSXAttributes(box [])
94+
))
95+
}))),
96+
RSXChild::CodeBlock(RSXArbitraryCodeRegion::RuntimeValue(RSXRuntimeValue(box {
97+
{
98+
1 + { 2 + { 3 } }
99+
}
100+
}))),
105101
])
106102
};
107103

@@ -111,20 +107,26 @@ mod tests {
111107
#[test]
112108
pub fn test_rsx_children() {
113109
assert_eq!(p(rsx_children).parse("").is_err(), true);
114-
assert_eq!(p(rsx_children).parse(" ").unwrap(), (RSXChildren(vec![" ".into()]), ""));
115-
assert_eq!(p(rsx_children).parse("foo").unwrap(), (RSXChildren(vec!["foo".into()]), ""));
116-
assert_eq!(p(rsx_children).parse("foo!").unwrap(), (RSXChildren(vec!["foo!".into()]), ""));
110+
assert_eq!(p(rsx_children).parse(" ").unwrap(), (RSXChildren::from(vec![" ".into()]), ""));
111+
assert_eq!(
112+
p(rsx_children).parse("foo").unwrap(),
113+
(RSXChildren::from(vec!["foo".into()]), "")
114+
);
115+
assert_eq!(
116+
p(rsx_children).parse("foo!").unwrap(),
117+
(RSXChildren::from(vec!["foo!".into()]), "")
118+
);
117119
assert_eq!(
118120
p(rsx_children).parse("foo bar baz!").unwrap(),
119-
(RSXChildren(vec!["foo bar baz!".into()]), "")
121+
(RSXChildren::from(vec!["foo bar baz!".into()]), "")
120122
);
121123
assert_eq!(
122124
p(rsx_children).parse("\"foo\" \"bar\" \"baz\"").unwrap(),
123-
(RSXChildren(vec!["\"foo\" \"bar\" \"baz\"".into()]), "")
125+
(RSXChildren::from(vec!["\"foo\" \"bar\" \"baz\"".into()]), "")
124126
);
125127
assert_eq!(
126128
p(rsx_children).parse("\"foo\"\n\"bar\"\n\"baz\"").unwrap(),
127-
(RSXChildren(vec!["\"foo\"\n\"bar\"\n\"baz\"".into()]), "")
129+
(RSXChildren::from(vec!["\"foo\"\n\"bar\"\n\"baz\"".into()]), "")
128130
);
129131
}
130132

src/parse_children_types.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,19 @@ use parse_external_types::RSXArbitraryCodeRegion;
2222

2323
#[derive(Debug, PartialEq)]
2424
#[cfg_attr(feature = "tokenize", derive(SelfTokenize))]
25-
pub struct RSXChildren(pub Vec<RSXChild>);
25+
pub struct RSXChildren(pub Box<[RSXChild]>);
26+
27+
impl From<Vec<RSXChild>> for RSXChildren {
28+
fn from(vec: Vec<RSXChild>) -> Self {
29+
RSXChildren(vec.into_boxed_slice())
30+
}
31+
}
32+
33+
impl FromIterator<RSXChild> for RSXChildren {
34+
fn from_iter<I: IntoIterator<Item = RSXChild>>(iter: I) -> Self {
35+
RSXChildren::from(iter.into_iter().collect::<Vec<_>>())
36+
}
37+
}
2638

2739
#[derive(Debug, PartialEq)]
2840
#[cfg_attr(feature = "tokenize", derive(SelfTokenize))]

0 commit comments

Comments
 (0)