pub struct Switch<T> { /* private fields */ }Expand description
Collection that contains several versions/options of data so you can switch between them in sequential manner. It can be used to produce next data frame based on previous data frame.
§Example
use psyche_utils::switch::Switch;
let mut switch = Switch::new(2, vec![1, 2, 4]);
if let Some((prev, next)) = switch.iterate() {
for i in 0..prev.len() {
// next frame item equals sum of two neighbors.
let start = i.max(1) - 1;
let end = (i + 2).min(prev.len());
next[i] = (start..end).map(|i| prev[i]).sum();
}
}
assert_eq!(switch.get().unwrap(), &vec![3, 7, 6]);Implementations§
Source§impl<T> Switch<T>
impl<T> Switch<T>
Sourcepub fn new(options: usize, value: T) -> Selfwhere
T: Clone,
pub fn new(options: usize, value: T) -> Selfwhere
T: Clone,
Creates new switch with number of options and cloned value applied for each of them.
§Arguments
options- Number of options that switch will hold.value- Initial value applied for each of options.
§Return
Instance of switch.
§Example
use psyche_utils::switch::Switch;
let switch = Switch::new(2, vec![1, 2, 4]);Sourcepub fn with_options(options: Vec<T>) -> Self
pub fn with_options(options: Vec<T>) -> Self
Creates new switch with initial options values.
§Note
Make sure that your options values have same length if they are for example arrays or vectors or any other collection that needs to have same length across each iteration.
§Arguments
options- Initial values applied for options.
§Return
Instance of switch.
§Example
use psyche_utils::switch::Switch;
let switch = Switch::with_options(vec![
vec![1, 2, 4],
vec![3, 7, 6],
]);Sourcepub fn switch(&mut self)
pub fn switch(&mut self)
Switches to next option.
§Example
use psyche_utils::switch::Switch;
let mut switch = Switch::with_options(vec![0, 1]);
assert_eq!(*switch.get().unwrap(), 0);
switch.switch();
assert_eq!(*switch.get().unwrap(), 1);Trait Implementations§
Source§impl<'de, T> Deserialize<'de> for Switch<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Switch<T>where
T: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl<T> Freeze for Switch<T>
impl<T> RefUnwindSafe for Switch<T>where
T: RefUnwindSafe,
impl<T> Send for Switch<T>where
T: Send,
impl<T> Sync for Switch<T>where
T: Sync,
impl<T> Unpin for Switch<T>where
T: Unpin,
impl<T> UnwindSafe for Switch<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more