Skip to main content

entity_impl

Attribute Macro entity_impl 

Source
#[entity_impl]
Expand description

Attribute macro for entity impl blocks.

Each async fn annotated with #[rpc] or #[workflow] becomes an RPC handler. Unannotated async functions are treated as internal helpers.

§Attributes

  • #[entity_impl] — default crate path
  • #[entity_impl(krate = "crate")] — for internal use within cruster
  • #[entity_impl(traits(TraitA, TraitB))] — compose entity traits
  • #[entity_impl(deferred_keys(SIGNAL: i32 = "signal"))] — generate DeferredKey constants

§State Attribute

Use #[state(Type)] on the impl block to define per-instance persistent state:

#[entity_impl]
#[state(MyState)]
impl MyEntity {
    fn init(&self, ctx: &EntityContext) -> Result<MyState, ClusterError> {
        Ok(MyState::default())
    }

    #[rpc]
    async fn get_value(&self) -> Result<i32, ClusterError> {
        Ok(self.state.value)  // self.state is &MyState (read-only)
    }

    #[activity]
    async fn set_value(&mut self, value: i32) -> Result<(), ClusterError> {
        self.state.value = value;  // self.state is &mut MyState
        Ok(())  // State auto-persisted on activity completion
    }

    #[workflow]
    async fn do_set(&self, value: i32) -> Result<(), ClusterError> {
        self.set_value(value).await  // Workflows call activities
    }
}

The state type must implement Clone + Serialize + DeserializeOwned + Send + Sync.

§State Access Pattern

  • #[rpc] / #[workflow] methods use &self and access self.state as &State (read-only)
  • #[activity] methods use &mut self and access self.state as &mut State (mutable)
  • Activities are called from workflows via self.activity_name() (auto-delegated)