diff --git a/nanvm-lib/src/vm/bigint/add.rs b/nanvm-lib/src/vm/bigint/add.rs index 281d008e1..6963c7389 100644 --- a/nanvm-lib/src/vm/bigint/add.rs +++ b/nanvm-lib/src/vm/bigint/add.rs @@ -1,11 +1,11 @@ use core::ops::Add; -use crate::vm::{BigInt, IContainer, IVm}; +use crate::vm::{BigInt, IVm}; impl Add for BigInt { type Output = Self; fn add(self, rhs: Self) -> Self::Output { - let rhs_sign = *rhs.0.header(); + let rhs_sign = rhs.sign(); self.add_signed(rhs, rhs_sign) } } diff --git a/nanvm-lib/src/vm/bigint/cmp.rs b/nanvm-lib/src/vm/bigint/cmp.rs index 6e1d87f41..74fd506d4 100644 --- a/nanvm-lib/src/vm/bigint/cmp.rs +++ b/nanvm-lib/src/vm/bigint/cmp.rs @@ -1,4 +1,4 @@ -use crate::vm::{IContainer, IVm, bigint::BigInt}; +use crate::vm::{IVm, bigint::BigInt}; use std::cmp::Ordering; impl PartialOrd for BigInt { @@ -11,8 +11,8 @@ impl Ord for BigInt { fn cmp(&self, rhs: &Self) -> Ordering { use crate::sign::Sign; - let lhs_sign = *self.0.header(); - let rhs_sign = *rhs.0.header(); + let lhs_sign = self.sign(); + let rhs_sign = rhs.sign(); match (lhs_sign, rhs_sign) { (Sign::Positive, Sign::Negative) => Ordering::Greater, diff --git a/nanvm-lib/src/vm/bigint/debug.rs b/nanvm-lib/src/vm/bigint/debug.rs index 71951dad7..db4ea9891 100644 --- a/nanvm-lib/src/vm/bigint/debug.rs +++ b/nanvm-lib/src/vm/bigint/debug.rs @@ -10,7 +10,7 @@ impl Debug for BigInt { if self.is_zero() { return f.write_str("0n"); } - if *self.0.header() == Sign::Negative { + if self.sign() == Sign::Negative { f.write_char('-')?; } f.write_str("0x")?; diff --git a/nanvm-lib/src/vm/bigint/mod.rs b/nanvm-lib/src/vm/bigint/mod.rs index 7b7ba93bf..eead2ba3d 100644 --- a/nanvm-lib/src/vm/bigint/mod.rs +++ b/nanvm-lib/src/vm/bigint/mod.rs @@ -46,6 +46,10 @@ impl BigInt { self.0.items().is_empty() } + fn sign(&self) -> Sign { + *self.0.header() + } + /// The function doesn't normalize the bigint. fn unchecked_new(sign: Sign, items: impl IntoIterator) -> Self { Self(A::InternalBigInt::new_ok(sign, items)) @@ -75,7 +79,7 @@ impl BigInt { /// via `Neg`, because `Neg` rebuilds the container (copying every word) — /// don't "simplify" `sub` back to `self + (-rhs)`. fn add_signed(self, rhs: Self, rhs_sign: Sign) -> Self { - let lhs_sign = *self.0.header(); + let lhs_sign = self.sign(); let (sign, vec) = if lhs_sign == rhs_sign { (lhs_sign, self.abs_add_vec(rhs)) } else { diff --git a/nanvm-lib/src/vm/bigint/mul.rs b/nanvm-lib/src/vm/bigint/mul.rs index b487dd14b..13eae3527 100644 --- a/nanvm-lib/src/vm/bigint/mul.rs +++ b/nanvm-lib/src/vm/bigint/mul.rs @@ -1,7 +1,7 @@ use crate::{ common::{sized_index::SizedIndex, vec::with_default}, sign::Sign, - vm::{BigInt, IContainer, IVm}, + vm::{BigInt, IVm}, }; use std::ops::Mul; @@ -34,7 +34,7 @@ impl Mul for BigInt { i += 1; } - let sign = if self.0.header() == rhs.0.header() { + let sign = if self.sign() == rhs.sign() { Sign::Positive } else { Sign::Negative diff --git a/nanvm-lib/src/vm/bigint/neg.rs b/nanvm-lib/src/vm/bigint/neg.rs index a8a5bbacd..ff0e99e6a 100644 --- a/nanvm-lib/src/vm/bigint/neg.rs +++ b/nanvm-lib/src/vm/bigint/neg.rs @@ -2,7 +2,7 @@ use std::ops::Neg; use crate::{ common::sized_index::SizedIndex, - vm::{BigInt, IContainer, IVm}, + vm::{BigInt, IVm}, }; impl Neg for BigInt { @@ -11,7 +11,7 @@ impl Neg for BigInt { if self.is_zero() { self } else { - Self::unchecked_new(self.0.header().flip(), self.index_iter()) + Self::unchecked_new(self.sign().flip(), self.index_iter()) } } } diff --git a/nanvm-lib/src/vm/bigint/shl.rs b/nanvm-lib/src/vm/bigint/shl.rs index 23bbac580..cc8bcfbf0 100644 --- a/nanvm-lib/src/vm/bigint/shl.rs +++ b/nanvm-lib/src/vm/bigint/shl.rs @@ -2,7 +2,7 @@ use core::ops::Shl; use crate::{ common::{div_mod::DivMod, sized_index::SizedIndex}, - vm::{Any, BigInt, IContainer, IVm}, + vm::{Any, BigInt, IVm}, }; const TOO_LARGE: &str = "shl: shift amount too large"; @@ -58,7 +58,7 @@ impl Shl for BigInt { "shl: result must be normalized and non-empty" ); - Ok(Self::unchecked_new(*self.0.header(), value)) + Ok(Self::unchecked_new(self.sign(), value)) } } diff --git a/nanvm-lib/src/vm/bigint/shr.rs b/nanvm-lib/src/vm/bigint/shr.rs index 997eca560..7b463cc89 100644 --- a/nanvm-lib/src/vm/bigint/shr.rs +++ b/nanvm-lib/src/vm/bigint/shr.rs @@ -2,7 +2,7 @@ use core::ops::Shr; use crate::{ common::{div_mod::DivMod, sized_index::SizedIndex}, - vm::{BigInt, IContainer, IVm}, + vm::{BigInt, IVm}, }; impl Shr for BigInt { @@ -38,7 +38,7 @@ impl Shr for BigInt { } } - Self::normalize_new(*self.0.header(), value) + Self::normalize_new(self.sign(), value) } } diff --git a/nanvm-lib/src/vm/bigint/sub.rs b/nanvm-lib/src/vm/bigint/sub.rs index 5ba84e332..2c344dc01 100644 --- a/nanvm-lib/src/vm/bigint/sub.rs +++ b/nanvm-lib/src/vm/bigint/sub.rs @@ -1,11 +1,11 @@ use core::ops::Sub; -use crate::vm::{BigInt, IContainer, IVm}; +use crate::vm::{BigInt, IVm}; impl Sub for BigInt { type Output = Self; fn sub(self, rhs: Self) -> Self::Output { - let rhs_sign = rhs.0.header().flip(); + let rhs_sign = rhs.sign().flip(); self.add_signed(rhs, rhs_sign) } } diff --git a/nanvm-lib/todo/bigint-sign-accessor.md b/nanvm-lib/todo/bigint-sign-accessor.md deleted file mode 100644 index 0f7cb8fc5..000000000 --- a/nanvm-lib/todo/bigint-sign-accessor.md +++ /dev/null @@ -1,50 +0,0 @@ -## bigint-sign-accessor. `*self.0.header()` is open-coded in eight bigint files - -**Priority:** P4 -**Status:** open - -### Problem - -`BigInt` stores its sign as the container header but exposes no accessor, -so every operator file reaches through the private tuple field into the -generic container — ten sites in eight files under `src/vm/bigint/`: - -```rust -// mod.rs:78 let lhs_sign = *self.0.header(); -// cmp.rs:14-15 let lhs_sign = *self.0.header(); let rhs_sign = *rhs.0.header(); -// add.rs:8 let rhs_sign = *rhs.0.header(); -// sub.rs:8 let rhs_sign = rhs.0.header().flip(); -// neg.rs:14 Self::unchecked_new(self.0.header().flip(), self.index_iter()) -// mul.rs:37 if self.0.header() == rhs.0.header() { -// shl.rs:61 Ok(Self::unchecked_new(*self.0.header(), value)) -// shr.rs:41 Self::normalize_new(*self.0.header(), value) -// debug.rs:13 if *self.0.header() == Sign::Negative { -``` - -This is also why most `bigint/*.rs` files carry a `use crate::vm::IContainer` -import they need for nothing else. The crate already has the right shape one -directory over: `Function` exposes `name()`/`length()` instead of letting -callers read `self.0.header().0`, and `BigInt` itself already wraps one -container access in `is_zero()` (`mod.rs:45-47`). - -### Proposal - -```rust -// src/vm/bigint/mod.rs, next to is_zero(): -pub(crate) fn sign(&self) -> Sign { - *self.0.header() -} -``` - -Route the ten sites through it (`rhs.sign()`, `self.sign().flip()`, …) and -drop the now-unneeded `IContainer` imports. - -### Tasks - -- [ ] Add `sign()`; rewrite the ten sites; prune imports. -- [ ] `cargo test`, `cargo clippy`, `cargo fmt -- --check`. - -### Related - -- [sign-algebra](./sign-algebra.md) — gives `Sign` its algebra once the sign - is in hand; this issue is about how the sign is *read*.