pub trait IncDecCpCmp<T, V>: CpCmp<T> {
// Required methods
fn inc(&self, a: &T, b: &V) -> Option<T>;
fn dec(&self, a: &T, b: &V) -> Option<T>;
fn cp_v(&self, v: &V) -> V;
// Provided methods
fn rebound_start(&self, start: Bound<&T>, rebound: &V) -> Option<T> { ... }
fn rebound_end(&self, end: Bound<&T>, rebound: &V) -> Option<T> { ... }
}Expand description
Increment, Decrement, Copy, Compare Values and Ranges
This is the base trait used to represent range manipulation by crate.
In general this library implements the proxy or wrapper approach to range and value manipulation as apposed to
manipulation by value. This means the trait implementation for values does not need to be implemented on the generic type.
Since the operations are not implemented by the values, this frees us from the worry of conflicting trait resoltion. This
also means we can quickly implement how the values and ranges are manipualted for the same data for a different task.
Example Implementation
use common_range_tools::{CpCmp,IncDecCpCmp};
struct MyTrait {}
// First: implement CpCmp<T> for your trait.
impl CpCmp<i32> for MyTrait {
fn min(&self) ->i32 { <i32>::MIN }
fn max(&self) ->i32 { <i32>::MAX }
fn min_ref(&self) ->&i32 { &<i32>::MIN }
fn max_ref(&self) ->&i32 { &<i32>::MAX }
fn cp(&self,v: &i32) ->i32 { return v.clone() }
fn lt(&self,a:&i32,b: &i32) ->bool { return a<b }
}
// Next: implement IncDecCpCmp<T> for your trait.
impl IncDecCpCmp<i32,i32> for MyTrait {
fn dec(&self, a: &i32, b:&i32) ->Option<i32> {
if *b<=0 { return None}
return a.clone().checked_sub(b.clone());
}
fn inc(&self, a: &i32, b: &i32) -> Option<i32> {
if *b<=0 { return None}
return a.clone().checked_add(b.clone())
}
fn cp_v(&self,v:&i32) ->i32 {
return *v;
}
}Required Methods§
Sourcefn inc(&self, a: &T, b: &V) -> Option<T>
fn inc(&self, a: &T, b: &V) -> Option<T>
Should safely increment a by b. The value should always go up.. if not then it should return None.
Sourcefn dec(&self, a: &T, b: &V) -> Option<T>
fn dec(&self, a: &T, b: &V) -> Option<T>
Should safely decrement a by b. The value should always go down… if not then it should return None.
fn cp_v(&self, v: &V) -> V
Provided Methods§
Sourcefn rebound_start(&self, start: Bound<&T>, rebound: &V) -> Option<T>
fn rebound_start(&self, start: Bound<&T>, rebound: &V) -> Option<T>
Returns the raw adjusted start value.
- std::ops::Bound::Unbounded becomes self.min()
- std::ops::Bound::Included value is not changed
- std::ops::Bound::Excluded value is incremented
Sourcefn rebound_end(&self, end: Bound<&T>, rebound: &V) -> Option<T>
fn rebound_end(&self, end: Bound<&T>, rebound: &V) -> Option<T>
Returns the raw adjusted end value.
- std::ops::Bound::Unbounded becomes
- std::ops::Bound::Included value is not changed
- std::ops::Bound::Excluded value is decremented
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".