From: Ian Jackson Date: Sun, 4 Oct 2020 18:32:55 +0000 (+0100) Subject: impl various things X-Git-Tag: otter-0.2.0~761 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=50b584001ba4df393e0d9fef60acc77271f2ca2d;p=otter.git impl various things Signed-off-by: Ian Jackson --- diff --git a/src/bigfloat.rs b/src/bigfloat.rs index e9d328fe..2a44bf03 100644 --- a/src/bigfloat.rs +++ b/src/bigfloat.rs @@ -13,14 +13,10 @@ use Sign::*; type Sz = u16; type Limb = [u16;3]; -#[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; +pub use innards::Bigfloat; mod innards { use super::*; @@ -29,7 +25,11 @@ mod innards { use std::alloc::{self, Layout}; use std::slice; - pub type Innards = NonNull; + #[derive(Deserialize)]//,Serialize + #[serde(try_from="&str")] + pub struct Bigfloat(Innards); + + type Innards = NonNull; pub(in super) struct Header { @@ -211,25 +211,24 @@ 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; -/* +pub 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 { } + fn try_from(s: &str) -> Bigfloat { + Bigfloat::from_str(s).ok_or(ParseError)? + } } -*/ +impl Serialize for Bigfloat { + fn serialize(&self, s: S) -> Result { + let d = self.to_string(); + s.serialize_str(&d) + } +} #[cfg(test)] mod test {