11
11
//! ```
12
12
//! use icu::plurals::rules::parse_condition;
13
13
//! use icu::plurals::rules::ast::*;
14
+ //! use smallvec::{SmallVec,smallvec};
14
15
//!
15
16
//! let input = "i = 1";
16
17
//!
17
18
//! let ast = parse_condition(input.as_bytes())
18
19
//! .expect("Parsing failed.");
19
20
//!
20
- //! assert_eq!(ast, Condition(Box::new( [
21
- //! AndCondition(Box::new( [
21
+ //! assert_eq!(ast, Condition(smallvec! [
22
+ //! AndCondition(smallvec! [
22
23
//! Relation {
23
24
//! expression: Expression {
24
25
//! operand: Operand::I,
25
26
//! modulus: None,
26
27
//! },
27
28
//! operator: Operator::Eq,
28
- //! range_list: RangeList(Box::new( [
29
+ //! range_list: RangeList(smallvec! [
29
30
//! RangeListItem::Value(
30
31
//! Value(1)
31
32
//! )
32
- //! ]))
33
+ //! ])
33
34
//! }
34
- //! ]))
35
- //! ]))) ;
35
+ //! ])
36
+ //! ]));
36
37
//! ```
37
38
//!
38
39
//! [`PluralCategory`]: crate::PluralCategory
41
42
use alloc:: boxed:: Box ;
42
43
use alloc:: string:: String ;
43
44
use core:: ops:: RangeInclusive ;
45
+ use smallvec:: SmallVec ;
44
46
45
47
/// A complete AST representation of a plural rule.
46
48
/// Comprises a vector of [`AndConditions`] and optionally a set of [`Samples`].
@@ -98,25 +100,26 @@ pub struct Rule {
98
100
/// ```
99
101
/// use icu::plurals::rules::ast::*;
100
102
/// use icu::plurals::rules::parse_condition;
103
+ /// use smallvec::{SmallVec,smallvec};
101
104
///
102
- /// let condition = Condition(Box::new( [
103
- /// AndCondition(Box::new( [Relation {
105
+ /// let condition = Condition(smallvec! [
106
+ /// AndCondition(smallvec! [Relation {
104
107
/// expression: Expression {
105
108
/// operand: Operand::I,
106
109
/// modulus: None,
107
110
/// },
108
111
/// operator: Operator::Eq,
109
- /// range_list: RangeList(Box::new( [RangeListItem::Value(Value(5))]) ),
110
- /// }])) ,
111
- /// AndCondition(Box::new( [Relation {
112
+ /// range_list: RangeList(smallvec! [RangeListItem::Value(Value(5))]),
113
+ /// }]),
114
+ /// AndCondition(smallvec! [Relation {
112
115
/// expression: Expression {
113
116
/// operand: Operand::V,
114
117
/// modulus: None,
115
118
/// },
116
119
/// operator: Operator::Eq,
117
- /// range_list: RangeList(Box::new( [RangeListItem::Value(Value(2))]) ),
118
- /// }])) ,
119
- /// ])) ;
120
+ /// range_list: RangeList(smallvec! [RangeListItem::Value(Value(2))]),
121
+ /// }]),
122
+ /// ]);
120
123
///
121
124
/// assert_eq!(
122
125
/// condition,
@@ -127,7 +130,7 @@ pub struct Rule {
127
130
///
128
131
/// [`AndConditions`]: AndCondition
129
132
#[ derive( Debug , Clone , PartialEq ) ]
130
- pub struct Condition ( pub Box < [ AndCondition ] > ) ;
133
+ pub struct Condition ( pub SmallVec < [ AndCondition ; 2 ] > ) ;
131
134
132
135
/// An incomplete AST representation of a plural rule. Comprises a vector of [`Relations`].
133
136
///
@@ -143,32 +146,33 @@ pub struct Condition(pub Box<[AndCondition]>);
143
146
/// Can be represented by the AST:
144
147
///
145
148
/// ```
146
- /// use icu::plurals::rules::ast::*;
149
+ /// use icu_plurals::rules::ast::*;
150
+ /// use smallvec::{SmallVec,smallvec};
147
151
///
148
- /// AndCondition(Box::new( [
152
+ /// AndCondition(smallvec! [
149
153
/// Relation {
150
154
/// expression: Expression {
151
155
/// operand: Operand::I,
152
156
/// modulus: None,
153
157
/// },
154
158
/// operator: Operator::Eq,
155
- /// range_list: RangeList(Box::new( [RangeListItem::Value(Value(5))]) ),
159
+ /// range_list: RangeList(smallvec! [RangeListItem::Value(Value(5))]),
156
160
/// },
157
161
/// Relation {
158
162
/// expression: Expression {
159
163
/// operand: Operand::V,
160
164
/// modulus: None,
161
165
/// },
162
166
/// operator: Operator::NotEq,
163
- /// range_list: RangeList(Box::new( [RangeListItem::Value(Value(2))]) ),
167
+ /// range_list: RangeList(smallvec! [RangeListItem::Value(Value(2))]),
164
168
/// },
165
- /// ])) ;
169
+ /// ]);
166
170
///
167
171
/// ```
168
172
///
169
173
/// [`Relations`]: Relation
170
174
#[ derive( Debug , Clone , PartialEq ) ]
171
- pub struct AndCondition ( pub Box < [ Relation ] > ) ;
175
+ pub struct AndCondition ( pub SmallVec < [ Relation ; 2 ] > ) ;
172
176
173
177
/// An incomplete AST representation of a plural rule. Comprises an [`Expression`], an [`Operator`], and a [`RangeList`].
174
178
///
@@ -185,14 +189,15 @@ pub struct AndCondition(pub Box<[Relation]>);
185
189
///
186
190
/// ```
187
191
/// use icu::plurals::rules::ast::*;
192
+ /// use smallvec::{SmallVec,smallvec};
188
193
///
189
194
/// Relation {
190
195
/// expression: Expression {
191
196
/// operand: Operand::I,
192
197
/// modulus: None,
193
198
/// },
194
199
/// operator: Operator::Eq,
195
- /// range_list: RangeList(Box::new( [RangeListItem::Value(Value(3))]) ),
200
+ /// range_list: RangeList(smallvec! [RangeListItem::Value(Value(3))]),
196
201
/// };
197
202
///
198
203
/// ```
@@ -304,17 +309,18 @@ pub enum Operand {
304
309
///
305
310
/// ```
306
311
/// use icu::plurals::rules::ast::*;
312
+ /// use smallvec::{SmallVec,smallvec};
307
313
///
308
- /// RangeList(Box::new( [
314
+ /// RangeList(smallvec! [
309
315
/// RangeListItem::Value(Value(5)),
310
316
/// RangeListItem::Value(Value(7)),
311
317
/// RangeListItem::Value(Value(9)),
312
- /// ])) ;
318
+ /// ]);
313
319
/// ```
314
320
///
315
321
/// [`RangeListItems`]: RangeListItem
316
322
#[ derive( Debug , Clone , PartialEq ) ]
317
- pub struct RangeList ( pub Box < [ RangeListItem ] > ) ;
323
+ pub struct RangeList ( pub SmallVec < [ RangeListItem ; 2 ] > ) ;
318
324
319
325
/// An enum of items that appear in a [`RangeList`]: `Range` or a `Value`.
320
326
///
@@ -408,6 +414,8 @@ pub struct Samples {
408
414
///
409
415
/// ```
410
416
/// use icu::plurals::rules::ast::*;
417
+ /// use smallvec::{SmallVec,smallvec};
418
+ ///
411
419
/// SampleList {
412
420
/// sample_ranges: Box::new([
413
421
/// SampleRange {
0 commit comments