shard

Attribute Macro shard 

Source
#[shard]
Expand description

Transforms a function into a shard component that can be navigated to via the routing system and (optionally) provided with a lazily‑initialized per‑shard state.

§Features

  • Generates a StructNameDestination (UpperCamelCase + Destination) implementing tessera_ui_shard::router::RouterDestination
  • (Optional) Injects a single #[state] parameter whose type:
    • Must implement Default + Send + Sync + 'static
    • Is constructed (or reused) and passed to your function body
  • Produces a stable shard ID: module_path!()::function_name

§Lifecycle

Controlled by the generated destination (via #[state(...)]).

  • Default: Shard – state is removed when the destination is pop()‑ed
  • Override: #[state(app)] (or #[state(application)]) – persist for the entire application

When pop() is called and the destination lifecycle is Shard, the registry entry is removed, freeing the state.

§Example

use tessera_ui::{shard, tessera};

#[tessera]
#[shard]
fn profile_page(#[state] state: ProfileState) {
    // Build your UI. You can navigate:
    // router::push(OtherPageDestination { ... });
}

#[derive(Default)]
struct ProfileState {
    // fields...
}

Pushing a shard:

use tessera_ui::router;
router::push(ProfilePageDestination { /* fields from fn params (excluding #[state]) */ });

§Parameter Transformation

  • At most one parameter may be annotated with #[state].
  • That parameter is removed from the generated function signature and supplied implicitly.
  • All other parameters remain explicit and become public fields on the generated *Destination struct.

§Generated Destination (Conceptual)

struct ProfilePageDestination { /* non-state params as public fields */ }
impl RouterDestination for ProfilePageDestination {
    fn exec_component(&self) { profile_page(/* fields */); }
    fn shard_id(&self) -> &'static str { "<module>::profile_page" }
}

§Limitations

  • No support for multiple #[state] params (compile panic if violated)
  • Do not manually implement RouterDestination for these pages; rely on generation

§See Also

  • Routing helpers: tessera_ui::router::{push, pop, router_root}
  • Shard state registry: tessera_ui_shard::ShardRegistry

§Safety

Internally uses an unsafe cast inside the registry to recover Arc<T> from Arc<dyn ShardState>; this is encapsulated and not exposed.

§Errors / Panics

  • Panics at compile time if multiple #[state] parameters are used or unsupported pattern forms are encountered.