-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic impl of a u64 with const 9 decimals
- Loading branch information
0 parents
commit 39cc201
Showing
11 changed files
with
315 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: rust-toolchain | ||
inputs: | ||
toolchain: | ||
description: "Which rust toolchain to use" | ||
required: false | ||
|
||
runs: | ||
using: "composite" | ||
steps: | ||
- id: get-toolchain | ||
shell: bash | ||
run: | | ||
USER_OVERRIDE=${{ inputs.toolchain }} | ||
DEFAULT_TOOLCHAIN=$(grep channel rust-toolchain.toml | awk '{print $3}' | sed 's/"//g') | ||
TOOLCHAIN=${USER_OVERRIDE:-$DEFAULT_TOOLCHAIN} | ||
echo "toolchain=$TOOLCHAIN" >> $GITHUB_OUTPUT | ||
- uses: actions-rs/toolchain@v1 | ||
with: | ||
profile: minimal | ||
toolchain: ${{ steps.get-toolchain.outputs.toolchain }} | ||
override: true | ||
- uses: Swatinem/[email protected] | ||
with: | ||
shared-key: ${{ steps.get-toolchain.outputs.toolchain }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
name: ci-rust-push | ||
|
||
on: | ||
push: | ||
branches: | ||
- 'main' | ||
- 'master' | ||
|
||
jobs: | ||
skip-duplicates: | ||
runs-on: ubuntu-latest | ||
outputs: | ||
should_skip: ${{ steps.skip_check.outputs.should_skip }} | ||
steps: | ||
- id: skip_check | ||
uses: fkirc/skip-duplicate-actions@v5 | ||
with: | ||
concurrent_skipping: "same_content_newer" | ||
|
||
check: | ||
needs: skip-duplicates | ||
if: needs.skip-duplicates.outputs.should_skip != 'true' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: recursive | ||
- uses: ./.github/workflows/actions/rust-toolchain | ||
- run: cargo check --all-features | ||
|
||
test: | ||
needs: skip-duplicates | ||
if: needs.skip-duplicates.outputs.should_skip != 'true' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: recursive | ||
- uses: ./.github/workflows/actions/rust-toolchain | ||
- run: cargo test --all-features | ||
|
||
fmt: | ||
needs: skip-duplicates | ||
if: needs.skip-duplicates.outputs.should_skip != 'true' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: recursive | ||
- uses: ./.github/workflows/actions/rust-toolchain | ||
with: | ||
toolchain: nightly | ||
- run: rustup component add rustfmt | ||
- run: cargo fmt --all --check | ||
|
||
clippy: | ||
needs: skip-duplicates | ||
if: needs.skip-duplicates.outputs.should_skip != 'true' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: recursive | ||
- uses: ./.github/workflows/actions/rust-toolchain | ||
- run: rustup component add clippy | ||
- run: cargo clippy --all-features -- --deny warnings | ||
|
||
doc: | ||
needs: skip-duplicates | ||
if: needs.skip-duplicates.outputs.should_skip != 'true' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: recursive | ||
- uses: ./.github/workflows/actions/rust-toolchain | ||
- run: cargo doc --all-features --no-deps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
/logs |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[package] | ||
name = "const-decimal" | ||
version = "0.1.0" | ||
authors = ["Oliver Chalk"] | ||
edition = "2021" | ||
readme = "README.md" | ||
|
||
[lints.clippy] | ||
cast_possible_truncation = 'warn' | ||
cast_possible_wrap = 'warn' | ||
cast_sign_loss = 'warn' | ||
# See `clippy.toml`. | ||
disallowed_methods = 'warn' | ||
|
||
[features] | ||
serde = ["dep:serde"] | ||
|
||
[dependencies] | ||
serde = { version = "1.0.210", features = ["derive"], optional = true } | ||
|
||
[build-dependencies] | ||
|
||
[profile.release] | ||
opt-level = 3 | ||
debug = true | ||
|
||
[profile.paranoid] | ||
inherits = "release" | ||
overflow-checks = true | ||
debug-assertions = true | ||
|
||
[profile.performance] | ||
inherits = "release" | ||
lto = "fat" | ||
codegen-units = 1 | ||
incremental = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Const Point | ||
|
||
Fixed precision maths type implemented using compile time constants. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[[disallowed-methods]] | ||
path = "core::cmp::Ord::min" | ||
reason = ''' | ||
Too easy to misread `x.min(y)` as "let the minimum value of `x` be `y`" when it actually means the exact opposite; | ||
Use `std::cmp::min` instead. | ||
''' | ||
|
||
[[disallowed-methods]] | ||
path = "core::cmp::Ord::max" | ||
reason = ''' | ||
Too easy to misread `x.max(y)` as "let the maximum value of `x` be `y`" when it actually means the exact opposite; | ||
Use `std::cmp::max` instead. | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[toolchain] | ||
channel = "1.76.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
edition = "2021" | ||
error_on_line_overflow = true | ||
wrap_comments = true | ||
use_field_init_shorthand = true | ||
imports_granularity = "Module" | ||
condense_wildcard_suffixes = true | ||
format_strings = true | ||
group_imports = "StdExternalCrate" | ||
use_small_heuristics = "Max" | ||
chain_width = 60 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::ops::{Add, Div, Mul, Sub}; | ||
|
||
pub const PRECISION_FACTOR: u64 = 10u64.pow(9); | ||
|
||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct Decimal64(pub u64); | ||
|
||
impl Decimal64 { | ||
pub const ONE: Decimal64 = Decimal64(PRECISION_FACTOR); | ||
} | ||
|
||
impl Add for Decimal64 { | ||
type Output = Self; | ||
|
||
#[inline] | ||
fn add(self, rhs: Self) -> Self::Output { | ||
Decimal64(self.0 + rhs.0) | ||
} | ||
} | ||
|
||
impl Sub for Decimal64 { | ||
type Output = Self; | ||
|
||
#[inline] | ||
fn sub(self, rhs: Self) -> Self::Output { | ||
Decimal64(self.0 - rhs.0) | ||
} | ||
} | ||
|
||
impl Mul for Decimal64 { | ||
type Output = Self; | ||
|
||
#[inline] | ||
fn mul(self, rhs: Self) -> Self::Output { | ||
Decimal64(self.0 * rhs.0 / PRECISION_FACTOR) | ||
} | ||
} | ||
|
||
impl Div for Decimal64 { | ||
type Output = Self; | ||
|
||
#[inline] | ||
fn div(self, rhs: Self) -> Self::Output { | ||
Decimal64(self.0 * PRECISION_FACTOR / rhs.0) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn add() { | ||
assert_eq!(Decimal64::ONE + Decimal64::ONE, Decimal64(PRECISION_FACTOR * 2)); | ||
} | ||
|
||
#[test] | ||
fn sub() { | ||
assert_eq!(Decimal64::ONE - Decimal64::ONE, Decimal64(0)); | ||
} | ||
|
||
#[test] | ||
fn mul() { | ||
assert_eq!(Decimal64::ONE * Decimal64::ONE, Decimal64::ONE); | ||
} | ||
|
||
#[test] | ||
fn div() { | ||
assert_eq!(Decimal64::ONE / Decimal64::ONE, Decimal64::ONE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[formatting] | ||
column_width = 120 | ||
array_auto_expand = true | ||
allowed_blank_lines = 1 | ||
|
||
[[rule]] | ||
keys = ["dependencies", "dev-dependencies", "build-dependencies", "toolchain", "workspace.dependencies"] | ||
|
||
[rule.formatting] | ||
reorder_keys = true |