{"id":13672382,"url":"https://github.com/nintha/autowired-rs","last_synced_at":"2025-04-27T21:32:56.987Z","repository":{"id":53152144,"uuid":"347385899","full_name":"nintha/autowired-rs","owner":"nintha","description":"rust dependency injection","archived":false,"fork":false,"pushed_at":"2021-07-05T01:35:36.000Z","size":35,"stargazers_count":12,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-07T01:07:00.266Z","etag":null,"topics":["autowired","dependency-injection","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/nintha.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-13T14:01:28.000Z","updated_at":"2024-10-25T01:58:42.000Z","dependencies_parsed_at":"2022-09-12T03:41:17.551Z","dependency_job_id":null,"html_url":"https://github.com/nintha/autowired-rs","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nintha%2Fautowired-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nintha%2Fautowired-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nintha%2Fautowired-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nintha%2Fautowired-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nintha","download_url":"https://codeload.github.com/nintha/autowired-rs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224087287,"owners_count":17253528,"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":["autowired","dependency-injection","rust"],"created_at":"2024-08-02T09:01:33.924Z","updated_at":"2024-11-11T10:30:43.347Z","avatar_url":"https://github.com/nintha.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# Autowired\n\n[![crates.io](https://img.shields.io/crates/v/autowired.svg)](https://crates.io/crates/autowired)\n[![docs.rs](https://docs.rs/autowired/badge.svg)](https://docs.rs/autowired)\n\nRust dependency injection project, inspired by `Spring IOC`.\n\n## Add Dependency\n\n```toml\n[dependencies]\nautowired=\"0.1\"\n```\n\n## Usage\n\nJust derive your struct with the marco `Component`, you can use the singleton component everywhere.\n\n```rust\n#[derive(Default, Component)]\nstruct Bar {\n    name: String,\n    age: u32,\n}\n\nfn main() {\n    // central registration in the beginning of the program\n    setup_submitted_beans();\n\n    // create `bar` via Default::default\n    let bar: Autowired\u003cBar\u003e = Autowired::new();\n\n    assert_eq!(String::default(), bar.name);\n    assert_eq!(u32::default(), bar.age);\n}\n```\n\nDefine custom component initialization logic\n\n```rust\nstruct Goo { pub list: Vec\u003cString\u003e }\n\n#[autowired::bean]\nfn build_goo() -\u003e Goo {\n    Goo { list: vec![\"hello\".to_string()] }\n}\n\nfn main() {\n    // central registration in the beginning of the program\n    setup_submitted_beans();\n\n    let goo = Autowired::\u003cGoo\u003e::new();\n    assert_eq!(\"hello\", goo.list[0])\n}\n```\n\n## Lazy components\n\nBy default, components are registered with `setup_submitted_beans`. \nUse `#[bean(lazy)]` to  register components lazily. The lazy components will be registered when be used.\n\n```rust\nuse std::sync::Arc;\nuse autowired::{ LazyComponent, setup_submitted_beans, bean, Autowired};\n\n#[allow(dead_code)]\n#[derive(Default, LazyComponent)]\nstruct Bar {\n    name: Arc\u003cString\u003e,\n    age: u32,\n}\n\n#[allow(dead_code)]\nstruct Goo { pub list: Vec\u003cString\u003e }\n\n#[bean(lazy)]\nfn build_goo() -\u003e Goo {\n    Goo { list: vec![\"hello\".to_string()] }\n}\n\n#[test]\nfn lazy() {\n    setup_submitted_beans();\n\n    assert!(!autowired::exist_component::\u003cBar\u003e());\n    assert!(!autowired::exist_component::\u003cGoo\u003e());\n\n    let bar = Autowired::\u003cBar\u003e::new();\n    assert!( bar.name.is_empty());\n\n    let goo = Autowired::\u003cGoo\u003e::new();\n    assert_eq!(\"hello\", goo.list[0]);\n\n    assert!(autowired::exist_component::\u003cBar\u003e());\n    assert!(autowired::exist_component::\u003cGoo\u003e());\n}\n```\n\n## Option components\n\nFunctional bean constructor can return `Option` with attribute `#[bean(option)]`.\n\nif return value is `None`, this bean will not be submitted.\n\nif you like, this feature can work with lazy components, `#[bean(option, lazy)]`.\n\n```rust\n#[allow(dead_code)]\nstruct Bar {\n    name: String,\n}\n\n/// return `None`, this bean will not be submitted\n#[bean(option)]\nfn build_bar_none() -\u003e Option\u003cBar\u003e {\n    None\n}\n\n#[allow(dead_code)]\nstruct Goo {\n    pub list: Vec\u003cString\u003e,\n}\n\n#[bean(option)]\nfn build_goo_some() -\u003e Option\u003cGoo\u003e {\n    Some(Goo { list: vec![\"hello\".to_string()] })\n}\n\n#[test]\nfn option() {\n    setup_submitted_beans();\n\n    assert!(!autowired::exist_component::\u003cBar\u003e());\n    assert!(autowired::exist_component::\u003cGoo\u003e());\n\n    let goo = Autowired::\u003cGoo\u003e::new();\n    assert_eq!(\"hello\", goo.list[0]);\n}\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnintha%2Fautowired-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnintha%2Fautowired-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnintha%2Fautowired-rs/lists"}