Skip to main content
Version: v0.35.0

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

as_expr
comptime fn as_expr(self) -> Option<Expr> {}

Source code: noir_stdlib/src/meta/quoted.nr#L6-L8

Parses the quoted token stream as an expression. Returns Option::none() if the expression failed to parse.

Example:

as_expr_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

as_module
comptime fn as_module(self) -> Option<Module> {}

Source code: noir_stdlib/src/meta/quoted.nr#L11-L13

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:

as_module_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#L116-L129

as_trait_constraint

as_trait_constraint
comptime fn as_trait_constraint(self) -> TraitConstraint {}

Source code: noir_stdlib/src/meta/quoted.nr#L16-L18

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:

implements_example
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#L154-L165

as_type

as_type
comptime fn as_type(self) -> Type {}

Source code: noir_stdlib/src/meta/quoted.nr#L21-L23

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.

implements_example
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#L154-L165

tokens

tokens
comptime fn tokens(self) -> [Quoted] {}

Source code: noir_stdlib/src/meta/quoted.nr#L26-L28

Returns a slice of the individual tokens that form this token stream.

Trait Implementations

impl Eq for Quoted
impl Hash for Quoted