aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive/src/parser/expr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'askama_derive/src/parser/expr.rs')
-rw-r--r--askama_derive/src/parser/expr.rs71
1 files changed, 0 insertions, 71 deletions
diff --git a/askama_derive/src/parser/expr.rs b/askama_derive/src/parser/expr.rs
index 1d4ea89..deefb48 100644
--- a/askama_derive/src/parser/expr.rs
+++ b/askama_derive/src/parser/expr.rs
@@ -43,77 +43,6 @@ impl Expr<'_> {
pub(super) fn parse_arguments(i: &str) -> IResult<&str, Vec<Expr<'_>>> {
arguments(i)
}
-
- /// Returns `true` if enough assumptions can be made,
- /// to determine that `self` is copyable.
- pub(crate) fn is_copyable(&self) -> bool {
- self.is_copyable_within_op(false)
- }
-
- fn is_copyable_within_op(&self, within_op: bool) -> bool {
- use Expr::*;
- match self {
- BoolLit(_) | NumLit(_) | StrLit(_) | CharLit(_) => true,
- Unary(.., expr) => expr.is_copyable_within_op(true),
- BinOp(_, lhs, rhs) => {
- lhs.is_copyable_within_op(true) && rhs.is_copyable_within_op(true)
- }
- Range(..) => true,
- // The result of a call likely doesn't need to be borrowed,
- // as in that case the call is more likely to return a
- // reference in the first place then.
- Call(..) | Path(..) => true,
- // If the `expr` is within a `Unary` or `BinOp` then
- // an assumption can be made that the operand is copy.
- // If not, then the value is moved and adding `.clone()`
- // will solve that issue. However, if the operand is
- // implicitly borrowed, then it's likely not even possible
- // to get the template to compile.
- _ => within_op && self.is_attr_self(),
- }
- }
-
- /// Returns `true` if this is an `Attr` where the `obj` is `"self"`.
- pub(crate) fn is_attr_self(&self) -> bool {
- match self {
- Expr::Attr(obj, _) if matches!(obj.as_ref(), Expr::Var("self")) => true,
- Expr::Attr(obj, _) if matches!(obj.as_ref(), Expr::Attr(..)) => obj.is_attr_self(),
- _ => false,
- }
- }
-
- /// Returns `true` if the outcome of this expression may be used multiple times in the same
- /// `write!()` call, without evaluating the expression again, i.e. the expression should be
- /// side-effect free.
- pub(crate) fn is_cacheable(&self) -> bool {
- match self {
- // Literals are the definition of pure:
- Expr::BoolLit(_) => true,
- Expr::NumLit(_) => true,
- Expr::StrLit(_) => true,
- Expr::CharLit(_) => true,
- // fmt::Display should have no effects:
- Expr::Var(_) => true,
- Expr::Path(_) => true,
- // Check recursively:
- Expr::Array(args) => args.iter().all(|arg| arg.is_cacheable()),
- Expr::Attr(lhs, _) => lhs.is_cacheable(),
- Expr::Index(lhs, rhs) => lhs.is_cacheable() && rhs.is_cacheable(),
- Expr::Filter(_, args) => args.iter().all(|arg| arg.is_cacheable()),
- Expr::Unary(_, arg) => arg.is_cacheable(),
- Expr::BinOp(_, lhs, rhs) => lhs.is_cacheable() && rhs.is_cacheable(),
- Expr::Range(_, lhs, rhs) => {
- lhs.as_ref().map_or(true, |v| v.is_cacheable())
- && rhs.as_ref().map_or(true, |v| v.is_cacheable())
- }
- Expr::Group(arg) => arg.is_cacheable(),
- Expr::Tuple(args) => args.iter().all(|arg| arg.is_cacheable()),
- // We have too little information to tell if the expression is pure:
- Expr::Call(_, _) => false,
- Expr::RustMacro(_, _) => false,
- Expr::Try(_) => false,
- }
- }
}
fn expr_bool_lit(i: &str) -> IResult<&str, Expr<'_>> {