# service_factory

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/api/ferroui-dependency-injection/macro-service_factory/

**macro** · [`ferroui-dependency-injection`](/docs/0.5.0/api/ferroui-dependency-injection/) · v0.5.0

```rust
macro_rules! service_factory
```

Writes a factory and its validation metadata from typed constructor parameters.

Parameters use `required(T)`, `optional(T)`, `all(T)`, `keyed(T, key)`,
`inherited(T)`, `key()` (the current `Option<ServiceKey>`), or `typed_key(T)`
(a cloned key of type `T`, checked at activation).
The body returns `Rc<S>`; use `ObjectFactory::new` for a fallible constructor.
```
## use ferroui_dependency_injection::*;
## use std::rc::Rc;
let factory = service_factory!(String, |name: required(str)| Rc::new(name.to_string()));
let descriptor = ServiceDescriptor::from_factory(ServiceLifetime::Transient, factory);
```

[View source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.DependencyInjection/activator_utilities.rs#L80)

## Availability

Extracted for `aarch64-apple-darwin`. Cargo features are listed in the [crate index](/docs/0.5.0/api/).
