Quoted
std::meta::quoted
contains methods on the built-in Quoted
type which represents
quoted token streams and is the result of the quote { ... }
expression.
Methods
as_expr
pub comptime fn as_expr(self) -> Option<Expr> {}
Parses the quoted token stream as an expression. Returns Option::none()
if
the expression failed to parse.
Example:
#[test]
fn test_expr_as_function_call() {
comptime
{
let expr = quote { foo(42) }.as_expr().unwrap();
let (_function, args) = expr.as_function_call().unwrap();
assert_eq(args.len(), 1);
assert_eq(args[0].as_integer().unwrap(), (42, false));
}
}
Source code: test_programs/noir_test_success/comptime_expr/src/main.nr#L360-L371
as_module
pub comptime fn as_module(self) -> Option<Module> {}
Interprets this token stream as a module path leading to the name of a module.
Returns Option::none()
if the module isn't found or this token stream cannot be parsed as a path.
Example:
mod baz {
pub mod qux {}
}
#[test]
fn as_module_test() {
comptime {
let my_mod = quote { baz::qux }.as_module().unwrap();
assert_eq(my_mod.name(), quote { qux });
}
}
Source code: test_programs/compile_success_empty/comptime_module/src/main.nr#L115-L127
as_trait_constraint
pub comptime fn as_trait_constraint(self) -> TraitConstraint {}
Interprets this token stream as a trait constraint (without an object type).
Note that this function panics instead of returning Option::none()
if the token
stream does not parse and resolve to a valid trait constraint.
Example:
pub fn function_with_where<T>(_x: T)
where
T: SomeTrait<i32>,
{
comptime {
let t = quote { T }.as_type();
let some_trait_i32 = quote { SomeTrait<i32> }.as_trait_constraint();
assert(t.implements(some_trait_i32));
assert(t.get_trait_impl(some_trait_i32).is_none());
}
}
Source code: test_programs/compile_success_empty/comptime_type/src/main.nr#L160-L173
as_type
pub comptime fn as_type(self) -> Type {}
Interprets this token stream as a resolved type. Panics if the token stream doesn't parse to a type or if the type isn't a valid type in scope.
pub fn function_with_where<T>(_x: T)
where
T: SomeTrait<i32>,
{
comptime {
let t = quote { T }.as_type();
let some_trait_i32 = quote { SomeTrait<i32> }.as_trait_constraint();
assert(t.implements(some_trait_i32));
assert(t.get_trait_impl(some_trait_i32).is_none());
}
}
Source code: test_programs/compile_success_empty/comptime_type/src/main.nr#L160-L173
tokens
pub comptime fn tokens(self) -> [Quoted] {}
Returns a slice of the individual tokens that form this token stream.
Trait Implementations
impl Eq for Quoted
impl Hash for Quoted