concision_core/traits/
setup.rs

1/*
2   Appellation: setup <mod>
3   Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use core::borrow::{Borrow, BorrowMut};
6
7/// A trait used to denote objects that may be used for configuring various items
8pub trait Config {}
9
10/// [Configuration] describes composite configuration objects;
11/// A configuration object is allowed to inherit from another configuration object
12pub trait Configuration<C>
13where
14    C: Config,
15    Self::Config: Borrow<C>,
16{
17    type Config: Config;
18
19    fn root(&self) -> &C;
20
21    fn set(&mut self, config: Self::Config);
22
23    fn set_root(&mut self, config: C);
24}
25
26pub trait Init {
27    fn init(self) -> Self;
28}
29
30pub trait InitInplace {
31    fn init(&mut self);
32}
33
34pub trait Setup {
35    type Config;
36
37    fn setup(&mut self, config: Self::Config);
38}
39
40pub trait Context<C>
41where
42    C: Config,
43{
44    type Cnf: Configuration<C>;
45
46    fn config(&self) -> Self::Cnf;
47}
48
49/*
50 ************* Implementations *************
51*/
52
53impl<C, D> Configuration<C> for D
54where
55    C: Config,
56    D: Config + BorrowMut<C>,
57{
58    type Config = D;
59
60    fn root(&self) -> &C {
61        self.borrow()
62    }
63
64    fn set(&mut self, config: Self::Config) {
65        *self = config;
66    }
67
68    fn set_root(&mut self, config: C) {
69        *self.borrow_mut() = config;
70    }
71}