use super::{Container, TEST_CONTAINER};
use std::any::Any;
use std::sync::Arc;
pub struct TestContainer;
impl TestContainer {
pub fn fake() -> TestContainerGuard {
TEST_CONTAINER.with(|c| {
*c.borrow_mut() = Some(Container::new());
});
TestContainerGuard
}
pub fn singleton<T: Any + Send + Sync + 'static>(instance: T) {
TEST_CONTAINER.with(|c| {
if let Some(ref mut container) = *c.borrow_mut() {
container.singleton(instance);
}
});
}
pub fn factory<T, F>(factory: F)
where
T: Any + Send + Sync + 'static,
F: Fn() -> T + Send + Sync + 'static,
{
TEST_CONTAINER.with(|c| {
if let Some(ref mut container) = *c.borrow_mut() {
container.factory(factory);
}
});
}
pub fn bind<T: ?Sized + Send + Sync + 'static>(instance: Arc<T>) {
TEST_CONTAINER.with(|c| {
if let Some(ref mut container) = *c.borrow_mut() {
container.bind(instance);
}
});
}
pub fn bind_factory<T: ?Sized + Send + Sync + 'static, F>(factory: F)
where
F: Fn() -> Arc<T> + Send + Sync + 'static,
{
TEST_CONTAINER.with(|c| {
if let Some(ref mut container) = *c.borrow_mut() {
container.bind_factory(factory);
}
});
}
}
pub struct TestContainerGuard;
impl Drop for TestContainerGuard {
fn drop(&mut self) {
TEST_CONTAINER.with(|c| {
*c.borrow_mut() = None;
});
}
}