Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,33 @@ Example:
```rust
use teloc::*;

struct ConstServiceConfig {
config_value: i32,
}

// Declare your structs
struct ConstService {
number: i32,
struct ConstService<'a> {
config: &'a ConstServiceConfig,
}
// #[inject] macro is indicate that dependency can be constructed using this function

// #[inject] macro is indicate that dependency can be constructed using this
// function
#[inject]
impl ConstService {
pub fn new(number: i32) -> Self {
ConstService { number }
impl<'a> ConstService<'a> {
pub fn new(config: &'a ConstServiceConfig) -> Self {
ConstService { config }
}
}

// Derive macro can be used when all fields implement `Dependency` trait, but
// Derive macro can be used when all fields implement `Dependency` trait, but
// we recommend using the #[inject] macro it in production code instead.
#[derive(Dependency)]
struct Controller {
number_service: ConstService,
struct Controller<'a> {
number_service: &'a ConstService<'a>,
}

fn main() {
#[test]
fn test() {
// Create `ServiceProvider` struct that store itself all dependencies
let sp = ServiceProvider::new()
// Add dependency with `Singleton` lifetime. More about lifetimes see above.
Expand All @@ -90,10 +97,10 @@ fn main() {
// .fork() method creates a local mutable scope with self parent immutable `ServiceProvider`.
.fork()
// Add an instance of `i32` that will be used when `ConstService` will be initialized.
.add_instance(10);
.add_instance(ConstServiceConfig { config_value: 10 });
// Get dependency from `ServiceProvider`
let controller: Controller = scope.resolve();
assert_eq!(controller.number_service.number, 10);
let controller: Controller = scope.resolve(); // fails here on resolve()
assert_eq!(controller.number_service.config.config_value, 10);
}
```

Expand Down
46 changes: 46 additions & 0 deletions teloc/tests/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use teloc::*;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this test to ensure that the code in the example compiles

Copy link
Owner

@p0lunin p0lunin Jul 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can test readme examples using such hack (taken from teloxide):

#[cfg(all(feature = "nightly", doctest))]
#[cfg_attr(feature = "nightly", cfg_attr(feature = "nightly", doc = include_str!("../README.md")))]
enum ReadmeDocTests {}


struct ConstServiceConfig {
config_value: i32,
}

// Declare your structs
struct ConstService<'a> {
config: &'a ConstServiceConfig,
}

// #[inject] macro is indicate that dependency can be constructed using this
// function
#[inject]
impl<'a> ConstService<'a> {
pub fn new(config: &'a ConstServiceConfig) -> Self {
ConstService { config }
}
}

// Derive macro can be used when all fields implement `Dependency` trait, but
// we recommend using the #[inject] macro it in production code instead.
#[derive(Dependency)]
struct Controller<'a> {
number_service: &'a ConstService<'a>,
}

#[test]
fn test() {
// Create `ServiceProvider` struct that store itself all dependencies
let sp = ServiceProvider::new()
// Add dependency with `Singleton` lifetime. More about lifetimes see above.
.add_singleton::<ConstService>()
// Add dependency with `Transient` lifetime. More about lifetimes see above.
.add_transient::<Controller>();
// Fork `ServiceProvider`. It creates a new `ServiceProvider` which will have
// access to the dependencies from parent `ServiceProvider`.
let scope = sp
// .fork() method creates a local mutable scope with self parent immutable `ServiceProvider`.
.fork()
// Add an instance of `i32` that will be used when `ConstService` will be initialized.
.add_instance(ConstServiceConfig { config_value: 10 });
// Get dependency from `ServiceProvider`
let controller: Controller = scope.resolve(); // fails here on resolve()
assert_eq!(controller.number_service.config.config_value, 10);
}