Expr
std::meta::expr
contains methods on the built-in Expr
type for quoted, syntactically valid expressions.
Methods
as_array
fn as_array(self) -> Option<[Expr]> {}
If this expression is an array, this returns a slice of each element in the array.
as_assert
fn as_assert(self) -> Option<(Expr, Option<Expr>)> {}
If this expression is an assert, this returns the assert expression and the optional message.
as_assert_eq
fn as_assert_eq(self) -> Option<(Expr, Expr, Option<Expr>)> {}
If this expression is an assert_eq, this returns the left-hand-side and right-hand-side expressions, together with the optional message.
as_assign
fn as_assign(self) -> Option<(Expr, Expr)> {}
If this expression is an assignment, this returns a tuple with the left hand side and right hand side in order.
as_binary_op
fn as_binary_op(self) -> Option<(Expr, BinaryOp, Expr)> {}
If this expression is a binary operator operation <lhs> <op> <rhs>
,
return the left-hand side, operator, and the right-hand side of the operation.
as_block
fn as_block(self) -> Option<[Expr]> {}
If this expression is a block { stmt1; stmt2; ...; stmtN }
, return
a slice containing each statement.
as_bool
fn as_bool(self) -> Option<bool> {}
If this expression is a boolean literal, return that literal.
as_comptime
fn as_comptime(self) -> Option<[Expr]> {}
If this expression is a comptime { stmt1; stmt2; ...; stmtN }
block,
return each statement in the block.
as_function_call
fn as_function_call(self) -> Option<(Expr, [Expr])> {}
If this expression is a function call foo(arg1, ..., argN)
, return
the function and a slice of each argument.
as_if
fn as_if(self) -> Option<(Expr, Expr, Option<Expr>)> {}
If this expression is an if condition { then_branch } else { else_branch }
,
return the condition, then branch, and else branch. If there is no else branch,
None
is returned for that branch instead.
as_index
fn as_index(self) -> Option<(Expr, Expr)> {}
If this expression is an index into an array array[index]
, return the
array and the index.
as_integer
fn as_integer(self) -> Option<(Field, bool)> {}
If this element is an integer literal, return the integer as a field as well as whether the integer is negative (true) or not (false).
as_member_access
fn as_member_access(self) -> Option<(Expr, Quoted)> {}
If this expression is a member access foo.bar
, return the struct/tuple
expression and the field. The field will be represented as a quoted value.
as_method_call
fn as_method_call(self) -> Option<(Expr, Quoted, [UnresolvedType], [Expr])> {}
If this expression is a method call foo.bar::<generic1, ..., genericM>(arg1, ..., argN)
, return
the receiver, method name, a slice of each generic argument, and a slice of each argument.
as_repeated_element_array
fn as_repeated_element_array(self) -> Option<(Expr, Expr)> {}
If this expression is a repeated element array [elem; length]
, return
the repeated element and the length expressions.
as_repeated_element_slice
fn as_repeated_element_slice(self) -> Option<(Expr, Expr)> {}
If this expression is a repeated element slice [elem; length]
, return
the repeated element and the length expressions.
as_slice
fn as_slice(self) -> Option<[Expr]> {}
If this expression is a slice literal &[elem1, ..., elemN]
,
return each element of the slice.
as_tuple
fn as_tuple(self) -> Option<[Expr]> {}
If this expression is a tuple (field1, ..., fieldN)
,
return each element of the tuple.
as_unary_op
fn as_unary_op(self) -> Option<(UnaryOp, Expr)> {}
If this expression is a unary operation <op> <rhs>
,
return the unary operator as well as the right-hand side expression.
as_unsafe
fn as_unsafe(self) -> Option<[Expr]> {}
If this expression is an unsafe { stmt1; ...; stmtN }
block,
return each statement inside in a slice.
has_semicolon
fn has_semicolon(self) -> bool {}
true
if this expression is trailed by a semicolon. E.g.
comptime {
let expr1 = quote { 1 + 2 }.as_expr().unwrap();
let expr2 = quote { 1 + 2; }.as_expr().unwrap();
assert(expr1.as_binary_op().is_some());
assert(expr2.as_binary_op().is_some());
assert(!expr1.has_semicolon());
assert(expr2.has_semicolon());
}
is_break
fn is_break(self) -> bool {}
true
if this expression is break
.
is_continue
fn is_continue(self) -> bool {}
true
if this expression is continue
.
modify
fn modify<Env>(self, f: fn[Env](Expr) -> Option<Expr>) -> Expr {
Applies a mapping function to this expression and to all of its sub-expressions.
f
will be applied to each sub-expression first, then applied to the expression itself.
This happens recursively for every expression within self
.
For example, calling modify
on (&[1], &[2, 3])
with an f
that returns Option::some
for expressions that are integers, doubling them, would return (&[2], &[4, 6])
.
quoted
fn quoted(self) -> Quoted {
Returns this expression as a Quoted
value. It's the same as quote { $self }
.
resolve
fn resolve(self, in_function: Option<FunctionDefinition>) -> TypedExpr {}
Resolves and type-checks this expression and returns the result as a TypedExpr
.
The in_function
argument specifies where the expression is resolved:
- If it's
none
, the expression is resolved in the function whereresolve
was called - If it's
some
, the expression is resolved in the given function
If any names used by this expression are not in scope or if there are any type errors,
this will give compiler errors as if the expression was written directly into
the current comptime
function.