#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FloatConstraint {
Finite,
NonNegative,
Positive,
}
impl FloatConstraint {
#[inline]
pub const fn check(self, value: f32) -> bool {
match self {
Self::Finite => value.is_finite(),
Self::NonNegative => value.is_finite() && value >= 0.0,
Self::Positive => value.is_finite() && value > 0.0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FlatOpFieldType {
U32,
ColorRgba,
F32(FloatConstraint),
Point2Slice {
max_len: u32,
max_const_name: &'static str,
coord_constraint: FloatConstraint,
},
StrSlice {
max_bytes: u32,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FlatOpFieldDesc {
pub name: &'static str,
pub ty: FlatOpFieldType,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FlatOpDesc {
pub name: &'static str,
pub opcode: u32,
pub fields: &'static [FlatOpFieldDesc],
}
impl FlatOpDesc {
#[inline]
pub const fn fixed_words(&self) -> Option<usize> {
if self.has_str_slice() {
None
} else {
Some(self.fields.len())
}
}
#[inline]
pub const fn min_words(&self) -> usize {
self.fields.len()
}
#[inline]
pub const fn has_point_slice(&self) -> bool {
if self.fields.is_empty() {
return false;
}
matches!(
self.fields[self.fields.len() - 1].ty,
FlatOpFieldType::Point2Slice { .. }
)
}
#[inline]
pub const fn has_str_slice(&self) -> bool {
let mut i = 0;
while i < self.fields.len() {
if matches!(self.fields[i].ty, FlatOpFieldType::StrSlice { .. }) {
return true;
}
i += 1;
}
false
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FlatOpScopeDesc {
pub limit_name: &'static str,
pub push_opcode: u32,
pub pop_opcode: u32,
pub max_depth: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FlatOpStreamDesc {
pub name: &'static str,
pub kind_name: &'static str,
pub max_ops_const_name: &'static str,
pub max_ops: u32,
pub header: &'static [FlatOpFieldDesc],
pub scope: Option<FlatOpScopeDesc>,
pub ops: &'static [FlatOpDesc],
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE_OP: FlatOpDesc = FlatOpDesc {
name: "StrokePolyline",
opcode: 6,
fields: &[
FlatOpFieldDesc {
name: "width",
ty: FlatOpFieldType::F32(FloatConstraint::NonNegative),
},
FlatOpFieldDesc {
name: "color",
ty: FlatOpFieldType::ColorRgba,
},
FlatOpFieldDesc {
name: "points",
ty: FlatOpFieldType::Point2Slice {
max_len: 16_384,
max_const_name: "MAX_POLYLINE_POINTS",
coord_constraint: FloatConstraint::Finite,
},
},
],
};
#[test]
fn float_constraints_handle_edge_values() {
assert!(FloatConstraint::Finite.check(0.0));
assert!(FloatConstraint::Finite.check(-0.0));
assert!(FloatConstraint::Finite.check(-42.5));
assert!(!FloatConstraint::Finite.check(f32::NAN));
assert!(!FloatConstraint::Finite.check(f32::INFINITY));
assert!(!FloatConstraint::Finite.check(f32::NEG_INFINITY));
assert!(FloatConstraint::NonNegative.check(0.0));
assert!(FloatConstraint::NonNegative.check(-0.0));
assert!(FloatConstraint::NonNegative.check(1.25));
assert!(!FloatConstraint::NonNegative.check(-0.0001));
assert!(!FloatConstraint::NonNegative.check(f32::NAN));
assert!(!FloatConstraint::NonNegative.check(f32::INFINITY));
assert!(!FloatConstraint::NonNegative.check(f32::NEG_INFINITY));
assert!(FloatConstraint::Positive.check(f32::MIN_POSITIVE));
assert!(FloatConstraint::Positive.check(f32::from_bits(1)));
assert!(FloatConstraint::Positive.check(f32::MAX));
assert!(!FloatConstraint::Positive.check(0.0));
assert!(!FloatConstraint::Positive.check(-0.0));
assert!(!FloatConstraint::Positive.check(-1.0));
assert!(!FloatConstraint::Positive.check(f32::NAN));
assert!(!FloatConstraint::Positive.check(f32::INFINITY));
}
#[test]
fn flat_op_desc_reports_word_shape() {
assert_eq!(SAMPLE_OP.fixed_words(), Some(3));
assert_eq!(SAMPLE_OP.min_words(), 3);
assert!(SAMPLE_OP.has_point_slice());
assert!(!SAMPLE_OP.has_str_slice());
const TEXT_OP: FlatOpDesc = FlatOpDesc {
name: "DrawText",
opcode: 9,
fields: &[
FlatOpFieldDesc {
name: "x",
ty: FlatOpFieldType::F32(FloatConstraint::Finite),
},
FlatOpFieldDesc {
name: "text",
ty: FlatOpFieldType::StrSlice { max_bytes: 4096 },
},
],
};
assert_eq!(TEXT_OP.fixed_words(), None);
assert_eq!(TEXT_OP.min_words(), 2);
assert!(!TEXT_OP.has_point_slice());
assert!(TEXT_OP.has_str_slice());
const EMPTY_OP: FlatOpDesc = FlatOpDesc {
name: "RestoreClip",
opcode: 8,
fields: &[],
};
assert_eq!(EMPTY_OP.fixed_words(), Some(0));
assert_eq!(EMPTY_OP.min_words(), 0);
assert!(!EMPTY_OP.has_point_slice());
assert!(!EMPTY_OP.has_str_slice());
}
}