Created
March 4, 2015 13:50
-
-
Save borntyping/e869a3b57412fc171ecd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use std::fmt::Debug; | |
use std::rc::Rc; | |
// Resources ------------------------------------------------------------------- | |
trait Resource : Debug { } | |
trait Process : Resource + Debug { } | |
trait Service : Resource + Debug { } | |
#[derive(Debug)] | |
struct Resources { | |
// Why yes, this is ridiculous | |
// https://github.com/rust-lang/rust/issues/18248 | |
processes: HashMap<String, Rc<Box<Process>>>, | |
services: HashMap<String, Rc<Box<Service>>> | |
} | |
impl Resources { | |
fn new() -> Self { | |
Resources { | |
processes: HashMap::new(), | |
services: HashMap::new() | |
} | |
} | |
} | |
// Rules ----------------------------------------------------------------------- | |
trait Action : Debug { } | |
trait Condition : Debug { } | |
#[derive(Debug)] | |
struct Rule { | |
condition: Box<Condition> | |
} | |
/// Condition implementation | |
#[derive(Debug)] | |
pub struct Alive { | |
service: Rc<Box<Service>> | |
} | |
impl Condition for Alive { } | |
// Implmenentation ------------------------------------------------------------- | |
/// Resource implementation | |
#[derive(Debug)] | |
pub struct Supervisor { | |
name: String | |
} | |
impl Process for Supervisor { } | |
impl Service for Supervisor { } | |
impl Resource for Supervisor { } | |
fn main() { | |
let name = "starbound".to_string(); | |
let service = Supervisor { name: "starbound".to_string() }; | |
// Build the resource maps | |
let resources = { | |
let mut resources = Resources::new(); | |
resources.services.insert(name, Rc::new(Box::new(service))); | |
println!("Resources: {:?}", resources); | |
resources | |
}; | |
// Build a Rule | |
{ | |
let service = resources.services.get("starbound").unwrap(); | |
let condition = Alive { service: service.clone() }; | |
let rule = Rule { condition: Box::new(condition) }; | |
println!("{:?}", rule); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment