diff options
author | HampusM <hampus@hampusmat.com> | 2022-07-24 15:58:30 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-07-24 15:58:30 +0200 |
commit | 5335cdad99a6d566ea6e83f97012c7954ba93c45 (patch) | |
tree | b4c91634b34f6d3858d6dd210afb609d9a1f76d7 /examples/generics | |
parent | 5ea16d1e5bf716831bc8c54c5c4c86229d7e0463 (diff) |
feat: add support for generics
Diffstat (limited to 'examples/generics')
-rw-r--r-- | examples/generics/bootstrap.rs | 17 | ||||
-rw-r--r-- | examples/generics/interfaces/mod.rs | 1 | ||||
-rw-r--r-- | examples/generics/interfaces/printer.rs | 6 | ||||
-rw-r--r-- | examples/generics/main.rs | 19 | ||||
-rw-r--r-- | examples/generics/printer.rs | 24 |
5 files changed, 67 insertions, 0 deletions
diff --git a/examples/generics/bootstrap.rs b/examples/generics/bootstrap.rs new file mode 100644 index 0000000..072350e --- /dev/null +++ b/examples/generics/bootstrap.rs @@ -0,0 +1,17 @@ +use syrette::{di_container_bind, DIContainer}; + +// Interfaces +use crate::interfaces::printer::IPrinter; +// +// Implementations +use crate::printer::Printer; + +pub fn bootstrap() -> DIContainer +{ + let mut di_container = DIContainer::new(); + + di_container_bind!(IPrinter<String> => Printer, di_container); + di_container_bind!(IPrinter<i32> => Printer, di_container); + + di_container +} diff --git a/examples/generics/interfaces/mod.rs b/examples/generics/interfaces/mod.rs new file mode 100644 index 0000000..3f31bdf --- /dev/null +++ b/examples/generics/interfaces/mod.rs @@ -0,0 +1 @@ +pub mod printer; diff --git a/examples/generics/interfaces/printer.rs b/examples/generics/interfaces/printer.rs new file mode 100644 index 0000000..3098d0b --- /dev/null +++ b/examples/generics/interfaces/printer.rs @@ -0,0 +1,6 @@ +use std::fmt::Display; + +pub trait IPrinter<Printable: Display> +{ + fn print(&self, out: Printable); +} diff --git a/examples/generics/main.rs b/examples/generics/main.rs new file mode 100644 index 0000000..9442641 --- /dev/null +++ b/examples/generics/main.rs @@ -0,0 +1,19 @@ +mod bootstrap; +mod interfaces; +mod printer; + +use bootstrap::bootstrap; +use interfaces::printer::IPrinter; + +fn main() +{ + let di_container = bootstrap(); + + let string_printer = di_container.get::<dyn IPrinter<String>>().unwrap(); + + string_printer.print("Hello there".to_string()); + + let int_printer = di_container.get::<dyn IPrinter<i32>>().unwrap(); + + int_printer.print(2782028); +} diff --git a/examples/generics/printer.rs b/examples/generics/printer.rs new file mode 100644 index 0000000..4307c4f --- /dev/null +++ b/examples/generics/printer.rs @@ -0,0 +1,24 @@ +use std::fmt::Display; + +use syrette::injectable; + +use crate::interfaces::printer::IPrinter; + +pub struct Printer {} + +#[injectable] +impl Printer +{ + pub fn new() -> Self + { + Self {} + } +} + +impl<Printable: Display> IPrinter<Printable> for Printer +{ + fn print(&self, out: Printable) + { + println!("{}", out); + } +} |