From 3580fd25462b515b9ffc7197f6aa2ab0af06bce4 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 11 Oct 2020 02:22:11 +0100 Subject: [PATCH] swap repack and try_into Signed-off-by: Ian Jackson --- zcoord/zcoord.rs | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/zcoord/zcoord.rs b/zcoord/zcoord.rs index 29bc8d72..03261e0d 100644 --- a/zcoord/zcoord.rs +++ b/zcoord/zcoord.rs @@ -202,7 +202,26 @@ impl Mutable { pub fn decrement(&mut self) -> ZCoord { self.addsub(&AddSubDec)? } #[throws(Overflow)] - pub fn repack(&self) -> ZCoord { self.try_into()? } + pub fn repack(&self) -> ZCoord { + let taillen = (self.limbs.len() * TEXT_PER_LIMB - 1).try_into()?; + let mut bf = ZCoord::alloc(taillen); + let mut w = bf.tail_mut(); + for mut l in self.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).0 as u8; + p[0] = if v < 10 { b'0' + v } else { (b'a' - 10) + v }; + l >>= BITS_PER_DIGIT; + } + if let Some(p) = w.get_mut(DIGITS_PER_LIMB) { + *p = b'_'; + } else { + break; + } + w = &mut w[TEXT_PER_LIMB..]; + } + bf + } } pub type RangeIterator = std::iter::Take; @@ -407,26 +426,7 @@ impl ZCoord { impl TryFrom<&Mutable> for ZCoord { type Error = Overflow; #[throws(Overflow)] - fn try_from(m: &Mutable) -> ZCoord { - let taillen = (m.limbs.len() * TEXT_PER_LIMB - 1).try_into()?; - let mut bf = ZCoord::alloc(taillen); - let mut w = bf.tail_mut(); - for mut l in m.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).0 as u8; - p[0] = if v < 10 { b'0' + v } else { (b'a' - 10) + v }; - l >>= BITS_PER_DIGIT; - } - if let Some(p) = w.get_mut(DIGITS_PER_LIMB) { - *p = b'_'; - } else { - break; - } - w = &mut w[TEXT_PER_LIMB..]; - } - bf - } + fn try_from(m: &Mutable) -> ZCoord { m.repack()? } } //---------- innards, unsafe ---------- -- 2.30.2