teloc icon indicating copy to clipboard operation
teloc copied to clipboard

Example still fails on latest Rust

Open DanielJoyce opened this issue 3 years ago • 2 comments

use teloc::*;

// Declare your structs
struct ConstService {
    number: i32,
}
// #[inject] macro is indicate that dependency can be constructed using this
// function
#[inject]
impl ConstService {
    pub fn new(number: i32) -> Self {
        ConstService { number }
    }
}

// Derive macro can be used when all fields implement `Dependency` trait, but
// we recommend using the #[inject] macro it in production code instead.
#[derive(Dependency)]
struct Controller {
    number_service: ConstService,
}

fn main() {
    // Create `ServiceProvider` struct that store itself all dependencies
    let sp = ServiceProvider::new()
        // Add dependency with `Singleton` lifetime. More about lifetimes see above.
        .add_singleton::<ConstService>()
        // Add dependency with `Transient` lifetime. More about lifetimes see above.
        .add_transient::<Controller>()
        .add_instance(10);
    // Fork `ServiceProvider`. It creates a new `ServiceProvider` which will have
    // access to the dependencies from parent `ServiceProvider`.
    let scope = sp
        // .fork() method creates a local mutable scope with self parent immutable `ServiceProvider`.
        .fork()
        // Add an instance of `i32` that will be used when `ConstService` will be initialized.
        .add_instance(10);
    // Get dependency from `ServiceProvider`
    let controller: Controller = scope.resolve();  // fails here on resolve()
    assert_eq!(controller.number_service.number, 10);
}

Produces:

the trait bound `teloc::ServiceProvider<&teloc::ServiceProvider<teloc::service_provider::EmptyServiceProvider, HCons<InstanceContainer<{integer}>, HCons<TransientContainer<Controller>, HCons<SingletonContainer<ConstService>, HNil>>>>, HCons<InstanceContainer<{integer}>, HNil>>: teloc::Resolver<'_, _, ConstService, _>` is not satisfied
required because of the requirements on the impl of `GetDependencies<'_, HCons<ConstService, HNil>, HCons<(_, _), HNil>>` for `teloc::ServiceProvider<&teloc::ServiceProvider<teloc::service_provider::EmptyServiceProvider, HCons<InstanceContainer<{integer}>, HCons<TransientContainer<Controller>, HCons<SingletonContainer<ConstService>, HNil>>>>, HCons<InstanceContainer<{integer}>, HNil>>`
required because of the requirements on the impl of `teloc::Resolver<'_, TransientContainer<Controller>, Controller, (teloc::index::ParentIndex<teloc::index::SelfIndex<There<Here>>>, HCons<ConstService, HNil>, HCons<(_, _), HNil>)>` for `teloc::ServiceProvider<&teloc::ServiceProvider<teloc::service_provider::EmptyServiceProvider, HCons<InstanceContainer<{integer}>, HCons<TransientContainer<Controller>, HCons<SingletonContainer<ConstService>, HNil>>>>, HCons<InstanceContainer<{integer}>, HNil>>`rustc[E0277](https://doc.rust-lang.org/error-index.html#E0277)

❯ rustc --version
rustc 1.58.1 (db9d1b20b 2022-01-20)

DanielJoyce avatar Feb 11 '22 18:02 DanielJoyce

Maybe this issue?

https://github.com/AzureMarker/shaku/issues/30

Regression in rust compiler, fixed in 1.59 supposedly?

DanielJoyce avatar Feb 11 '22 18:02 DanielJoyce

I was able to repo on both 1.58.1 and 1.61.0. I'll work on a fix to the example

aburkhalter512 avatar May 28 '22 04:05 aburkhalter512