pub struct SwitchBlock<T: Apply>{
pub data: BlockData,
/* private fields */
}Expand description
Switches between multiple input signals based on a condition.
The condition is the first input, and the rest are the signals to switch between. The block will output the signal that corresponds to the index of the `cases`` parameter that matches the condition input. If no matches are found, it will output the last input. For example:
use core::time::Duration;
use pictorus_blocks::SwitchBlock;
use pictorus_traits::ProcessBlock;
use pictorus_block_data::BlockData as OldBlockData;
use pictorus_traits::Context;
#[derive(Default)]
struct StubContext {}
impl Context for StubContext {
fn time(&self) -> Duration {
Duration::from_secs(0)
}
fn timestep(&self) -> Option<Duration> {
None
}
fn fundamental_timestep(&self) -> Duration {
Duration::from_millis(100)
}
}
let ctxt = StubContext::default();
let mut block = SwitchBlock::<(f64, f64, f64)>::default();
// If condition is 0, output the signal at index 0
// If condition is 1, output the signal at index 1
// If condition is anything else, output the signal at index 1
let cases = OldBlockData::from_vector(&[0.0, 1.0]);
let parameters = <SwitchBlock<(f64, f64, f64)> as ProcessBlock>::Parameters::new(&cases);
// Here we have a condition of 0.0, and inputs of [1.0, 2.0]
// Since condition matches case 0, the output will be 1.0
let input = (0.0, 1.0, 2.0);
let output = block.process(¶meters, &ctxt, input);
assert_eq!(output, 1.0);Fields§
§data: BlockDataTrait Implementations§
Source§impl<T: Apply> Default for SwitchBlock<T>
impl<T: Apply> Default for SwitchBlock<T>
Source§impl<T: Apply> ProcessBlock for SwitchBlock<T>
impl<T: Apply> ProcessBlock for SwitchBlock<T>
Auto Trait Implementations§
impl<T> Freeze for SwitchBlock<T>where
<<T as Apply>::Output as DefaultStorage>::Storage: Freeze,
impl<T> RefUnwindSafe for SwitchBlock<T>where
<<T as Apply>::Output as DefaultStorage>::Storage: RefUnwindSafe,
impl<T> Send for SwitchBlock<T>where
<<T as Apply>::Output as DefaultStorage>::Storage: Send,
impl<T> Sync for SwitchBlock<T>where
<<T as Apply>::Output as DefaultStorage>::Storage: Sync,
impl<T> Unpin for SwitchBlock<T>where
<<T as Apply>::Output as DefaultStorage>::Storage: Unpin,
impl<T> UnsafeUnpin for SwitchBlock<T>where
<<T as Apply>::Output as DefaultStorage>::Storage: UnsafeUnpin,
impl<T> UnwindSafe for SwitchBlock<T>where
<<T as Apply>::Output as DefaultStorage>::Storage: 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
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.