use std::borrow::Cow;
#[derive(Debug, Clone, PartialEq)]
pub struct Poly3 {
pub a: f64,
pub b: f64,
pub c: f64,
pub d: f64,
}
impl Poly3 {
pub fn v(&self, u: f64) -> f64 {
self.a + (self.b * u) + (self.c * u * u) + (self.d * u * u * u)
}
pub fn visit_attributes(
&self,
visitor: impl for<'b> FnOnce(
Cow<'b, [xml::attribute::Attribute<'b>]>,
) -> xml::writer::Result<()>,
) -> xml::writer::Result<()> {
visit_attributes!(
visitor,
"a" => &self.a.to_scientific_string(),
"b" => &self.b.to_scientific_string(),
"c" => &self.c.to_scientific_string(),
"d" => &self.d.to_scientific_string(),
)
}
pub fn visit_children(
&self,
mut visitor: impl FnMut(xml::writer::XmlEvent) -> xml::writer::Result<()>,
) -> xml::writer::Result<()> {
visit_children!(visitor);
Ok(())
}
}
impl<'a, I> TryFrom<crate::parser::ReadContext<'a, I>> for Poly3
where
I: Iterator<Item = xml::reader::Result<xml::reader::XmlEvent>>,
{
type Error = Box<crate::parser::Error>;
fn try_from(mut read: crate::parser::ReadContext<'a, I>) -> Result<Self, Self::Error> {
read.expecting_no_child_elements_for(Self {
a: read.attribute("a")?,
b: read.attribute("b")?,
c: read.attribute("c")?,
d: read.attribute("d")?,
})
}
}
#[cfg(feature = "fuzzing")]
impl arbitrary::Arbitrary<'_> for Poly3 {
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Self> {
use crate::fuzzing::NotNan;
Ok(Self {
a: u.not_nan_f64()?,
b: u.not_nan_f64()?,
c: u.not_nan_f64()?,
d: u.not_nan_f64()?,
})
}
}