Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example prevent-circular: Cannot resolve binding #1

Closed
dotai2012 opened this issue Nov 2, 2022 · 13 comments
Closed

Example prevent-circular: Cannot resolve binding #1

dotai2012 opened this issue Nov 2, 2022 · 13 comments
Labels
documentation Improvements or additions to documentation

Comments

@dotai2012
Copy link

dotai2012 commented Nov 2, 2022

Version: 0.4.1
Problem:

Error: BindingResolveFailed { reason: ResolveFailed { reason: BindingResolveFailed { reason: ResolveFailed { reason: BindingResolveFailed { reason: DetectedCircular { dependency_history: DependencyHistory { inner: ["creditor::Foo", "creditor::Bar", "creditor::Foo"] } }, interface: "creditor::Foo" }, affected: "creditor::Bar" }, interface: "creditor::Bar" }, affected: "creditor::Foo" }, interface: "creditor::Foo" }

How to reproduce: This is the code I copied exactly from the example (https://git.hampusmat.com/syrette/tree/examples/prevent-circular)

#![deny(clippy::all)]
#![deny(clippy::pedantic)]
#![allow(clippy::disallowed_names)]
use std::error::Error;

use syrette::di_container::blocking::prelude::*;
use syrette::injectable;
use syrette::ptr::TransientPtr;

struct Foo
{
    bar: TransientPtr<Bar>,
}

#[injectable]
impl Foo
{
    fn new(bar: TransientPtr<Bar>) -> Self
    {
        Self { bar }
    }
}

struct Bar
{
    foo: TransientPtr<Foo>,
}

#[injectable]
impl Bar
{
    fn new(foo: TransientPtr<Foo>) -> Self
    {
        Self { foo }
    }
}

fn main() -> Result<(), anyhow::Error>
{
    let mut di_container = DIContainer::new();

    di_container.bind::<Foo>().to::<Foo>()?;
    di_container.bind::<Bar>().to::<Bar>()?;

    let foo = di_container.get::<Foo>()?.transient()?;

    Ok(())
}
@HampusMat
Copy link
Owner

This is the expected behaviour of this example. Should i maybe add comments to explain it?

@HampusMat HampusMat added the documentation Improvements or additions to documentation label Nov 2, 2022
@dotai2012
Copy link
Author

Oh, I see, so it's expected, I think we can add a comment in the code 😅

Btw, how can I bind Bar to Bar (without a trait)

Something like:

struct Bar
{}

#[injectable]
impl Bar
{
    fn new() -> Self
    {
        Self { }
    }
}

fn main() -> Result<(), Box<dyn Error>>
{
    let mut di_container = DIContainer::new();

    di_container.bind::<Bar>().to::<Bar>()?;

    let bar = di_container.get::<Bar>()?.transient()?;

    Ok(())
}

I got an error:

Error: CastFailed { interface: "creditor::Bar", binding_kind: "transient" }

Or do I have to use the factory feature?

@HampusMat
Copy link
Owner

I honestly haven't though much about that use case. Using a trait should be preferred in my opinion. But if you for any reason would want to, you could use a default factory

@HampusMat HampusMat changed the title Cannot resolve binding Example prevent-circular: Cannot resolve binding Nov 2, 2022
@dotai2012
Copy link
Author

So my codebase has a global Config struct -> it doesn't have any methods (so no trait)

Other services will use that global config

This workflow is very easy to do in Go (Uber Fx or Google wire)

Could it be achievable without using the factory? (because of Rust nightly)

@HampusMat
Copy link
Owner

I realize now that it actually should be possible to bind a struct to itself. So i have no idea why it doesn't work. I will look into it a bit later today.

@HampusMat
Copy link
Owner

Basically what Syrette does deep down is it downcasts types using the downcast methods of either Box, Rc or Arc. In this case it would be Box.

And the downcast method of Box is basically:

if self.is::<T>() {
    unsafe {
        let (raw, alloc): (*mut dyn Any, _) =
            Box::into_raw_with_allocator(self);

        Ok(Box::from_raw_in(raw as *mut T, alloc))
    }
}
else {
    Err(self)
}

So it would maybe be if self.is::<T>() returns false. But that makes no sense as it is literally the same type.

But as i said, i will look into this later. I just wanted to quickly throw this out there.

@HampusMat
Copy link
Owner

I found the problem and fixed it in e282375.

It was because no interface was being declared. The injectable attribute macro declares the interface with the declare_interface macro when a interface argument is given to it. So because no interface argument was given, it didn't do that. So i made it by default declare the concrete type as the interface when no interface argument is given.

@HampusMat
Copy link
Owner

Oh, and i added some comments to the prevent-circular example in 488faf9.

@HampusMat
Copy link
Owner

Thank you so much for the help! :)

@dotai2012
Copy link
Author

Thank you so much @HampusMat

This is the best DI lib. I've already starred your repo

@dotai2012
Copy link
Author

Btw, I think we also need to update:

Interface: 'static + ?Sized + Send + Sync,

@HampusMat
Copy link
Owner

Btw, I think we also need to update:

Interface: 'static + ?Sized + Send + Sync,

Why exactly? I can't see how that line would cause any problems.

@dotai2012
Copy link
Author

Yes, I think right now we point to a struct directly, but the param requires Send + Sync

image

File to reproduce:

async.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants