Skip to content

Commit e3aae68

Browse files
committed
perf(value): Support str's
The benchmarks come back as a wash. The benchmarks don't exercise this case (ie see the benefit) so this means we aren't worse off then before. BREAKING CHANGE: Any `str` passed in must have `&'static` lifetime.
1 parent 9750f22 commit e3aae68

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

src/compiler/token.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Token {
6060
/// be interpreted as a Value.
6161
pub fn to_value(&self) -> Result<Value> {
6262
match self {
63-
&Token::StringLiteral(ref x) => Ok(Value::scalar(x.as_str())),
63+
&Token::StringLiteral(ref x) => Ok(Value::scalar(x.to_owned())),
6464
&Token::IntegerLiteral(x) => Ok(Value::scalar(x)),
6565
&Token::FloatLiteral(x) => Ok(Value::scalar(x)),
6666
&Token::BooleanLiteral(x) => Ok(Value::scalar(x)),
@@ -74,7 +74,7 @@ impl Token {
7474
match *self {
7575
Token::IntegerLiteral(f) => Ok(Argument::Val(Value::scalar(f))),
7676
Token::FloatLiteral(f) => Ok(Argument::Val(Value::scalar(f))),
77-
Token::StringLiteral(ref s) => Ok(Argument::Val(Value::scalar(s.as_str()))),
77+
Token::StringLiteral(ref s) => Ok(Argument::Val(Value::scalar(s.to_owned()))),
7878
Token::BooleanLiteral(b) => Ok(Argument::Val(Value::scalar(b))),
7979
Token::Identifier(ref id) => {
8080
let mut var = Variable::default();

src/filters/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ pub fn split(input: &Value, args: &[Value]) -> FilterResult {
243243

244244
// Split and construct resulting Array
245245
Ok(Value::Array(
246-
input.split(pattern.as_ref()).map(Value::scalar).collect(),
246+
input.split(pattern.as_ref()).map(|s| Value::scalar(s.to_owned())).collect(),
247247
))
248248
}
249249

@@ -258,7 +258,7 @@ pub fn strip(input: &Value, args: &[Value]) -> FilterResult {
258258
check_args_len(args, 0, 0)?;
259259

260260
let input = input.to_str();
261-
Ok(Value::scalar(input.trim()))
261+
Ok(Value::scalar(input.trim().to_owned()))
262262
}
263263

264264
/// Removes all whitespaces (tabs, spaces, and newlines) from the beginning of a string.
@@ -271,7 +271,7 @@ pub fn lstrip(input: &Value, args: &[Value]) -> FilterResult {
271271
check_args_len(args, 0, 0)?;
272272

273273
let input = input.to_str();
274-
Ok(Value::scalar(input.trim_left()))
274+
Ok(Value::scalar(input.trim_left().to_owned()))
275275
}
276276

277277
/// Removes all whitespace (tabs, spaces, and newlines) from the right side of a string.
@@ -284,7 +284,7 @@ pub fn rstrip(input: &Value, args: &[Value]) -> FilterResult {
284284
check_args_len(args, 0, 0)?;
285285

286286
let input = input.to_str();
287-
Ok(Value::scalar(input.trim_right()))
287+
Ok(Value::scalar(input.trim_right().to_owned()))
288288
}
289289

290290
/// Removes any newline characters (line breaks) from a string.

src/value/scalar.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ enum ScalarEnum {
2121
Bool(bool),
2222
#[cfg_attr(feature = "serde", serde(with = "friendly_date"))]
2323
Date(Date),
24-
Str(String),
24+
Str(borrow::Cow<'static, str>),
2525
}
2626

2727
impl Scalar {
@@ -35,7 +35,7 @@ impl Scalar {
3535
ScalarEnum::Float(ref x) => borrow::Cow::Owned(x.to_string()),
3636
ScalarEnum::Bool(ref x) => borrow::Cow::Owned(x.to_string()),
3737
ScalarEnum::Date(ref x) => borrow::Cow::Owned(x.format(DATE_FORMAT).to_string()),
38-
ScalarEnum::Str(ref x) => borrow::Cow::Borrowed(x.as_str()),
38+
ScalarEnum::Str(ref x) => borrow::Cow::Borrowed(x.as_ref()),
3939
}
4040
}
4141

@@ -45,7 +45,7 @@ impl Scalar {
4545
ScalarEnum::Float(x) => x.to_string(),
4646
ScalarEnum::Bool(x) => x.to_string(),
4747
ScalarEnum::Date(x) => x.to_string(),
48-
ScalarEnum::Str(x) => x,
48+
ScalarEnum::Str(x) => x.into_owned(),
4949
}
5050
}
5151

@@ -80,7 +80,7 @@ impl Scalar {
8080
pub fn to_date(&self) -> Option<Date> {
8181
match self.0 {
8282
ScalarEnum::Date(ref x) => Some(*x),
83-
ScalarEnum::Str(ref x) => parse_date(x.as_str()),
83+
ScalarEnum::Str(ref x) => parse_date(x.as_ref()),
8484
_ => None,
8585
}
8686
}
@@ -150,23 +150,23 @@ impl From<Date> for Scalar {
150150
impl From<String> for Scalar {
151151
fn from(s: String) -> Self {
152152
Scalar {
153-
0: ScalarEnum::Str(s),
153+
0: ScalarEnum::Str(s.into()),
154154
}
155155
}
156156
}
157157

158158
impl<'a> From<&'a String> for Scalar {
159159
fn from(s: &String) -> Self {
160160
Scalar {
161-
0: ScalarEnum::Str(s.to_owned()),
161+
0: ScalarEnum::Str(s.to_owned().into()),
162162
}
163163
}
164164
}
165165

166-
impl<'a> From<&'a str> for Scalar {
167-
fn from(s: &str) -> Self {
166+
impl From<&'static str> for Scalar {
167+
fn from(s: &'static str) -> Self {
168168
Scalar {
169-
0: ScalarEnum::Str(s.to_owned()),
169+
0: ScalarEnum::Str(s.into()),
170170
}
171171
}
172172
}

0 commit comments

Comments
 (0)