Skip to main content

Module data

Module data 

Source
Expand description

Utilities for task runtime data extension.

The Data type and related middleware are important for sharing state across tasks or layers within the same task.

§Overview

  • Data<T>: Wraps a value of type T for sharing across tasks.
  • AddExtension<S, T>: Middleware for injecting shared data into a task’s context.
  • MissingDataError: Error type for missing or unavailable data in a task context.

§Usage

Use Data to share application state (such as database connections, configuration, etc.) across tasks and layers. Apply this middleware using WorkerBuilder::data.

§Example


struct State { /* ... */ }

async fn email_service(email: String, state: Data<Arc<State>>) {
    // Use shared state here
}

let state = Arc::new(State { /* ... */ });
let worker = WorkerBuilder::new("tasty-avocado")
    .backend(MemoryStorage::new())
    .data(state)
    .build(email_service);

§Features

  • Type-safe access to shared data.
  • Integrated as middleware.
  • Error handling for missing data.

§See Also

  • FromRequest trait for extracting data from task contexts.
  • Task type representing a unit of work.

Structs§

AddExtension
Middleware for adding some shareable value to [request data].
Data
Extension data for tasks. This is commonly used to share state across tasks. or across layers within the same tasks

Enums§

MissingDataError
Error type for missing data in a task’s context.