use super::super::data::{Stream, Type};
use super::super::exec::SelectorCx;
use super::super::registry::{GraphId, SelectFn};
#[derive(Debug)]
pub(crate) struct ConstantSelector;
pub(crate) static CONSTANT: ConstantSelector = ConstantSelector;
const GRAPH_CONSTANT_SERIAL: GraphId = GraphId::std(42);
const GRAPH_CONSTANT_FIXED: GraphId = GraphId::std(43);
fn is_single_byte_pattern(input: &Stream<'_>) -> bool {
if input.n_elts() == 0 {
return false;
}
let first_element = &input.bytes()[..input.width()];
first_element.iter().all(|&byte| byte == first_element[0])
}
impl SelectFn for ConstantSelector {
fn select(&self, _: &mut SelectorCx<'_, '_, '_>, input: &Stream<'_>, _: &[GraphId]) -> GraphId {
if is_single_byte_pattern(input) {
return GRAPH_CONSTANT_SERIAL;
}
match input.ty() {
Type::Serial => GRAPH_CONSTANT_SERIAL,
Type::Struct | Type::Numeric => GRAPH_CONSTANT_FIXED,
Type::String => GraphId::ILLEGAL,
}
}
}