From bb965413a86295a3d98b95132015b5f11627bc1e Mon Sep 17 00:00:00 2001 From: artus40 Date: Thu, 14 Feb 2019 21:11:52 +0100 Subject: [PATCH] works on Domain docs and api --- planner/src/solver.rs | 50 +++++++++++++++++++++++++++++++++++++------ web/src/main.rs | 5 +++-- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/planner/src/solver.rs b/planner/src/solver.rs index a304f61..a6a3103 100644 --- a/planner/src/solver.rs +++ b/planner/src/solver.rs @@ -14,12 +14,14 @@ enum Assignment<'a, V, K> { Clear(K) } - +/// Collection of references to values owned by a domain. pub type DomainValues<'a, V> = Vec<&'a V>; -/// The domain of values that can be assigned to variables + +/// The domain of values that can be assigned to a variable. +/// The values are owned by the instance. #[derive(Clone)] pub struct Domain { - pub values: Vec + values: Vec } impl Domain { @@ -27,11 +29,21 @@ impl Domain { Domain { values } } - /// Returns all values of a Domain instance + /// Returns references to all values of this instance + /// + /// # Examples + /// + /// ``` + /// # extern crate planner; + /// # use planner::solver::Domain; + /// let domain = Domain::new(vec!["a", "b", "c"]); + /// assert_eq!(domain.all(), vec![&"a", &"b", &"c"]); + /// ``` pub fn all(&self) -> DomainValues { self.values.iter().collect() } - /// Returns a Filter filter applied to inner values + /// Returns a collection of references to a filtered + /// subset of this domain. /// /// # Examples /// @@ -45,13 +57,39 @@ impl Domain { /// assert_eq!(domain.filter(even), vec![&2]); /// assert_eq!(domain.filter(|i: &i32| i % 2 == 1), vec![&1,&3]); /// ``` - pub fn filter(&self, filter_func: fn(&V) -> bool) -> DomainValues { + pub fn filter(&self, filter_func: F) -> DomainValues + where F: Fn(&V) -> bool + { self.values .iter() .filter(|v: &&V| filter_func(*v)) .collect() } + /// Wrapper for `find`, returns a optionnal reference + /// to the first found value of this domain. + /// + /// # Examples + /// + /// ``` + /// # extern crate planner; + /// # use planner::solver::Domain; + /// let domain = Domain::new(vec![1,2,3]); + /// fn even(i: &i32) -> bool { + /// *i == 2 + /// }; + /// assert_eq!(domain.find(even), Some(&2)); + /// assert_eq!(domain.find(|i: &i32| i % 2 == 1), Some(&1)); + /// assert_eq!(domain.find(|i| *i == 4), None); + /// ``` + pub fn find(&self, getter_func: F) -> Option<&V> + where F: Fn(&V) -> bool + { + self.values + .iter() + .find(|v: &&V| getter_func(*v)) + } + } impl fmt::Debug for Domain { diff --git a/web/src/main.rs b/web/src/main.rs index 2a6b698..1542f60 100644 --- a/web/src/main.rs +++ b/web/src/main.rs @@ -151,7 +151,8 @@ mod api { } }); let ini = if let Some(id) = initial_id { - let new_ini = domain.values.iter().find(|r| r.id == id); + // Not working because we're capturing `id` + let new_ini = domain.find(|r| {r.id == id}); println!("Overrided {:?}", new_ini); new_ini } else { @@ -184,7 +185,7 @@ fn main() -> Result<(), Error> { let (allowed_origins, failed_origins) = AllowedOrigins::some(&["http://localhost:8080"]); assert!(failed_origins.is_empty()); - // You can also deserialize this + // You can also deserialize this let cors = rocket_cors::CorsOptions { allowed_origins: allowed_origins, allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),