{"id":13672129,"url":"https://github.com/udoprog/async-injector","last_synced_at":"2025-04-05T22:06:17.395Z","repository":{"id":56637447,"uuid":"199259169","full_name":"udoprog/async-injector","owner":"udoprog","description":" Reactive dependency injection for Rust.","archived":false,"fork":false,"pushed_at":"2025-03-29T10:58:28.000Z","size":169,"stargazers_count":42,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-29T21:05:41.017Z","etag":null,"topics":["async","dependency-injection","reconfigure","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/udoprog.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-28T07:57:31.000Z","updated_at":"2025-03-29T10:58:31.000Z","dependencies_parsed_at":"2024-03-03T15:32:20.943Z","dependency_job_id":"215049e0-b19f-4e0a-937b-1c0229bd0310","html_url":"https://github.com/udoprog/async-injector","commit_stats":null,"previous_names":[],"tags_count":38,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/udoprog%2Fasync-injector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/udoprog%2Fasync-injector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/udoprog%2Fasync-injector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/udoprog%2Fasync-injector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/udoprog","download_url":"https://codeload.github.com/udoprog/async-injector/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247406087,"owners_count":20933803,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","dependency-injection","reconfigure","rust"],"created_at":"2024-08-02T09:01:27.428Z","updated_at":"2025-04-05T22:06:17.377Z","avatar_url":"https://github.com/udoprog.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# async-injector\n\n[\u003cimg alt=\"github\" src=\"https://img.shields.io/badge/github-udoprog/async--injector-8da0cb?style=for-the-badge\u0026logo=github\" height=\"20\"\u003e](https://github.com/udoprog/async-injector)\n[\u003cimg alt=\"crates.io\" src=\"https://img.shields.io/crates/v/async-injector.svg?style=for-the-badge\u0026color=fc8d62\u0026logo=rust\" height=\"20\"\u003e](https://crates.io/crates/async-injector)\n[\u003cimg alt=\"docs.rs\" src=\"https://img.shields.io/badge/docs.rs-async--injector-66c2a5?style=for-the-badge\u0026logoColor=white\u0026logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K\" height=\"20\"\u003e](https://docs.rs/async-injector)\n[\u003cimg alt=\"build status\" src=\"https://img.shields.io/github/actions/workflow/status/udoprog/async-injector/ci.yml?branch=main\u0026style=for-the-badge\" height=\"20\"\u003e](https://github.com/udoprog/async-injector/actions?query=branch%3Amain)\n\nAsynchronous dependency injection for Rust.\n\nThis library provides the glue which allows for building robust decoupled\napplications that can be reconfigured dynamically while they are running.\n\nFor a real world example of how this is used, see [`OxidizeBot`] for which\nit was written.\n\n\u003cbr\u003e\n\n## Usage\n\nAdd `async-injector` to your `Cargo.toml`.\n\n```toml\n[dependencies]\nasync-injector = \"0.19.4\"\n```\n\n\u003cbr\u003e\n\n## Example\n\nIn the following we'll showcase the injection of a *fake* `Database`. The\nidea here would be that if something about the database connection changes,\na new instance of `Database` would be created and cause the application to\nreconfigure itself.\n\n```rust\nuse async_injector::{Key, Injector, Provider};\n\n#[derive(Debug, Clone)]\nstruct Database;\n\n#[derive(Debug, Provider)]\nstruct Service {\n    #[dependency]\n    database: Database,\n}\n\nasync fn service(injector: Injector) -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    let mut provider = Service::provider(\u0026injector).await?;\n\n    let Service { database } = provider.wait().await;\n    println!(\"Service got initial database {database:?}!\");\n\n    let Service { database } = provider.wait().await;\n    println!(\"Service got new database {database:?}!\");\n\n    Ok(())\n}\n```\n\n\u003e **Note:** This is available as the `database` example:\n\u003e ```sh\n\u003e cargo run --example database\n\u003e ```\n\nThe [`Injector`] above provides a structured broadcasting system that allows\nfor configuration updates to be cleanly integrated into asynchronous\ncontexts. The update itself is triggered by some other component that is\nresponsible for constructing the `Database` instance.\n\nBuilding up the components of your application like this means that it can\nbe reconfigured without restarting it. Providing a much richer user\nexperience.\n\n\u003cbr\u003e\n\n## Injecting multiple things of the same type\n\nIn the previous section you might've noticed that the injected value was\nsolely discriminated by its type: `Database`. In this example we'll show how\n[`Key`] can be used to *tag* values of the same type with different names to\ndiscriminate them. This can be useful when dealing with overly generic types\nlike [`String`].\n\nThe tag used must be serializable with [`serde`]. It must also not use any\ncomponents which [cannot be hashed], like `f32` and `f64`.\n\n\u003cbr\u003e\n\n### A simple greeter\n\nThe following example showcases the use of `Key` to injector two different\nvalues into an asynchronous `greeter`.\n\n```rust\nuse async_injector::{Key, Injector};\n\nasync fn greeter(injector: Injector) -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    let name = Key::\u003cString\u003e::tagged(\"name\")?;\n    let fun = Key::\u003cString\u003e::tagged(\"fun\")?;\n\n    let (mut name_stream, mut name) = injector.stream_key(name).await;\n    let (mut fun_stream, mut fun) = injector.stream_key(fun).await;\n\n    loop {\n        tokio::select! {\n            update = name_stream.recv() =\u003e {\n                name = update;\n            }\n            update = fun_stream.recv() =\u003e {\n                fun = update;\n            }\n        }\n\n        let (Some(name), Some(fun)) = (\u0026name, \u0026fun) else {\n            continue;\n        };\n\n        println!(\"Hi {name}! I see you do \\\"{fun}\\\" for fun!\");\n        return Ok(());\n    }\n}\n```\n\n\u003e **Note:** you can run this using:\n\u003e ```sh\n\u003e cargo run --example greeter\n\u003e ```\n\nThe loop above can be implemented more easily using the [`Provider`] derive,\nso let's do that.\n\n```rust\nuse async_injector::{Injector, Provider};\n\n#[derive(Provider)]\nstruct Dependencies {\n    #[dependency(tag = \"name\")]\n    name: String,\n    #[dependency(tag = \"fun\")]\n    fun: String,\n}\n\nasync fn greeter(injector: Injector) -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    let mut provider = Dependencies::provider(\u0026injector).await?;\n    let Dependencies { name, fun } = provider.wait().await;\n    println!(\"Hi {name}! I see you do \\\"{fun}\\\" for fun!\");\n    Ok(())\n}\n```\n\n\u003e **Note:** you can run this using:\n\u003e ```sh\n\u003e cargo run --example greeter_provider\n\u003e ```\n\n\u003cbr\u003e\n\n## The `Provider` derive\n\nThe [`Provider`] derive can be used to conveniently implement the mechanism\nnecessary to wait for a specific set of dependencies to become available.\n\nIt builds a companion structure next to the type being provided called\n`\u003cname\u003eProvider` which in turn implements the following set of methods:\n\n```rust\nuse async_injector::{Error, Injector};\n\nimpl Dependencies {\n    /// Construct a new provider.\n    async fn provider(injector: \u0026Injector) -\u003e Result\u003cDependenciesProvider, Error\u003e\n}\n\nstruct DependenciesProvider {\n    /* private fields */\n}\n\nimpl DependenciesProvider {\n    /// Try to construct the current value. Returns [None] unless all\n    /// required dependencies are available.\n    fn build(\u0026mut self) -\u003e Option\u003cDependencies\u003e\n\n    /// Wait until we can successfully build the complete provided\n    /// value.\n    async fn wait(\u0026mut self) -\u003e Dependencies\n\n    /// Wait until the provided value has changed. Either some\n    /// dependencies are no longer available at which it returns `None`,\n    /// or all dependencies are available after which we return the\n    /// build value.\n    async fn wait_for_update(\u0026mut self) -\u003e Option\u003cDependencies\u003e\n}\n```\n\n\u003cbr\u003e\n\n### Fixed arguments to `Provider`\n\nAny arguments which do not have the `#[dependency]` attribute are known as\n\"fixed\" arguments. These must be passed in when calling the `provider`\nconstructor. They can also be used during tag construction.\n\n```rust\nuse async_injector::{Injector, Key, Provider};\n\n#[derive(Provider)]\nstruct Dependencies {\n    name_tag: \u0026'static str,\n    #[dependency(tag = name_tag)]\n    name: String,\n}\n\nasync fn greeter(injector: Injector) -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    let mut provider = Dependencies::provider(\u0026injector, \"name\").await?;\n    let Dependencies { name, .. } = provider.wait().await;\n    println!(\"Hi {name}!\");\n    Ok(())\n}\n```\n\n[`OxidizeBot`]: https://github.com/udoprog/OxidizeBot\n[cannot be hashed]: https://internals.rust-lang.org/t/f32-f64-should-implement-hash/5436\n[`Injector`]: https://docs.rs/async-injector/0/async_injector/struct.Injector.html\n[`Key`]: https://docs.rs/async-injector/0/async_injector/struct.Key.html\n[`Provider`]: https://docs.rs/async-injector/0/async_injector/derive.Provider.html\n[`serde`]: https://serde.rs\n[`Stream`]: https://docs.rs/futures-core/0/futures_core/stream/trait.Stream.html\n[`String`]: https://doc.rust-lang.org/std/string/struct.String.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fudoprog%2Fasync-injector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fudoprog%2Fasync-injector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fudoprog%2Fasync-injector/lists"}