From: Ian Jackson Date: Sun, 4 Oct 2020 18:20:20 +0000 (+0100) Subject: impl various things X-Git-Tag: otter-0.2.0~762 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=062be97b4994e5e01da2fda76aef43bb98b938e4;p=otter.git impl various things Signed-off-by: Ian Jackson --- diff --git a/src/bigfloat.rs b/src/bigfloat.rs index c783e4a5..e9d328fe 100644 --- a/src/bigfloat.rs +++ b/src/bigfloat.rs @@ -13,7 +13,14 @@ use Sign::*; type Sz = u16; type Limb = [u16;3]; -pub struct Bigfloat(innards::Innards); +#[derive(Serialize)]//,Deserialize +#[serde(transparent)] +pub struct Bigfloat(#[serde(serialize_with="serialize")] Innards); + +const CHARS_HEADER : usize = 5; +const CHARS_PER_LIMB : usize = 15; + +use innards::Innards; mod innards { use super::*; @@ -157,7 +164,7 @@ impl Bigfloat { }; let exp = p.hex16(); - let mut limbs = Vec::with_capacity(p.0.len() / 15); + let mut limbs = Vec::with_capacity(p.0.len() / CHARS_PER_LIMB); loop { match p.next() { None => break, @@ -173,6 +180,14 @@ impl Bigfloat { if limbs.is_empty() { None? } Bigfloat::from_parts(sign, 0, &limbs) } + + fn to_string(&self) -> String { + let (h, _) = self.as_parts(); + let n = CHARS_HEADER + CHARS_PER_LIMB * (h.nlimbs as usize); + let mut s = String::with_capacity(n); + write!(&mut s, "{}", self).unwrap(); + s + } } impl Display for Bigfloat { @@ -196,6 +211,26 @@ impl Debug for Bigfloat { } } +fn serialize(v: &Innards, s: S) -> Result { + let d = Bigfloat(*v).to_string(); + s.serialize_str(&d) +} + +#[derive(Error,Clone,Copy,Debug)] +#[error("error parsing bigfloat (z value)")] +struct ParseError; +/* +impl TryFrom<&str> for Bigfloat { + type Error = ParseError; + #[throws(ParseError)] + fn try_from(s: &str) -> Bigfloat { } +} +impl From<&Bigfloat> for String { + fn from(v: &Bigfloat) -> String { } +} +*/ + + #[cfg(test)] mod test { use super::*;