From 37d8b1f5e8b9f9a77cea68eeb8e0d7427abe3d68 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 11 Feb 2022 15:48:41 -0500 Subject: [PATCH] Some renaming and minor style fixes --- Cargo.toml | 2 +- Makefile | 5 ++++- rustfmt.toml | 7 ++++++ src/bigint.rs | 57 +++++++++++++++++++++++++------------------------ src/rational.rs | 15 ++++++------- 5 files changed, 47 insertions(+), 39 deletions(-) create mode 100644 rustfmt.toml diff --git a/Cargo.toml b/Cargo.toml index 6d110b5..1d42f78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rusty-numbers" version = "0.2.0" -authors = ["Timothy J. Warren "] +authors = ["Timothy J. Warren "] edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/Makefile b/Makefile index bb52643..e6e2a3f 100644 --- a/Makefile +++ b/Makefile @@ -13,10 +13,13 @@ clean: rm cobertura.xml rm coverage.html +fmt: + cargo +nightly fmt + test: cargo test test-no-std: cargo test --no-default-features --features alloc -.PHONY: test test-no-std \ No newline at end of file +.PHONY: test test-no-std fmt \ No newline at end of file diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..c9918d7 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,7 @@ +unstable_features = true +format_code_in_doc_comments = true +format_macro_matchers = true +format_strings = false +imports_granularity = "Module" +group_imports = "StdExternalCrate" +use_field_init_shorthand = true diff --git a/src/bigint.rs b/src/bigint.rs index 55472c2..2ca7800 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -1,4 +1,3 @@ -#![allow(unused_variables)] //! \[WIP\] Arbitrarily large integers use crate::num::FracOp; use crate::num::Sign::{self, Negative, Positive}; @@ -9,19 +8,17 @@ extern crate alloc; use alloc::string::*; #[cfg(all(feature = "alloc", not(feature = "std")))] use alloc::vec::*; - -#[cfg(feature = "std")] -use std::prelude::v1::*; - use core::cmp::{Ordering, PartialEq, PartialOrd}; use core::convert::TryInto; -use core::mem::replace; use core::ops::{ Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Sub, SubAssign, }; use core::usize; +#[cfg(feature = "std")] +use std::prelude::v1::*; #[derive(Clone, Debug, PartialEq)] +/// The representation of a `BigInt` pub struct BigInt { inner: Vec, sign: Sign, @@ -33,7 +30,7 @@ macro_rules! big_int { ($w:literal) => { $crate::bigint::BigInt::from($w) }; - (-$x:literal) => { + (- $x:literal) => { $crate::bigint::BigInt::from(-$x) }; } @@ -73,11 +70,15 @@ impl BigInt { /// /// The various `From` implementations are more useful in most cases #[must_use] - pub fn new() -> Self { + pub fn zero() -> Self { Self::default() } - fn new_empty() -> Self { + pub fn new(initial: impl Into) -> Self { + initial.into() + } + + fn empty() -> Self { Self { inner: Vec::new(), sign: Sign::Positive, @@ -140,8 +141,7 @@ impl BigInt { /// # Panics /// * If radix is not between 1 and 36 inclusive /// * Some branches are not yet implemented - #[allow(clippy::needless_pass_by_value)] - pub fn from_str_radix(s: T, radix: usize) -> BigInt { + pub fn from_str_radix(s: &T, radix: usize) -> BigInt { // Two lines due to borrow checker complaints let input = s.to_string().to_ascii_uppercase(); let input = input.trim(); @@ -188,6 +188,9 @@ impl BigInt { todo!(); } + /// Get the larger number of digits when comparing two `BigInt`s. + /// This is helpful to determine sizing for a `BigInt` after + /// a numeric operation. fn get_ceil_digit_count(a: &Self, b: &Self) -> usize { let a_digits = a.inner.len(); let b_digits = b.inner.len(); @@ -356,8 +359,6 @@ impl Mul for BigInt { #[must_use] fn mul(self, rhs: Self) -> Self::Output { - let input_digits = Self::get_ceil_digit_count(&self, &rhs); - // Multiplication can result in twice the number of digits let out_digits = Self::get_ceil_digit_count(&self, &rhs) * 2; @@ -369,7 +370,7 @@ impl Div for BigInt { type Output = Self; #[must_use] - fn div(self, rhs: Self) -> Self::Output { + fn div(self, _rhs: Self) -> Self::Output { todo!() } } @@ -378,42 +379,42 @@ impl Rem for BigInt { type Output = Self; #[must_use] - fn rem(self, rhs: Self) -> Self::Output { + fn rem(self, _rhs: Self) -> Self::Output { todo!() } } impl AddAssign for BigInt { fn add_assign(&mut self, rhs: Self) { - let this = replace(self, BigInt::new()); + let this = core::mem::replace(self, BigInt::zero()); *self = this + rhs; } } impl SubAssign for BigInt { fn sub_assign(&mut self, rhs: Self) { - let this = replace(self, BigInt::new()); + let this = core::mem::replace(self, BigInt::zero()); *self = this - rhs; } } impl MulAssign for BigInt { fn mul_assign(&mut self, rhs: Self) { - let this = replace(self, BigInt::new()); + let this = core::mem::replace(self, BigInt::zero()); *self = this * rhs; } } impl DivAssign for BigInt { fn div_assign(&mut self, rhs: Self) { - let this = replace(self, BigInt::new()); + let this = core::mem::replace(self, BigInt::zero()); *self = this / rhs; } } impl RemAssign for BigInt { fn rem_assign(&mut self, rhs: Self) { - let this = replace(self, BigInt::new()); + let this = core::mem::replace(self, BigInt::zero()); *self = this % rhs; } } @@ -510,7 +511,7 @@ macro_rules! impl_from_smaller { impl From<$u> for BigInt { /// Create a `BigInt` from an unsigned integer primitive fn from(n: $u) -> Self { - let mut new = Self::new_empty(); + let mut new = Self::empty(); new.inner.push(n as usize); new @@ -823,7 +824,7 @@ mod tests { let a = BigInt::from(core::usize::MAX); let b = BigInt::from(5); - let product = a * b; + let _product = a * b; } #[test] @@ -841,7 +842,7 @@ mod tests { let a = BigInt::from(128); let b = BigInt::from(32); - let quotient = a / b; + let _quotient = a / b; } #[test] @@ -859,7 +860,7 @@ mod tests { let a = BigInt::from(5); let b = BigInt::from(2); - let rem = a % b; + let _rem = a % b; } #[test] @@ -873,8 +874,8 @@ mod tests { #[test] fn test_zeros() { - let a = BigInt::new(); - let b = BigInt::new(); + let a = BigInt::zero(); + let b = BigInt::zero(); let c = a.clone() - b.clone(); assert_eq!(a.clone(), b.clone()); @@ -978,7 +979,7 @@ mod tests { #[test] fn test_from_str_radix_1() { let s = "1".repeat(32); - let num = BigInt::from_str_radix(s, 1); + let num = BigInt::from_str_radix(&s, 1); assert_eq!(num.inner[0], 32_usize); } @@ -988,7 +989,7 @@ mod tests { let zeroes = "0".repeat(24); let s = ones + &zeroes; - let num = BigInt::from_str_radix(s, 1); + let num = BigInt::from_str_radix(&s, 1); assert_eq!(num.inner[0], 32_usize); } diff --git a/src/rational.rs b/src/rational.rs index ba42c18..2c62673 100644 --- a/src/rational.rs +++ b/src/rational.rs @@ -1,9 +1,10 @@ //! # Rational Numbers (fractions) +use core::cmp::{Ord, Ordering, PartialOrd}; +use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; + use crate::num::Sign::{Negative, Positive}; use crate::num::{FracOp, Int, Sign, Unsigned}; -use core::cmp::{Ord, Ordering, PartialOrd}; -use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; /// Type representing a fraction /// @@ -79,9 +80,7 @@ impl Frac { /// # Panics /// if `d` is 0, this constructor will panic pub fn new_unreduced>(n: N, d: N) -> Frac { - if d.is_zero() { - panic!("Fraction can not have a zero denominator"); - } + assert!(!d.is_zero(), "Fraction can not have a zero denominator"); let mut sign = Positive; @@ -104,14 +103,12 @@ impl Frac { } } - /// Create a new rational from all the raw parts + /// Create a new, reduced rational from all the raw parts /// /// # Panics /// if `d` is 0, this constructor will panic fn raw(n: T, d: T, s: Sign) -> Frac { - if d.is_zero() { - panic!("Fraction can not have a zero denominator"); - } + assert!(!d.is_zero(), "Fraction can not have a zero denominator"); Frac { numerator: n,