Skip to content

Commit

Permalink
Added Build.md with build instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
paupino committed Jul 28, 2021
1 parent 5b1da93 commit 76f1c91
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 26 deletions.
114 changes: 114 additions & 0 deletions BUILD.md
@@ -0,0 +1,114 @@
Developing Rust Decimal
=======================

* [Setup](#setup)
* [Building](#building)
* [Library Versioning](#library-versioning)
* [Formatting / Code Style](#formatting--code-style)
* [Testing](#testing)
* [Fuzzing](#fuzzing)
* [Benchmarking](#benchmarking)
* [Code Coverage](#code-coverage)

## Setup

**Minimum Rust Version:** `1.46.0`

Rust Decimal leverages [cargo make](https://github.com/sagiegurari/cargo-make) to ensure a consistent build/test/release
approach. This also handles installing any additional dependencies in order to execute various commands, making getting set
up and going relatively straight forward and easy.

Installing this can be done using `cargo`:

```shell
cargo install --force cargo-make
```

Once this has been installed, common tasks can be initiated with the `makers` command.

## Building

**Useful Make Commands:** `makers build`

Building this library doesn't require any special commands and can be performed using a standard `cargo build`.

### Building no-std

Builds by default leverage `std`. To disable this functionality you can build Rust Decimal without any default features
enabled:

```shell
cargo build --no-default-features
```

## Library Versioning

**Useful Make Commands:** `makers outdated`

The general rule of thumb with Rust Decimal is to ensure that downstream dependencies are kept up to date, so long as they
align with the minimum requirements of the Rust Decimal build system. As a helpful measure, a `makers` command has been created
to identify any outdated dependencies within the project. On a regular basis this command is run and results evaluated for potential
updates to be applied.

Please note: because this is a library, the `Cargo.lock` file is not committed into the repository. This means that `cargo update`
should be run before running `makers outdated`.

## Formatting / Code Style

**Useful Make Commands:** `makers format`, `makers clippy`

To maintain consistent styling across the project, both `clippy` and `cargo fmt` are used. Github actions will
check for consistent styling upon pull request however it is recommended that both of these commands are run before creating
a PR.

## Testing

**Useful Make Commands:** `makers test-all`, `makers test-no-std`, `makers test-maths`, `makers test-serde`, `makers test-db`

Testing is a critical part of the Rust Decimal development lifecycle. It helps ensure that the library is behaving correctly under
a mixture of different inputs and feature configurations.

There are many different test configurations defined in `Makefile.toml` to help ensure that common test cases are easy to execute locally.
For the full complete list, please review `Makefile.toml` directly for all `test-*` tasks. Some useful test tasks are listed below:

* `test-all`: Tests all test configurations against all features
* `test-default`: Test the default configuration
* `test-no-std`: Test the library against a `no-std` build
* `test-maths`: Test the maths feature of the library using both legacy and default ops.
* `test-serde`: Test serialization and deserialization using a mixture of serde features
* `test-db`: Test a variety of database logic changes, including `diesel` and `postgres` configurations.

When adding new features, please make sure to generate new tasks within the `Makefile.toml` to ensure continued
coverage of your feature going forward.

### Generated tests

Rust Decimal includes a number of generated tests that have been generated outside the scope of this project. These are
effectively CSV files that use randomized data for the `lo`, `mid` and `hi` portions of the Decimal number.

Modification of these files is restricted and should not be committed. If you would like to add further generated tests then
please create new files as you see fit.

## Fuzzing

**Useful Make Commands:** `makers fuzz`

Rust Decimal includes some fuzz testing support also to help capture a wider range of testing scenarios. These can be
run by using `makers fuzz`.

## Benchmarking

**Useful Make Commands:** `makers bench`

In order to ensure that Rust Decimal runs quickly, a number of benchmarks have been created and can be executed using
`makers bench`.

When adding new benchmarking configurations please ensure that you add a corresponding `Makefile.toml` task to capture the
updated configuration.

## Code Coverage

**Useful Make Commands:** `makers coverage`, `makers codecov-open`

Limited code coverage support has been added to Rust Decimal using `gcov`. A code coverage report
can be generated by running `makers coverage`. Once generated, the report can be opened using `makers codecov-open`.
28 changes: 17 additions & 11 deletions Makefile.toml
Expand Up @@ -8,6 +8,23 @@ FUZZ_RUNS = 10000000
command = "cargo"
args = ["build"]

[tasks.format]
workspace = true
install_crate = "rustfmt"
command = "cargo"
args = ["fmt", "--", "--emit=files"]

[tasks.clippy]
workspace = true
install_crate = "clippy"
command = "cargo"
args = ["clippy"]

[tasks.outdated]
install_crate = "cargo-outdated"
command = "cargo"
args = ["outdated", "-R"]

[tasks.bench]
toolchain = "nightly"
command = "cargo"
Expand Down Expand Up @@ -73,12 +90,6 @@ dependencies = ["test-no-std"]
command = "cargo"
args = ["test"]

[tasks.format]
workspace = true
install_crate = "rustfmt"
command = "cargo"
args = ["fmt", "--", "--emit=files"]

[tasks.fuzz]
dependencies = [
"fuzz-arithmetic",
Expand All @@ -97,11 +108,6 @@ install_crate = "cargo-fuzz"
command = "cargo"
args = ["fuzz", "run", "constructors", "--", "-runs=${FUZZ_RUNS}"]

[tasks.outdated]
install_crate = "cargo-outdated"
command = "cargo"
args = ["outdated", "-R"]

[tasks.test-all]
dependencies = [
"test-no-std",
Expand Down
26 changes: 15 additions & 11 deletions README.md
Expand Up @@ -85,39 +85,39 @@ assert_eq!(total.to_string(), "27.26");
* [serde-arbitrary-precision](#serde-arbitrary-precision)
* [std](#std)

## `c-repr`
### `c-repr`

Forces `Decimal` to use `[repr(C)]`. The corresponding target layout is 128 bit aligned.

## `db-postgres`
### `db-postgres`

This feature enables a PostgreSQL communication module. It allows for reading and writing the `Decimal`
type by transparently serializing/deserializing into the `NUMERIC` data type within PostgreSQL.

## `db-tokio-postgres`
### `db-tokio-postgres`

Enables the tokio postgres module allowing for async communication with PostgreSQL.

## `db-diesel-postgres`
### `db-diesel-postgres`

Enable `diesel` PostgreSQL support.

## `legacy-ops`
### `legacy-ops`

As of `1.10` the algorithms used to perform basic operations have changed which has benefits of significant speed improvements.
To maintain backwards compatibility this can be opted out of by enabling the `legacy-ops` feature.

## `maths`
### `maths`

The `maths` feature enables additional complex mathematical functions such as `pow`, `ln`, `enf`, `exp` etc.
Documentation detailing the additional functions can be found on the
[`MathematicalOps`](https://docs.rs/rust_decimal/latest/rust_decimal/trait.MathematicalOps.html) trait.

## `rust-fuzz`
### `rust-fuzz`

Enable `rust-fuzz` support by implementing the `Arbitrary` trait.

## `serde-float`
### `serde-float`

Enable this so that JSON serialization of `Decimal` types are sent as a float instead of a string (default).

Expand All @@ -128,7 +128,7 @@ e.g. with this turned on, JSON serialization would output:
}
```

## `serde-str`
### `serde-str`

This is typically useful for `bincode` or `csv` like implementations.

Expand All @@ -140,14 +140,18 @@ If, for some reason, you also have `serde-float` enabled then this will use `des
converting to `f64` _loses_ precision, it's highly recommended that you do NOT enable this feature when working with
`bincode`. That being said, this will only use 8 bytes so is slightly more efficient in terms of storage size.

## `serde-arbitrary-precision`
### `serde-arbitrary-precision`

This is used primarily with `serde_json` and consequently adds it as a "weak dependency". This supports the
`arbitrary_precision` feature inside `serde_json` when parsing decimals.

This is recommended when parsing "float" looking data as it will prevent data loss.

## `std`
### `std`

Enable `std` library support. This is enabled by default, however in the future will be opt in. For now, to support `no_std`
libraries, this crate can be compiled with `--no-default-features`.

## Building

Please refer to the [Build document](BUILD.md) for more information on building and testing Rust Decimal.
6 changes: 2 additions & 4 deletions src/ops/rem.rs
Expand Up @@ -182,10 +182,8 @@ fn rem_full(d1: &Dec64, d2: &Buf12, scale: i32) -> CalculationResult {
// TODO: Optimize slice logic

let mut tmp = Buf16::zero();
let divisor = d2.low64() << shift;
if d2.hi() == 0 {
// 64 bit divisor so we adjust accordingly
let divisor = d2.low64() << shift;

// Do some division
if upper == 6 {
upper -= 1;
Expand Down Expand Up @@ -232,7 +230,7 @@ fn rem_full(d1: &Dec64, d2: &Buf12, scale: i32) -> CalculationResult {
d1.scale,
))
} else {
let divisor_low64 = d2.low64() << shift;
let divisor_low64 = divisor;
let divisor = Buf12 {
data: [
divisor_low64 as u32,
Expand Down

0 comments on commit 76f1c91

Please sign in to comment.