Comments (13)
This is the expected behaviour of this example. Should i maybe add comments to explain it?
from syrette.
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.
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.
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.
Thank you so much @HampusMat
This is the best DI lib. I've already starred your repo
from syrette.
Btw, I think we also need to update:
Syrette/src/di_container/asynchronous/mod.rs
Line 249 in 3c993aa
from syrette.
Yes, I think right now we point to a struct directly, but the param requires Send + Sync
File to reproduce:
from syrette.
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.
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.
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.
Oh, and i added some comments to the prevent-circular example in 488faf9.
from syrette.
Thank you so much for the help! :)
from syrette.
Btw, I think we also need to update:
Syrette/src/di_container/asynchronous/mod.rs
Line 249 in 3c993aa
Why exactly? I can't see how that line would cause any problems.
from syrette.
Related Issues (20)
- Add support for stack allocation of injectables
- Make factories usable without nightly Rust
- Make tests not return Results HOT 1
- Make DependencyHistory more usable by dependants HOT 1
- Remove DI container interface traits HOT 1
- Expose DI containers's get_bound methods to public API HOT 1
- Make IFactory not take arguments as generic parameter HOT 1
- Add examples to DI container & related functions HOT 1
- IFactory is useless HOT 1
- Async constructors HOT 1
- Remove need to explicitly make injectables async compatible
- Implement Injectable & AsyncInjectable for types implementing Default HOT 1
- Find a way to get rid of Linkme dependency
- Add a way to bind a specific instance of a singleton HOT 3
- Remove need to nest factory passed to to_*factory binding builder functions in function
- Improve macro errors HOT 1
- Wrong quotes in docs of binding configurators HOT 1
- Add tagged bindings
- Add specifying constructor function HOT 1
- Make the blocking container thread-safe
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from syrette.