[−][src]Struct psyche_utils::switch::Switch
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]);
Methods
impl<T> Switch<T>
[src]
pub fn new(options: usize, value: T) -> Self where
T: Clone,
[src]
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]);
pub fn with_options(options: Vec<T>) -> Self
[src]
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], ]);
pub fn index(&self) -> usize
[src]
Gets currently active option index.
Return
Index of currently active switch option.
Example
use psyche_utils::switch::Switch; let mut switch = Switch::new(2, 0); assert_eq!(switch.index(), 0); switch.switch(); assert_eq!(switch.index(), 1); switch.switch(); assert_eq!(switch.index(), 0);
pub fn count(&self) -> usize
[src]
Gets number of options that holds.
Return
Number of switch options.
Example
use psyche_utils::switch::Switch; let mut switch = Switch::new(2, 0); assert_eq!(switch.count(), 2);
pub fn switch(&mut self)
[src]
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);
pub fn iterate(&mut self) -> Option<(&T, &mut T)>
[src]
Switches to next option and returns pair of previous and next options.
Return
Pair of previous and next options if holds more than one.
Example
use psyche_utils::switch::Switch; let mut switch = Switch::with_options(vec![0, 1]); assert_eq!(switch.iterate().unwrap(), (&0, &mut 1));
pub fn get(&self) -> Option<&T>
[src]
Gets currently active option if any.
Return
Reference to currently active option.
Example
use psyche_utils::switch::Switch; let mut switch = Switch::with_options(vec![0, 1]); assert_eq!(switch.get().unwrap(), &0);
pub fn get_mut(&mut self) -> Option<&mut T>
[src]
Trait Implementations
impl<T> Serialize for Switch<T> where
T: Serialize,
[src]
T: Serialize,
fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
__S: Serializer,
[src]
__S: Serializer,
impl<'de, T> Deserialize<'de> for Switch<T> where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,