aboutsummaryrefslogtreecommitdiff
path: root/examples/example-lazy-resources
diff options
context:
space:
mode:
Diffstat (limited to 'examples/example-lazy-resources')
-rw-r--r--examples/example-lazy-resources/Cargo.lock76
-rw-r--r--examples/example-lazy-resources/Cargo.toml8
-rw-r--r--examples/example-lazy-resources/page.toml10
-rw-r--r--examples/example-lazy-resources/src/main.rs92
4 files changed, 186 insertions, 0 deletions
diff --git a/examples/example-lazy-resources/Cargo.lock b/examples/example-lazy-resources/Cargo.lock
new file mode 100644
index 0000000..eb7d9b3
--- /dev/null
+++ b/examples/example-lazy-resources/Cargo.lock
@@ -0,0 +1,76 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "example-lazy-resources"
+version = "0.1.0"
+dependencies = [
+ "mingling",
+]
+
+[[package]]
+name = "just_fmt"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5454cda0d57db59778608d7a47bff5b16c6705598265869fb052b657f66cf05e"
+
+[[package]]
+name = "mingling"
+version = "0.2.0"
+dependencies = [
+ "mingling_core",
+ "mingling_macros",
+]
+
+[[package]]
+name = "mingling_core"
+version = "0.2.0"
+dependencies = [
+ "just_fmt",
+]
+
+[[package]]
+name = "mingling_macros"
+version = "0.2.0"
+dependencies = [
+ "just_fmt",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.106"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.45"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "syn"
+version = "2.0.117"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
diff --git a/examples/example-lazy-resources/Cargo.toml b/examples/example-lazy-resources/Cargo.toml
new file mode 100644
index 0000000..1f47f8e
--- /dev/null
+++ b/examples/example-lazy-resources/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "example-lazy-resources"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies.mingling]
+path = "../../mingling"
+features = []
diff --git a/examples/example-lazy-resources/page.toml b/examples/example-lazy-resources/page.toml
new file mode 100644
index 0000000..d2c740f
--- /dev/null
+++ b/examples/example-lazy-resources/page.toml
@@ -0,0 +1,10 @@
+[example]
+id = "example-lazy-resources"
+name = "Lazy Resources"
+icon = "💤️"
+category = "advanced"
+desc = """
+Example Lazy Resources
+"""
+tags = ["LazyRes<Res>"]
+files = ["src/main.rs", "Cargo.toml"]
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<Key, Value>,
+}
+
+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<Key, Value>);
+
+fn main() {
+ let mut program = ThisProgram::new();
+
+ // --------- IMPORTANT ---------
+ // _ Use lazy_init to create LazyRes<ResLargeData>
+ // /
+ // 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<ResLargeData>
+// | / instead of ResLargeData
+#[renderer] // vvvv vvvvvvvvvvvvvvvvvvvvv
+fn render_entry_show(_args: EntryShow, res: &mut LazyRes<ResLargeData>) {
+ // _______ Initialization happens here
+ // /
+ // vvvvvvv
+ let res = res.get_ref();
+ for (key, value) in &res.data {
+ r_println!("{}: {}", key, value);
+ }
+}
+
+// When not using LazyRes<ResLargeData>, it will not be initialized
+#[renderer]
+fn render_entry_none(_args: EntryNone) {
+ r_println!("None");
+}
+
+gen_program!();