works on Domain docs and api
This commit is contained in:
@@ -14,12 +14,14 @@ enum Assignment<'a, V, K> {
|
|||||||
Clear(K)
|
Clear(K)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Collection of references to values owned by a domain.
|
||||||
pub type DomainValues<'a, V> = Vec<&'a V>;
|
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)]
|
#[derive(Clone)]
|
||||||
pub struct Domain<V> {
|
pub struct Domain<V> {
|
||||||
pub values: Vec<V>
|
values: Vec<V>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V> Domain<V> {
|
impl<V> Domain<V> {
|
||||||
@@ -27,11 +29,21 @@ impl<V> Domain<V> {
|
|||||||
Domain { values }
|
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<V> {
|
pub fn all(&self) -> DomainValues<V> {
|
||||||
self.values.iter().collect()
|
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
|
/// # Examples
|
||||||
///
|
///
|
||||||
@@ -45,13 +57,39 @@ impl<V> Domain<V> {
|
|||||||
/// assert_eq!(domain.filter(even), vec![&2]);
|
/// assert_eq!(domain.filter(even), vec![&2]);
|
||||||
/// assert_eq!(domain.filter(|i: &i32| i % 2 == 1), vec![&1,&3]);
|
/// assert_eq!(domain.filter(|i: &i32| i % 2 == 1), vec![&1,&3]);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn filter(&self, filter_func: fn(&V) -> bool) -> DomainValues<V> {
|
pub fn filter<F>(&self, filter_func: F) -> DomainValues<V>
|
||||||
|
where F: Fn(&V) -> bool
|
||||||
|
{
|
||||||
self.values
|
self.values
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|v: &&V| filter_func(*v))
|
.filter(|v: &&V| filter_func(*v))
|
||||||
.collect()
|
.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<F>(&self, getter_func: F) -> Option<&V>
|
||||||
|
where F: Fn(&V) -> bool
|
||||||
|
{
|
||||||
|
self.values
|
||||||
|
.iter()
|
||||||
|
.find(|v: &&V| getter_func(*v))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V: fmt::Debug> fmt::Debug for Domain<V> {
|
impl<V: fmt::Debug> fmt::Debug for Domain<V> {
|
||||||
|
|||||||
@@ -151,7 +151,8 @@ mod api {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
let ini = if let Some(id) = initial_id {
|
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);
|
println!("Overrided {:?}", new_ini);
|
||||||
new_ini
|
new_ini
|
||||||
} else {
|
} else {
|
||||||
@@ -184,7 +185,7 @@ fn main() -> Result<(), Error> {
|
|||||||
let (allowed_origins, failed_origins) = AllowedOrigins::some(&["http://localhost:8080"]);
|
let (allowed_origins, failed_origins) = AllowedOrigins::some(&["http://localhost:8080"]);
|
||||||
assert!(failed_origins.is_empty());
|
assert!(failed_origins.is_empty());
|
||||||
|
|
||||||
// You can also deserialize this
|
// You can also deserialize this
|
||||||
let cors = rocket_cors::CorsOptions {
|
let cors = rocket_cors::CorsOptions {
|
||||||
allowed_origins: allowed_origins,
|
allowed_origins: allowed_origins,
|
||||||
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
||||||
|
|||||||
Reference in New Issue
Block a user