Skip to main content
Version: dev

Module

std::meta::module contains methods on the built-in Module type which represents a module in the source program. Note that this type represents a module generally, it isn't limited to only mod my_submodule { ... } declarations in the source program.

Methods

add_item

add_item
pub comptime fn add_item(self, item: Quoted) {}

Source code: noir_stdlib/src/meta/module.nr#L5-L7

Adds a top-level item (a function, a struct, a global, etc.) to the module. Adding multiple items in one go is also valid if the Quoted value has multiple items in it. Note that the items are type-checked as if they are inside the module they are being added to.

child_modules

child_modules
pub comptime fn child_modules(self) -> [Module] {}

Source code: noir_stdlib/src/meta/module.nr#L30-L32

Returns all the child modules of the current module.

child_modules_example
mod my_module {
pub mod child1 {}
pub mod child2 {}
pub mod child3 {
pub mod nested_child {}
}
}

#[test]
fn child_modules_test() {
comptime {
let my_module = quote [my_module].as_module().unwrap();
let children = my_module.child_modules().map(Module::name);

// The order children are returned in is left unspecified.
assert_eq(children, &[quote [child1], quote [child2], quote [child3]]);
}
}

Source code: test_programs/compile_success_empty/comptime_module/src/main.nr#L150-L169

functions

functions
pub comptime fn functions(self) -> [FunctionDefinition] {}

Source code: noir_stdlib/src/meta/module.nr#L20-L22

Returns each function defined in the module.

has_named_attribute

has_named_attribute
pub comptime fn has_named_attribute<let N: u32>(self, name: str<N>) -> bool {}

Source code: noir_stdlib/src/meta/module.nr#L10-L12

Returns true if this module has a custom attribute with the given name.

is_contract

is_contract
pub comptime fn is_contract(self) -> bool {}

Source code: noir_stdlib/src/meta/module.nr#L15-L17

true if this module is a contract module (was declared via contract foo { ... }).

name

name
pub comptime fn name(self) -> Quoted {}

Source code: noir_stdlib/src/meta/module.nr#L35-L37

Returns the name of the module. The top-level module in each crate has no name and is thus empty.

parent

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

Source code: noir_stdlib/src/meta/module.nr#L40-L42

Returns the parent module of the given module, if any.

parent_example
mod module1 {
pub mod module2 {}
}

#[test]
fn parent_test() {
comptime {
let my_module2 = quote [module1::module2].as_module().unwrap();
assert_eq(my_module2.name(), quote [module2]);

let my_module1 = my_module2.parent().unwrap();
assert_eq(my_module1.name(), quote [module1]);

// The top-level module in each crate has no name
let top_level_module = my_module1.parent().unwrap();
assert_eq(top_level_module.name(), quote []);
}
}

Source code: test_programs/compile_success_empty/comptime_module/src/main.nr#L129-L148

structs

structs
pub comptime fn structs(self) -> [TypeDefinition] {}

Source code: noir_stdlib/src/meta/module.nr#L25-L27

Returns each struct defined in the module.

Trait Implementations

impl Eq for Module
impl Hash for Module

Note that each module is assigned a unique ID internally and this is what is used for equality and hashing. So even modules with identical names and contents may not be equal in this sense if they were originally different items in the source program.