Skip to main content

singleton

Attribute Macro singleton 

Source
#[singleton]
Expand description

Declares the annotated item as a singleton provider.

The instance of a singleton provider is constructed only once and shared by every resolution, so it can be resolved by reference or, when the type implements Clone, by value. The annotated type also gets an implementation of next_web_core::traits::singleton::Singleton returning the name of the provider.

The macro can be applied to a struct, an enum, an impl block or a fn:

  • on a struct, an enum or an impl block the annotated item is the provided type,
  • on a fn a struct named after the function is created and the provided type is the return type of the function. The struct carries no value, it only gives the provider a type of its own.

§Arguments

  • name = "...": name of the provider. On a fn it defaults to the name of the function in camel case, so fn test_name provides testName; on a struct it defaults to the type name with its first character lower cased, e.g. MyService becomes myService.
  • eager_create: construct the instance while the provider is registered instead of while it is resolved for the first time.
  • condition = path or condition = |cx| ...: a function or a closure taking &dyn ApplicationContext that decides whether the provider is inserted into the context.
  • binds = [path, ...]: expressions converting the instance into the types it is bound to, e.g. binds = [Self::into_service].
  • async: whether the constructor of a struct or an enum is async. Not supported on a fn, which is async when it is declared as async fn.
  • auto_register: whether the provider is submitted to the auto register registry, true by default. Generic items have to disable it with auto_register = false.
  • default: build the instance with Default::default() instead of resolving the fields of the annotated struct.

§Field and argument attributes

  • #[autowired(name = "...")]: name of the provider to resolve, which wins over the defaults below. Without it the name of the field or of the argument is used, so my_service resolves myService; a field or an argument without a name of its own uses the name of its type with its first character lower cased, so &MyService resolves myService.
  • #[autowired(option)]: the field is an Option<T> that is None when there is no provider with the given name.
  • #[autowired(default)]: the field uses Default::default() when there is no provider with the given name; #[autowired(default = expr)] uses the given expression instead.
  • #[autowired(vec)]: the field is a Vec<T> collecting every provider of T.
  • #[autowired(map)]: the field is a HashMap<String, T> naming every provider of T; T has to implement Singleton.
  • #[autowired(ref)]: the field takes a reference to the provider, which is required when a type alias hides the reference of the field type.
  • A field or an argument declared as a reference, e.g. &MyService or &mut MyService, is taken from the context as a reference to the instance and does not need #[autowired(ref)]; the instance is created in the context first when it does not exist yet. The context lends any number of immutable references, or a single mutable one, so a &mut field or argument cannot be combined with another reference.
  • #[value(key = "...")]: the field takes the value of the given application property.
  • #[resource(path = ...)]: path of the crate used by the generated code, ::next_web_context by default.

§Example

ⓘ
#[singleton(binds = [Self::into_service])]
#[derive(Clone)]
struct MyService {
    #[autowired(name = "myRepository")]
    repository: Arc<dyn Repository>,
}