pub trait Module:
Send
+ Sync
+ 'static {
// Required method
fn name(&self) -> &'static str;
// Provided methods
fn routes(&self) -> Routes { ... }
fn routes_with(&self, _contributions: &Contributions) -> Routes { ... }
fn migrations(&self) -> &'static [Migration] { ... }
fn register(&self, _app: &mut Registry) { ... }
}Expand description
A self-contained piece of an application: its routes and migrations, and later its jobs, policies and views.
pub struct Products;
impl Module for Products {
fn name(&self) -> &'static str { "products" }
fn routes(&self) -> Routes {
Routes::new()
.get("/products", index).name("products.index")
.get("/products/{id}", show).name("products.show")
}
}Required Methods§
Provided Methods§
Sourcefn routes_with(&self, _contributions: &Contributions) -> Routes
fn routes_with(&self, _contributions: &Contributions) -> Routes
The module’s routes, given everything modules contributed in
register (see Registry::contribute). Every register has run
by then, so module order does not matter. Calls Module::routes
by default.
struct Link(&'static str);
struct Menu;
impl Module for Menu {
fn name(&self) -> &'static str { "menu" }
fn routes_with(&self, contributions: &Contributions) -> Routes {
let _links = contributions.all::<Link>();
Routes::new()
}
}Sourcefn migrations(&self) -> &'static [Migration]
fn migrations(&self) -> &'static [Migration]
Migrations this module owns, e.g. renox::migrations!("src/app/products/migrations").
Sourcefn register(&self, _app: &mut Registry)
fn register(&self, _app: &mut Registry)
Registers the module’s jobs, event listeners and scheduled tasks.
fn register(&self, app: &mut Registry) {
app.job::<SendReceipt>()
.listen(|e: OrderPlaced, state| async move {
state.dispatch(SendReceipt { order_id: e.order_id }).await?;
Ok(())
});
app.schedule().daily_at("02:00", "close-day", close_day);
}Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".