Code Monkey home page Code Monkey logo

Comments (13)

HampusMat avatar HampusMat commented on June 16, 2024 1

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

from syrette.

dotai2012 avatar dotai2012 commented on June 16, 2024 1

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)

from syrette.

HampusMat avatar HampusMat commented on June 16, 2024 1

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.

from syrette.

HampusMat avatar HampusMat commented on June 16, 2024 1

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.

from syrette.

dotai2012 avatar dotai2012 commented on June 16, 2024 1

Thank you so much @HampusMat

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

from syrette.

dotai2012 avatar dotai2012 commented on June 16, 2024 1

Btw, I think we also need to update:

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

from syrette.

dotai2012 avatar dotai2012 commented on June 16, 2024 1

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

image

File to reproduce:

async.zip

from syrette.

dotai2012 avatar dotai2012 commented on June 16, 2024

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?

from syrette.

HampusMat avatar HampusMat commented on June 16, 2024

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

from syrette.

HampusMat avatar HampusMat commented on June 16, 2024

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.

from syrette.

HampusMat avatar HampusMat commented on June 16, 2024

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

from syrette.

HampusMat avatar HampusMat commented on June 16, 2024

Thank you so much for the help! :)

from syrette.

HampusMat avatar HampusMat commented on June 16, 2024

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.

from syrette.

Related Issues (20)

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.