Skip to content

Commit

Permalink
feat: basic impl of a u64 with const 9 decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverNChalk committed Sep 10, 2024
0 parents commit 39cc201
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/actions/rust-toolchain/action.yml
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 }}
77 changes: 77 additions & 0 deletions .github/workflows/ci.yml
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/logs
65 changes: 65 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions Cargo.toml
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
3 changes: 3 additions & 0 deletions README.md
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.
13 changes: 13 additions & 0 deletions clippy.toml
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.
'''
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "1.76.0"
10 changes: 10 additions & 0 deletions rustfmt.toml
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
72 changes: 72 additions & 0 deletions src/lib.rs
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);
}
}
10 changes: 10 additions & 0 deletions taplo.toml
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

0 comments on commit 39cc201

Please sign in to comment.