Coder Social home page Coder Social logo

zihantype / rudi Goto Github PK

View Code? Open in Web Editor NEW
72.0 72.0 3.0 260 KB

Rudi - an out-of-the-box dependency injection framework for Rust -- Rudi,一个开箱即用的 Rust 依赖注入框架

License: Apache License 2.0

Shell 0.27% Rust 99.73%
dependency dependency-injection di ioc

rudi's People

Contributors

zihantype avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rudi's Issues

Parentheses should be omitted when just use #[di()] in struct field.

In current version, If I need to inject a i32 for a struct field without specified dependency name, I need to type #[di()]

Here's my usecase:

#[Singleton]
async fn Number() -> i32 {
    452
}

#[Singleton(async)]
struct Config {
    #[di()]
    number: i32,
    #[di(default = 123)]
    test: u32,
}

It would be better to write it just as #[di].
Meanwhile, if I don't need to specify test to 123, I can just remove the #[di(default = 123)], and derive Default for Config, tell the provider to use u32::default() for initializing this field

The better syntax may be like this:

#[Singleton]
async fn Number() -> i32 {
    452
}

#[derive(Default)]
#[Singleton(async)]
struct Config {
    #[di]
    number: i32,
    test: u32,
}

Error handling

  • To better handle Errors during instantiation, use try_resolve_xxx or try_get_xxx to get the instance

eager_create configuration does not take effect

Example code:

use rudi::Context;
use rudi_macro::Singleton;

#[Singleton(eager_create)]
fn Run() {
    println!("Run()");
}


#[test]
fn singleton_owned() {
    Context::auto_register();

    Context::options().eager_create(false).auto_register();

    Context::options().eager_create(false).allow_only_single_eager_create(false).auto_register();


}

Anticipation:
Only the first one that defaults to true executes the Run, the rest of the ones specified as false do not execute anything with eager_create

image

The code in the red box in the figure and is seen to validate the eager_create configuration.

I also have a question for you.
Why is the function call in the red box in the above picture called twice in flash, is there any special mechanism? I'm sorry I don't have a thorough understanding of the project's source code, so I'd like to ask

Support for dependency's field injection.

When inject a specific field from a struct, for example, I think it would be better to code like this:

// Register async function and specify name
#[Singleton(name = "number")]
async fn Number() -> u32 {
    42
}

#[derive(Debug, Clone)]
#[Singleton(name = "config")] // Register async constructor and specify name
struct Config{
    #[di(name = "number")] // Specify the name of the dependency
    max_user: u32,
}

#[derive(Debug)]
struct Bar(i32);

impl Bar {
    fn into_debug(self) -> Rc<dyn Debug> {
        Rc::new(self)
    }
}

#[Transient(binds = [Self::into_debug])] // Bind the implementation of the `Debug` trait and the trait object of the `Debug` trait
impl Bar {
    async fn new(#[di(name = "config.max_user")] max_user: u32) -> Bar { // Register async constructor
        Bar(max_user)
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.