AzureMarker / shaku

Compile Time Dependency lnjection Library for Rust
https://docs.rs/shaku
Apache License 2.0
429 stars 22 forks source link

Support for owned, clonable components #21

Closed austinjones closed 3 years ago

austinjones commented 3 years ago

It would be really useful if you could define components that would be injected as Self, and cloned instead of shared with Arc. Many types in Rust internally maintain shared state behind an Arc, and can be cloned as cheaply as the Arc wrapper that Shaku provides.

A good example is mongodb::Client (more details in #20).

Here is an example with an invented client struct:

struct Shared { ... }

#[derive(Component, Clone)]
#[shaku(interface = Self)]
struct Connection { shared: Arc<Shared> }

module! {
    MyModule {
        components = [#[cloned] Connection],
        providers = []
    }
}
AzureMarker commented 3 years ago

I feel like the workaround of calling module.resolve_ref().clone() would work just fine. Why should this be special-cased? Can you give more details on your requirements; perhaps there is a better way to solve your problem (e.g. database pool).

AzureMarker commented 3 years ago

@austinjones Can you elaborate on your issue/proposal? Otherwise the issue will be closed.