From f8c1ad78d97c02b7daf7ac7cb12c8927cae78f45 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 20 Nov 2020 23:48:53 +0000 Subject: [PATCH] this doesn't work, revert it not worth debugging I think Signed-off-by: Ian Jackson --- Cargo.lock.example | 12 ------------ zcoord/Cargo.toml | 3 +-- zcoord/zcoord.rs | 48 +++++++++++++--------------------------------- 3 files changed, 14 insertions(+), 49 deletions(-) diff --git a/Cargo.lock.example b/Cargo.lock.example index 9727eb58..7a7e0f68 100644 --- a/Cargo.lock.example +++ b/Cargo.lock.example @@ -352,17 +352,6 @@ dependencies = [ "syn 1.0.48", ] -[[package]] -name = "derive_more" -version = "0.99.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "298998b1cf6b5b2c8a7b023dfd45821825ce3ba8a8af55c921a0e734e4653f76" -dependencies = [ - "proc-macro2 1.0.24", - "quote 1.0.7", - "syn 1.0.48", -] - [[package]] name = "deunicode" version = "0.4.3" @@ -1175,7 +1164,6 @@ dependencies = [ name = "otter-zcoord" version = "0.0.1" dependencies = [ - "derive_more", "fehler", "serde", "serde_with", diff --git a/zcoord/Cargo.toml b/zcoord/Cargo.toml index 7a29c1f0..f7705995 100644 --- a/zcoord/Cargo.toml +++ b/zcoord/Cargo.toml @@ -13,8 +13,7 @@ path = "zcoord.rs" [dependencies] -derive_more = "0.99" +serde = { version = "1", features = ["derive","rc"] } fehler = "1" thiserror = "1" -serde = { version = "1", features = ["derive","rc"] } serde_with = "1" diff --git a/zcoord/zcoord.rs b/zcoord/zcoord.rs index 6d63243b..03ca0e1e 100644 --- a/zcoord/zcoord.rs +++ b/zcoord/zcoord.rs @@ -72,7 +72,6 @@ use std::num::{TryFromIntError, Wrapping}; use std::str; use std::str::FromStr; use fehler::{throw, throws}; -use derive_more::*; use serde::{Serialize, Deserialize}; use thiserror::Error; use serde_with::DeserializeFromStr; @@ -86,22 +85,19 @@ const BITS_PER_DIGIT : usize = 5; const DIGITS_PER_LIMB : usize = 10; type RawLimbVal = u64; -#[derive(Copy,Clone,Eq,PartialEq,Ord,PartialOrd)] -#[derive(Neg,Add,BitAnd,Sub,Shr,ShrAssign)] -pub struct LimbVal (Wrapping); +type LimbVal = Wrapping; -const DELTA : LimbVal = lv(0x4000_0000); -const ZERO : LimbVal = lv(0); -const ONE : LimbVal = lv(1); -const MINUS_ONE : LimbVal = lv(-1i64 as u64); +const DELTA : LimbVal = Wrapping(0x4000_0000); +const ZERO : LimbVal = Wrapping(0); +const ONE : LimbVal = Wrapping(1); const RAW_LIMB_MODULUS : RawLimbVal = 1u64 << BITS_PER_LIMB; const BITS_PER_LIMB : usize = BITS_PER_DIGIT * DIGITS_PER_LIMB; -const DIGIT_MASK : LimbVal = lv((1u64 << BITS_PER_DIGIT) - 1); +const DIGIT_MASK : LimbVal = Wrapping((1u64 << BITS_PER_DIGIT) - 1); const TEXT_PER_LIMB : usize = DIGITS_PER_LIMB + 1; -const LIMB_MODULUS : LimbVal = lv(RAW_LIMB_MODULUS); -const LIMB_MASK : LimbVal = lv(RAW_LIMB_MODULUS-1); +const LIMB_MODULUS : LimbVal = Wrapping(RAW_LIMB_MODULUS); +const LIMB_MASK : LimbVal = Wrapping(RAW_LIMB_MODULUS-1); #[derive(DeserializeFromStr,SerializeDisplay)] pub struct ZCoord(innards::Innards); @@ -128,24 +124,6 @@ pub enum LogicError { #[error("{0}")] RangeBackwards (#[from] RangeBackwards ), } -//---------- LimbVal ---------- - -impl From for LimbVal { - fn from(raw: RawLimbVal) -> LimbVal { lv(raw) } -} - -const fn lv(raw: RawLimbVal) -> LimbVal { LimbVal(Wrapping(raw)) } - -impl LimbVal { - fn primitive(self) -> RawLimbVal { self.0.0 } -} - -impl Debug for LimbVal { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error> { - write!(f, "{:x?}", self) - } -} - //---------- Mutabel ---------- #[derive(Clone,Debug)] @@ -166,7 +144,7 @@ impl Mutable { for lt in tail.chunks(TEXT_PER_LIMB) { let s = str::from_utf8(<[0..DIGITS_PER_LIMB]).unwrap(); let v = RawLimbVal::from_str_radix(s, 1 << BITS_PER_DIGIT).unwrap(); - limbs.push(v.into()); + limbs.push(Wrapping(v)); } Mutable { limbs } } @@ -206,7 +184,7 @@ impl AddSubOffset for Increment { pub struct Decrement; impl AddSubOffset for Decrement { fn init_delta(&self) -> LimbVal { -DELTA } - const CARRY_DELTA : LimbVal = MINUS_ONE; + const CARRY_DELTA : LimbVal = Wrapping(ONE .0.wrapping_neg()); const NEW_LIMBS : LimbVal = LIMB_MASK; #[throws(as Option)] fn check_underflow(_: &Mutable, i: usize, nv: LimbVal) { @@ -266,7 +244,7 @@ impl Mutable { for mut l in limbs.iter().cloned() { if l >= LIMB_MODULUS { throw!(Overflow) }; for p in w[0..DIGITS_PER_LIMB].rchunks_exact_mut(1) { - let v = (l & DIGIT_MASK).primitive() as u8; + let v = (l & DIGIT_MASK).0 as u8; p[0] = if v < 10 { b'0' + v } else { (b'a' - 10) + v }; l >>= BITS_PER_DIGIT; } @@ -336,7 +314,7 @@ impl Mutable { if la == lb { continue } let wantgaps = count+1; - let avail = (lb.primitive() as i64) - (la.primitive() as i64) + let avail = (lb.0 as i64) - (la.0 as i64) + if borrowing { RAW_LIMB_MODULUS as i64 } else { 0}; if avail < 0 { throw!(RangeBackwards) } let avail = avail as u64; @@ -361,14 +339,14 @@ impl Mutable { } else { // Not enough space here, but we can pick a unique value for // this limb, and divide the next limb evenly - current.limbs[i] = la + (avail/2).into(); + current.limbs[i] = la + Wrapping(avail/2); i += 1; step = (RAW_LIMB_MODULUS-1) / wantgaps; init = ZERO; } current.extend_to_limb(i); current.limbs[i] = init; - break 'ok ASRD { i, step: step.into() }; + break 'ok ASRD { i, step: Wrapping(step) }; } }; IteratorCore { current, aso } } -- 2.30.2