Skip to content

Instantly share code, notes, and snippets.

@janhohenheim
Created April 30, 2019 15:04
Show Gist options
  • Select an option

  • Save janhohenheim/a3c751448bf3f049270d1cdc3f5368ed to your computer and use it in GitHub Desktop.

Select an option

Save janhohenheim/a3c751448bf3f049270d1cdc3f5368ed to your computer and use it in GitHub Desktop.
Dependency injection in Rust MVP
use core::any::TypeId;
use maplit::hashmap;
use std::collections::HashMap;
use std::rc::Rc;
fn main() {
let container = Container::new();
let _string: Rc<String> = container.resolve_shared();
let _foo: Rc<dyn Foo> = container.resolve_shared();
}
trait Resolvable<T>
where
T: ?Sized,
{
fn resolve_shared(&self) -> Rc<T>;
}
struct Container {
shared_items: HashMap<TypeId, ResolvableType>,
}
impl Container {
fn new() -> Self {
Self {
shared_items: hashmap! {
TypeId::of::<String>() => ResolvableType::String(Rc::new(String::new())),
TypeId::of::<Foo>() => ResolvableType::Foo(Rc::new(FooImpl::new()))
},
}
}
}
impl Resolvable<String> for Container {
fn resolve_shared(&self) -> Rc<String> {
let type_id = TypeId::of::<String>();
let resolvable_type = self
.shared_items
.get(&type_id)
.expect("No registered implementations of type String found");
match resolvable_type {
ResolvableType::String(value) => value.clone(),
_ => panic!(""),
}
}
}
impl Resolvable<Foo> for Container {
fn resolve_shared(&self) -> Rc<dyn Foo> {
let type_id = TypeId::of::<Foo>();
let resolvable_type = self
.shared_items
.get(&type_id)
.expect("No registered implementations of type Foo found");
match resolvable_type {
ResolvableType::Foo(value) => value.clone(),
_ => panic!(""),
}
}
}
enum ResolvableType {
String(Rc<String>),
Foo(Rc<dyn Foo>),
}
trait Foo {}
struct FooImpl {}
impl FooImpl {
fn new() -> Self {
FooImpl {}
}
}
impl Foo for FooImpl {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment