From 9725bbc5a41e00a2b952cdfe4a04d472765c9e41 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sun, 7 Jun 2026 19:42:02 +0800 Subject: Add LazyRes for lazy resource initialization --- examples/example-lazy-resources/src/main.rs | 92 +++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 examples/example-lazy-resources/src/main.rs (limited to 'examples/example-lazy-resources/src') diff --git a/examples/example-lazy-resources/src/main.rs b/examples/example-lazy-resources/src/main.rs new file mode 100644 index 0000000..3533ae2 --- /dev/null +++ b/examples/example-lazy-resources/src/main.rs @@ -0,0 +1,92 @@ +//! Example Lazy Resources +//! +//! > This example demonstrates how to use `LazyRes` for lazy resource initialization. +//! +//! Run: +//! ```bash +//! cargo run --manifest-path examples/example-lazy-resources/Cargo.toml --quiet none +//! +//! cargo run --manifest-path examples/example-lazy-resources/Cargo.toml --quiet show +//! ``` +//! +//! Output: +//! ```plaintext +//! None +//! +//! Initialized +//! foo: bar +//! rust: lang +//! baz: qux +//! hello: world +//! key: value +//! ``` + +use std::collections::BTreeMap; + +use mingling::{LazyInit, LazyRes, prelude::*}; + +type Key = String; +type Value = String; + +// Define a resource that requires time-consuming initialization +#[derive(Default, Clone)] +pub struct ResLargeData { + pub data: BTreeMap, +} + +fn init_res_large_data() -> ResLargeData { + // Perform time-consuming initialization here + let mut data = BTreeMap::new(); + data.insert("foo".to_string(), "bar".to_string()); + data.insert("baz".to_string(), "qux".to_string()); + data.insert("hello".to_string(), "world".to_string()); + data.insert("rust".to_string(), "lang".to_string()); + data.insert("key".to_string(), "value".to_string()); + + // Print to indicate initialization is complete + println!("Initialized"); + ResLargeData { data } +} + +dispatcher!("show", CMDShow => EntryShow); +dispatcher!("none", CMDNone => EntryNone); + +pack!(ResultShow = BTreeMap); + +fn main() { + let mut program = ThisProgram::new(); + + // --------- IMPORTANT --------- + // _ Use lazy_init to create LazyRes + // / + // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv + program.with_resource(ResLargeData::lazy_init(init_res_large_data)); + // --------- IMPORTANT --------- + + program.with_dispatchers((CMDShow, CMDNone)); + program.exec_and_exit(); +} + +// Inject LazyRes instead of a normal resource +// __________________________ Must use &mut because `get_ref` and `get_mut` +// / both require mutable borrow +// | _____________________ Use LazyRes +// | / instead of ResLargeData +#[renderer] // vvvv vvvvvvvvvvvvvvvvvvvvv +fn render_entry_show(_args: EntryShow, res: &mut LazyRes) { + // _______ Initialization happens here + // / + // vvvvvvv + let res = res.get_ref(); + for (key, value) in &res.data { + r_println!("{}: {}", key, value); + } +} + +// When not using LazyRes, it will not be initialized +#[renderer] +fn render_entry_none(_args: EntryNone) { + r_println!("None"); +} + +gen_program!(); -- cgit