An open API service indexing awesome lists of open source software.

https://github.com/studiole/di

Rust libraries for dependency injection
https://github.com/studiole/di

Last synced: about 1 month ago
JSON representation

Rust libraries for dependency injection

Awesome Lists containing this project

README

          

# Dependency Injection for Rust

## Highlights

- Sync and Async constructors
- Resolve by type or trait
- Singleton or transient services

## Usage

### Define services

Implement `FromServices` to describe how a type is constructed from the container:

```rust
use studiole_di::prelude::*;

struct Config {
port: u16,
}

struct Database {
config: Arc,
}

impl FromServices for Database {
type Error = ResolveError;

fn from_services(services: &ServiceProvider) -> Result> {
let config = services.get::()?;
Ok(Self { config })
}
}
```

### Register and resolve

```rust
let services = ServiceBuilder::new()
.with_instance(Config { port: 8080 })
.with_type::()
.build();

let db = services.get::().expect("should resolve");
assert_eq!(db.config.port, 8080);
```

### Transient services

By default, services are singletons. Use the `_transient` variants for a fresh instance on every resolution:

```rust
let services = ServiceBuilder::new()
.with_type_transient::()
.build();
```

### Trait objects

*Requires nightly + `traits` feature*

Register a concrete type and resolve it as one or more trait objects:

```rust
let services = ServiceBuilder::new()
.with_trait::()
.with_trait::()
.build();

let cache = services.get_trait::().expect("should resolve");
```

Both trait registrations share the same concrete singleton.

### Async services

*Requires `async` feature*

Implement `FromServicesAsync` for services that need async construction:

```rust
struct AsyncDatabase {
config: Arc,
}

impl FromServicesAsync for AsyncDatabase {
type Error = ResolveError;

async fn from_services_async(
services: &ServiceProvider,
) -> Result> {
let config = services.get::()?;
Ok(Self { config })
}
}
```

```rust
let services = ServiceBuilder::new()
.with_instance(Config { port: 8080 })
.with_type_async::()
.build();

let db = services.get_async::().await.expect("should resolve");
```

Async trait objects work the same way with `with_trait_async` and `get_trait_async`.

## Migration

- [0.2 to 0.3](docs/migration-guides/0.2-to-0.3.md)

## License

This repository and its libraries are provided open source with the [AGPL-3.0](https://www.gnu.org/licenses/agpl-3.0.en.html) license that requires you must disclose your source code when you distribute, publish, or provide access to modified or derivative software.

Developers who wish to keep modified or derivative software proprietary or closed source can [get in touch for a commercial license agreements](https://studiole.uk/contact/)

> Copyright © Laurence Elsdon 2025-2026
>
> This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
>
> This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
>
> You should have received a copy of the GNU Affero General Public License along with this program. If not, see .

→ [GNU Affero General Public License](LICENSE.md)