Skip to content

Commit

Permalink
Adopt decimal to no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-cichocki committed Oct 31, 2023
1 parent 8e03e7f commit 5a9b644
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 55 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ license = "MIT"
description = "This is a Rust fixed-point numeric library targeting blockchain development. Originally created and used as a part of the Invariant Protocol. The current version leverages macros, traits and generics to exchange dozens of lines of error prone code with a single line and generating the rest."

[dependencies]
safe_decimal_core = { path = "decimal_core"}
# checked_decimal_macro_core = "0.1.4"
safe_decimal_core = { path = "decimal_core" }
integer-sqrt = "0.1.5"
uint = "0.9"
num-traits = "0.2.14"
uint = { version = "0.9", default-features = false }
num-traits = { version = "0.2.14", default-features = false }
43 changes: 23 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
mod traits;
mod uint;
mod walkthrough;
#![no_std]

pub use crate::uint::U256;
extern crate alloc;

mod traits;
mod uint;
pub use crate::uint::{checked_u320_to_u256, to_u256, u256_to_u320, U256, U320};
pub use num_traits;
pub use safe_decimal_core::decimal;

pub use traits::*;

#[cfg(test)]
#[decimal(3, u128)]
#[derive(Default, std::fmt::Debug, Clone, Copy, PartialEq)]
struct R(u32);
pub mod tests {
use super::*;
use crate::alloc::string::ToString;

#[cfg(test)]
#[decimal(1)]
#[derive(Default, std::fmt::Debug, Clone, Copy, PartialEq)]
struct Q {
v: u16,
}
#[cfg(test)]
#[decimal(3, u128)]
#[derive(Default, Debug, Clone, Copy, PartialEq)]
struct R(u32);

#[cfg(test)]
#[decimal(0)]
#[derive(Default, std::fmt::Debug, Clone, Copy, PartialEq)]
struct N(u8);
#[cfg(test)]
#[decimal(1)]
#[derive(Default, Debug, Clone, Copy, PartialEq)]
struct Q {
v: u16,
}

#[cfg(test)]
pub mod tests {
use super::*;
#[cfg(test)]
#[decimal(0)]
#[derive(Default, Debug, Clone, Copy, PartialEq)]
struct N(u8);

#[test]
fn test_from_decimal() {
Expand Down
28 changes: 15 additions & 13 deletions src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::fmt::Debug;
use core::fmt::{Debug, Display};

use alloc::string::String;

pub trait Decimal {
type U: Debug + Default;
Expand All @@ -10,17 +12,17 @@ pub trait Decimal {
fn here<Y: TryFrom<Self::U>>(&self) -> Y;
fn scale() -> u8;
fn one<T: TryFrom<u128>>() -> T;
fn checked_one<T: TryFrom<u128>>() -> std::result::Result<T, String>
fn checked_one<T: TryFrom<u128>>() -> Result<T, String>
where
T::Error: std::fmt::Display;
T::Error: Display;
fn almost_one<T: TryFrom<u128>>() -> T;
}

pub trait BigOps<T>: Sized {
fn big_mul(self, rhs: T) -> Self;
fn big_mul_up(self, rhs: T) -> Self;
fn big_div(self, rhs: T) -> Self;
fn checked_big_div(self, rhs: T) -> std::result::Result<Self, String>;
fn checked_big_div(self, rhs: T) -> Result<Self, String>;
fn big_div_up(self, rhs: T) -> Self;
}

Expand All @@ -36,13 +38,13 @@ pub trait OthersSameType {
pub trait Factories<T>: Sized {
fn from_integer(integer: T) -> Self;
fn from_scale(integer: T, scale: u8) -> Self;
fn checked_from_scale(integer: T, scale: u8) -> std::result::Result<Self, String>;
fn checked_from_scale(integer: T, scale: u8) -> Result<Self, String>;
fn from_scale_up(integer: T, scale: u8) -> Self;
}

pub trait BetweenDecimals<T>: Sized {
fn from_decimal(other: T) -> Self;
fn checked_from_decimal(other: T) -> std::result::Result<Self, String>;
fn checked_from_decimal(other: T) -> Result<Self, String>;
fn from_decimal_up(other: T) -> Self;
}

Expand All @@ -52,22 +54,22 @@ pub trait ToValue<T, B> {
}

pub trait FactoriesToValue<T, B> {
fn checked_from_scale_to_value(integer: T, scale: u8) -> std::result::Result<B, String>;
fn checked_from_scale_to_value(integer: T, scale: u8) -> Result<B, String>;
}

pub trait BetweenDecimalsToValue<T, B> {
fn checked_from_decimal_to_value(other: T) -> std::result::Result<B, String>;
fn checked_from_decimal_to_value(other: T) -> Result<B, String>;
}

pub trait ByNumber<B>: Sized {
fn big_div_by_number(self, number: B) -> Self;
fn big_div_by_number_up(self, number: B) -> Self;
fn checked_big_div_by_number(self, number: B) -> std::result::Result<Self, String>;
fn checked_big_div_by_number_up(self, number: B) -> std::result::Result<Self, String>;
fn checked_big_div_by_number(self, number: B) -> Result<Self, String>;
fn checked_big_div_by_number_up(self, number: B) -> Result<Self, String>;
}

pub trait CheckedOps: Sized {
fn checked_add(self, rhs: Self) -> std::result::Result<Self, String>;
fn checked_sub(self, rhs: Self) -> std::result::Result<Self, String>;
fn checked_div(self, rhs: Self) -> std::result::Result<Self, String>;
fn checked_add(self, rhs: Self) -> Result<Self, String>;
fn checked_sub(self, rhs: Self) -> Result<Self, String>;
fn checked_div(self, rhs: Self) -> Result<Self, String>;
}
45 changes: 27 additions & 18 deletions src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,43 @@ construct_uint! {
}

#[allow(dead_code)]
pub const fn to_u256(n: u128) -> U256 {
U256([n as u64, (n >> 64) as u64, 0, 0])
}
pub fn checked_u320_to_u256(n: U320) -> Option<U256> {
if !(n >> 256).is_zero() {
return None;
}

#[allow(dead_code)]
pub fn u256_to_u320(n: U256) -> U320 {
U320([
Some(U256([
n.low_u64(),
(n >> 64).low_u64(),
(n >> 128).low_u64(),
(n >> 192).low_u64(),
0,
])
]))
}

#[allow(dead_code)]
pub fn to_u320(n: u128) -> U320 {
u256_to_u320(to_u256(n))
pub fn u320_to_u256(n: U320) -> U256 {
checked_u320_to_u256(n).unwrap()
}

#[allow(dead_code)]
pub fn checked_u320_to_u256(n: U320) -> Option<U256> {
if !(n >> 256).is_zero() {
return None;
}
pub const fn to_u256(n: u128) -> U256 {
U256([n as u64, (n >> 64) as u64, 0, 0])
}

Some(U256([
#[allow(dead_code)]
pub fn u256_to_u320(n: U256) -> U320 {
U320([
n.low_u64(),
(n >> 64).low_u64(),
(n >> 128).low_u64(),
(n >> 192).low_u64(),
]))
0,
])
}

#[allow(dead_code)]
pub fn u320_to_u256(n: U320) -> U256 {
checked_u320_to_u256(n).unwrap()
pub fn to_u320(n: u128) -> U320 {
u256_to_u320(to_u256(n))
}

#[cfg(test)]
Expand Down Expand Up @@ -153,6 +153,15 @@ mod tests {
}
}

#[test]
fn test_u320_methods() {
let _max = U320::MAX;
let _from = U320::from(10);
let zero = U320::zero();
let is_zero = zero.is_zero();
assert!(is_zero);
}

#[test]
fn test_u320_to_u256() {
let max_u256 = U320([u64::MAX, u64::MAX, u64::MAX, u64::MAX, 0]);
Expand Down

0 comments on commit 5a9b644

Please sign in to comment.