From ff9add85cd7e7e5ea44d65a0980a2793d9a37348 Mon Sep 17 00:00:00 2001 From: Aster Date: Wed, 27 Sep 2023 00:42:31 +0800 Subject: [PATCH] Convert number to primitive type --- projects/nyar-number/Cargo.toml | 2 +- projects/nyar-number/src/decimal/into.rs | 59 +++++++++++++++ projects/nyar-number/src/decimal/mod.rs | 1 + projects/nyar-number/src/integer/into.rs | 59 +++++++++++++++ projects/nyar-number/src/integer/mod.rs | 4 +- projects/nyar-number/src/lib.rs | 4 +- projects/nyar-number/src/number/into.rs | 87 +++++++++++++++++++++++ projects/nyar-number/src/number/mod.rs | 1 + projects/nyar-number/src/rational/into.rs | 59 +++++++++++++++ projects/nyar-number/src/rational/mod.rs | 4 +- projects/nyar-number/src/unsigned/into.rs | 59 +++++++++++++++ projects/nyar-number/src/unsigned/mod.rs | 3 +- 12 files changed, 334 insertions(+), 8 deletions(-) create mode 100644 projects/nyar-number/src/decimal/into.rs create mode 100644 projects/nyar-number/src/integer/into.rs create mode 100644 projects/nyar-number/src/number/into.rs create mode 100644 projects/nyar-number/src/rational/into.rs create mode 100644 projects/nyar-number/src/unsigned/into.rs diff --git a/projects/nyar-number/Cargo.toml b/projects/nyar-number/Cargo.toml index 345efb5..f674dc1 100644 --- a/projects/nyar-number/Cargo.toml +++ b/projects/nyar-number/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nyar-number" -version = "0.3.2" +version = "0.3.3" authors = ["Aster <192607617@qq.com>"] description = "Numeric types with GC optimization" repository = "https://github.com/nyar-vm/nyar-prime" diff --git a/projects/nyar-number/src/decimal/into.rs b/projects/nyar-number/src/decimal/into.rs new file mode 100644 index 0000000..7081300 --- /dev/null +++ b/projects/nyar-number/src/decimal/into.rs @@ -0,0 +1,59 @@ +use super::*; + +impl ToPrimitive for NyarDecimal { + fn to_isize(&self) -> Option { + self.delegate().to_isize() + } + + fn to_i8(&self) -> Option { + self.delegate().to_i8() + } + + fn to_i16(&self) -> Option { + self.delegate().to_i16() + } + + fn to_i32(&self) -> Option { + self.delegate().to_i32() + } + + fn to_i64(&self) -> Option { + self.delegate().to_i64() + } + + fn to_i128(&self) -> Option { + self.delegate().to_i128() + } + + fn to_usize(&self) -> Option { + self.delegate().to_usize() + } + + fn to_u8(&self) -> Option { + self.delegate().to_u8() + } + + fn to_u16(&self) -> Option { + self.delegate().to_u16() + } + + fn to_u32(&self) -> Option { + self.delegate().to_u32() + } + + fn to_u64(&self) -> Option { + self.delegate().to_u64() + } + + fn to_u128(&self) -> Option { + self.delegate().to_u128() + } + + fn to_f32(&self) -> Option { + self.delegate().to_f32() + } + + fn to_f64(&self) -> Option { + self.delegate().to_f64() + } +} diff --git a/projects/nyar-number/src/decimal/mod.rs b/projects/nyar-number/src/decimal/mod.rs index e47cf84..10a11cd 100644 --- a/projects/nyar-number/src/decimal/mod.rs +++ b/projects/nyar-number/src/decimal/mod.rs @@ -16,6 +16,7 @@ use std::{ mod arith; mod from; +mod into; #[cfg(feature = "serde")] mod der; diff --git a/projects/nyar-number/src/integer/into.rs b/projects/nyar-number/src/integer/into.rs new file mode 100644 index 0000000..c972ac3 --- /dev/null +++ b/projects/nyar-number/src/integer/into.rs @@ -0,0 +1,59 @@ +use super::*; + +impl ToPrimitive for NyarInteger { + fn to_isize(&self) -> Option { + todo!() + } + + fn to_i8(&self) -> Option { + todo!() + } + + fn to_i16(&self) -> Option { + todo!() + } + + fn to_i32(&self) -> Option { + todo!() + } + + fn to_i64(&self) -> Option { + todo!() + } + + fn to_i128(&self) -> Option { + todo!() + } + + fn to_usize(&self) -> Option { + todo!() + } + + fn to_u8(&self) -> Option { + todo!() + } + + fn to_u16(&self) -> Option { + todo!() + } + + fn to_u32(&self) -> Option { + todo!() + } + + fn to_u64(&self) -> Option { + todo!() + } + + fn to_u128(&self) -> Option { + todo!() + } + + fn to_f32(&self) -> Option { + todo!() + } + + fn to_f64(&self) -> Option { + todo!() + } +} diff --git a/projects/nyar-number/src/integer/mod.rs b/projects/nyar-number/src/integer/mod.rs index 7e97af6..5949823 100644 --- a/projects/nyar-number/src/integer/mod.rs +++ b/projects/nyar-number/src/integer/mod.rs @@ -1,5 +1,5 @@ use crate::unsigned::NyarUnsigned; -use num::{bigint::Sign, BigInt, BigUint, Num, One, Signed, Zero}; +use num::{bigint::Sign, BigInt, BigUint, Num, One, Signed, ToPrimitive, Zero}; use shredder::{ marker::{GcDrop, GcSafe}, Gc, Scan, Scanner, @@ -10,11 +10,11 @@ use std::{ ops::{Add, Div, Mul, Neg, Rem, Sub}, str::FromStr, }; - mod arith; #[cfg(feature = "serde")] mod der; mod from; +mod into; #[cfg(feature = "serde")] mod ser; diff --git a/projects/nyar-number/src/lib.rs b/projects/nyar-number/src/lib.rs index ad5a11a..31e6c86 100644 --- a/projects/nyar-number/src/lib.rs +++ b/projects/nyar-number/src/lib.rs @@ -5,12 +5,12 @@ #![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/91894079")] #![feature(lazy_cell)] +mod complex; mod decimal; mod integer; mod number; mod rational; mod sign; -mod complex; mod unsigned; pub(crate) mod utils; @@ -18,4 +18,4 @@ pub use self::{ decimal::NyarDecimal, integer::NyarInteger, number::NyarReal, rational::NyarRational, unsigned::NyarUnsigned, utils::NyarNumberError, }; -pub use num::traits::{One, Zero}; +pub use num::traits::{Num, One, ToPrimitive, Zero}; diff --git a/projects/nyar-number/src/number/into.rs b/projects/nyar-number/src/number/into.rs new file mode 100644 index 0000000..86e44d0 --- /dev/null +++ b/projects/nyar-number/src/number/into.rs @@ -0,0 +1,87 @@ +use super::*; +use num::ToPrimitive; + +impl ToPrimitive for NyarReal { + /// Used to act as an array index + fn to_isize(&self) -> Option { + match self { + NyarReal::Rational(v) => { + if v.denominator.get().is_one() { + v.numerator.get().to_isize() + } + else { + None + } + } + NyarReal::Decimal(_) => None, + } + } + + fn to_i8(&self) -> Option { + todo!() + } + + fn to_i16(&self) -> Option { + todo!() + } + + fn to_i32(&self) -> Option { + todo!() + } + + fn to_i64(&self) -> Option { + todo!() + } + + fn to_i128(&self) -> Option { + todo!() + } + /// Used to act as an array index + fn to_usize(&self) -> Option { + match self { + NyarReal::Rational(v) => { + if v.denominator.get().is_one() { + v.numerator.get().to_usize() + } + else { + None + } + } + NyarReal::Decimal(_) => None, + } + } + + fn to_u8(&self) -> Option { + todo!() + } + + fn to_u16(&self) -> Option { + todo!() + } + + fn to_u32(&self) -> Option { + todo!() + } + + fn to_u64(&self) -> Option { + todo!() + } + + fn to_u128(&self) -> Option { + todo!() + } + + fn to_f32(&self) -> Option { + match self { + NyarReal::Rational(v) => v.to_f32(), + NyarReal::Decimal(v) => v.to_f32(), + } + } + + fn to_f64(&self) -> Option { + match self { + NyarReal::Rational(v) => v.to_f64(), + NyarReal::Decimal(v) => v.to_f64(), + } + } +} diff --git a/projects/nyar-number/src/number/mod.rs b/projects/nyar-number/src/number/mod.rs index 730be30..16105e3 100644 --- a/projects/nyar-number/src/number/mod.rs +++ b/projects/nyar-number/src/number/mod.rs @@ -11,6 +11,7 @@ mod arith; #[cfg(feature = "serde")] mod der; mod from; +mod into; #[cfg(feature = "serde")] mod ser; diff --git a/projects/nyar-number/src/rational/into.rs b/projects/nyar-number/src/rational/into.rs new file mode 100644 index 0000000..5ad8ff7 --- /dev/null +++ b/projects/nyar-number/src/rational/into.rs @@ -0,0 +1,59 @@ +use super::*; + +impl ToPrimitive for NyarRational { + fn to_isize(&self) -> Option { + self.delegate().to_isize() + } + + fn to_i8(&self) -> Option { + self.delegate().to_i8() + } + + fn to_i16(&self) -> Option { + self.delegate().to_i16() + } + + fn to_i32(&self) -> Option { + self.delegate().to_i32() + } + + fn to_i64(&self) -> Option { + self.delegate().to_i64() + } + + fn to_i128(&self) -> Option { + self.delegate().to_i128() + } + + fn to_usize(&self) -> Option { + self.delegate().to_usize() + } + + fn to_u8(&self) -> Option { + self.delegate().to_u8() + } + + fn to_u16(&self) -> Option { + self.delegate().to_u16() + } + + fn to_u32(&self) -> Option { + self.delegate().to_u32() + } + + fn to_u64(&self) -> Option { + self.delegate().to_u64() + } + + fn to_u128(&self) -> Option { + self.delegate().to_u128() + } + + fn to_f32(&self) -> Option { + self.delegate().to_f32() + } + + fn to_f64(&self) -> Option { + self.delegate().to_f64() + } +} diff --git a/projects/nyar-number/src/rational/mod.rs b/projects/nyar-number/src/rational/mod.rs index 1b860f3..321dfcc 100644 --- a/projects/nyar-number/src/rational/mod.rs +++ b/projects/nyar-number/src/rational/mod.rs @@ -1,16 +1,16 @@ mod arith; mod from; +mod into; #[cfg(feature = "serde")] mod der; #[cfg(feature = "serde")] mod ser; - use crate::{ unsigned::{ONE, ZERO}, NyarInteger, NyarUnsigned, }; -use num::{bigint::Sign, BigInt, BigRational, BigUint, Num, One, Signed, Zero}; +use num::{bigint::Sign, BigInt, BigRational, BigUint, Num, One, Signed, ToPrimitive, Zero}; use shredder::{ marker::{GcDrop, GcSafe}, Gc, Scan, Scanner, diff --git a/projects/nyar-number/src/unsigned/into.rs b/projects/nyar-number/src/unsigned/into.rs new file mode 100644 index 0000000..d274d22 --- /dev/null +++ b/projects/nyar-number/src/unsigned/into.rs @@ -0,0 +1,59 @@ +use super::*; + +impl ToPrimitive for NyarUnsigned { + fn to_isize(&self) -> Option { + self._repr.to_isize() + } + + fn to_i8(&self) -> Option { + self._repr.to_i8() + } + + fn to_i16(&self) -> Option { + self._repr.to_i16() + } + + fn to_i32(&self) -> Option { + self._repr.to_i32() + } + + fn to_i64(&self) -> Option { + self._repr.to_i64() + } + + fn to_i128(&self) -> Option { + self._repr.to_i128() + } + + fn to_usize(&self) -> Option { + self._repr.to_usize() + } + + fn to_u8(&self) -> Option { + self._repr.to_u8() + } + + fn to_u16(&self) -> Option { + self._repr.to_u16() + } + + fn to_u32(&self) -> Option { + self._repr.to_u32() + } + + fn to_u64(&self) -> Option { + self._repr.to_u64() + } + + fn to_u128(&self) -> Option { + self._repr.to_u128() + } + + fn to_f32(&self) -> Option { + self._repr.to_f32() + } + + fn to_f64(&self) -> Option { + self._repr.to_f64() + } +} diff --git a/projects/nyar-number/src/unsigned/mod.rs b/projects/nyar-number/src/unsigned/mod.rs index 679cb4e..4d5707a 100644 --- a/projects/nyar-number/src/unsigned/mod.rs +++ b/projects/nyar-number/src/unsigned/mod.rs @@ -1,4 +1,4 @@ -use num::{BigUint, One, Zero}; +use num::{BigUint, One, ToPrimitive, Zero}; use shredder::{ marker::{GcDrop, GcSafe}, Gc, Scan, Scanner, @@ -15,6 +15,7 @@ mod arith; #[cfg(feature = "serde")] mod der; mod from; +mod into; #[cfg(feature = "serde")] mod ser;