Skip to content

Commit

Permalink
Convert number to primitive type
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed Sep 26, 2023
1 parent 175f996 commit ff9add8
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 8 deletions.
2 changes: 1 addition & 1 deletion projects/nyar-number/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nyar-number"
version = "0.3.2"
version = "0.3.3"
authors = ["Aster <[email protected]>"]
description = "Numeric types with GC optimization"
repository = "https://github.com/nyar-vm/nyar-prime"
Expand Down
59 changes: 59 additions & 0 deletions projects/nyar-number/src/decimal/into.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use super::*;

impl ToPrimitive for NyarDecimal {
fn to_isize(&self) -> Option<isize> {
self.delegate().to_isize()
}

fn to_i8(&self) -> Option<i8> {
self.delegate().to_i8()
}

fn to_i16(&self) -> Option<i16> {
self.delegate().to_i16()
}

fn to_i32(&self) -> Option<i32> {
self.delegate().to_i32()
}

fn to_i64(&self) -> Option<i64> {
self.delegate().to_i64()
}

fn to_i128(&self) -> Option<i128> {
self.delegate().to_i128()
}

fn to_usize(&self) -> Option<usize> {
self.delegate().to_usize()
}

fn to_u8(&self) -> Option<u8> {
self.delegate().to_u8()
}

fn to_u16(&self) -> Option<u16> {
self.delegate().to_u16()
}

fn to_u32(&self) -> Option<u32> {
self.delegate().to_u32()
}

fn to_u64(&self) -> Option<u64> {
self.delegate().to_u64()
}

fn to_u128(&self) -> Option<u128> {
self.delegate().to_u128()
}

fn to_f32(&self) -> Option<f32> {
self.delegate().to_f32()
}

fn to_f64(&self) -> Option<f64> {
self.delegate().to_f64()
}
}
1 change: 1 addition & 0 deletions projects/nyar-number/src/decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::{

mod arith;
mod from;
mod into;

#[cfg(feature = "serde")]
mod der;
Expand Down
59 changes: 59 additions & 0 deletions projects/nyar-number/src/integer/into.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use super::*;

impl ToPrimitive for NyarInteger {
fn to_isize(&self) -> Option<isize> {
todo!()
}

fn to_i8(&self) -> Option<i8> {
todo!()
}

fn to_i16(&self) -> Option<i16> {
todo!()
}

fn to_i32(&self) -> Option<i32> {
todo!()
}

fn to_i64(&self) -> Option<i64> {
todo!()
}

fn to_i128(&self) -> Option<i128> {
todo!()
}

fn to_usize(&self) -> Option<usize> {
todo!()
}

fn to_u8(&self) -> Option<u8> {
todo!()
}

fn to_u16(&self) -> Option<u16> {
todo!()
}

fn to_u32(&self) -> Option<u32> {
todo!()
}

fn to_u64(&self) -> Option<u64> {
todo!()
}

fn to_u128(&self) -> Option<u128> {
todo!()
}

fn to_f32(&self) -> Option<f32> {
todo!()
}

fn to_f64(&self) -> Option<f64> {
todo!()
}
}
4 changes: 2 additions & 2 deletions projects/nyar-number/src/integer/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions projects/nyar-number/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
#![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;

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};
87 changes: 87 additions & 0 deletions projects/nyar-number/src/number/into.rs
Original file line number Diff line number Diff line change
@@ -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<isize> {
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<i8> {
todo!()
}

fn to_i16(&self) -> Option<i16> {
todo!()
}

fn to_i32(&self) -> Option<i32> {
todo!()
}

fn to_i64(&self) -> Option<i64> {
todo!()
}

fn to_i128(&self) -> Option<i128> {
todo!()
}
/// Used to act as an array index
fn to_usize(&self) -> Option<usize> {
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<u8> {
todo!()
}

fn to_u16(&self) -> Option<u16> {
todo!()
}

fn to_u32(&self) -> Option<u32> {
todo!()
}

fn to_u64(&self) -> Option<u64> {
todo!()
}

fn to_u128(&self) -> Option<u128> {
todo!()
}

fn to_f32(&self) -> Option<f32> {
match self {
NyarReal::Rational(v) => v.to_f32(),
NyarReal::Decimal(v) => v.to_f32(),
}
}

fn to_f64(&self) -> Option<f64> {
match self {
NyarReal::Rational(v) => v.to_f64(),
NyarReal::Decimal(v) => v.to_f64(),
}
}
}
1 change: 1 addition & 0 deletions projects/nyar-number/src/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod arith;
#[cfg(feature = "serde")]
mod der;
mod from;
mod into;
#[cfg(feature = "serde")]
mod ser;

Expand Down
59 changes: 59 additions & 0 deletions projects/nyar-number/src/rational/into.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use super::*;

impl ToPrimitive for NyarRational {
fn to_isize(&self) -> Option<isize> {
self.delegate().to_isize()
}

fn to_i8(&self) -> Option<i8> {
self.delegate().to_i8()
}

fn to_i16(&self) -> Option<i16> {
self.delegate().to_i16()
}

fn to_i32(&self) -> Option<i32> {
self.delegate().to_i32()
}

fn to_i64(&self) -> Option<i64> {
self.delegate().to_i64()
}

fn to_i128(&self) -> Option<i128> {
self.delegate().to_i128()
}

fn to_usize(&self) -> Option<usize> {
self.delegate().to_usize()
}

fn to_u8(&self) -> Option<u8> {
self.delegate().to_u8()
}

fn to_u16(&self) -> Option<u16> {
self.delegate().to_u16()
}

fn to_u32(&self) -> Option<u32> {
self.delegate().to_u32()
}

fn to_u64(&self) -> Option<u64> {
self.delegate().to_u64()
}

fn to_u128(&self) -> Option<u128> {
self.delegate().to_u128()
}

fn to_f32(&self) -> Option<f32> {
self.delegate().to_f32()
}

fn to_f64(&self) -> Option<f64> {
self.delegate().to_f64()
}
}
4 changes: 2 additions & 2 deletions projects/nyar-number/src/rational/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Loading

0 comments on commit ff9add8

Please sign in to comment.