diff --git a/README.md b/README.md index a3b3aed..056e23f 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,8 @@ where both `a` and `b` are of the same type. ### Conversion +#### Type Casting + Any type that can be converted to a `u128` via the `Into` trait can similarly be converted to a `Wad` or `Ray` via the `Into` trait. Additionally, the following conversions from integer types are supported for `SignedWad` and `SignedRay` via the `Into` trait`: @@ -94,18 +96,37 @@ The following conversions from this library's types can also be performed via th - `Wad` -> `felt252`: Convert a `Wad` to a `felt252` without modifying the value - `Wad` -> `u256`: Convert a `Wad` to a `u256` without modifying the value - `Wad` -> `SignedWad`: Convert a `Wad` to a `SignedWad` without modifying the value, with the `sign` set to `false` -- `Wad` -> `SignedRay`: Multiply the `Wad` by 109 and return a `SignedRay` with the `sign` set to `false` -- `Ray` -> `Wad`: Divide the `Ray` value by 109 and return a `Wad` - `Ray` -> `SignedRay`: Convert a `Ray` to a `SignedRay` without modifying the value, with the `sign` set to `false` - `SignedWad` -> `felt252`: Convert a `SignedWad` to a `felt252` without modifying the value - `SignedRay` -> `felt252`: Convert a `SignedRay` to a `felt252` without modifying the value The following conversions can be performed via the `TryInto` trait: -- `Wad` -> `Option::`: Multiply the `Wad` value by 109 and return `Option::Some` if there is no overflow or `Option::None` otherwise - `u256` -> `Option::`: Returns `Option::Some` if the value is within bounds of `u128` or `Option::None` otherwise - `SignedWad` -> `Option::`: Returns `Option::Some` if `sign` is false or `Option::None` otherwise - `SignedRay` -> `Option::`: Returns `Option::Some` if `sign` is false or `Option::None` otherwise +#### Scaling + +The following functions can be used to scale between `Wad` and `Ray`: +- `fn ray_to_wad(x: Ray) -> Wad`: Divide the `Ray` value by 109 and return a `Wad` +- `fn wad_to_ray(x: Wad) -> Option::`: Multiply the `Wad` value by 109 and return `Option::Some` if there is no overflow or `Option::None` otherwise + +#### Additional notes on conversion between `Wad` and `Ray` + +##### Overview + +Starting from `v0.5.0` of this library, we have made significant changes to how `Wad` values are converted to `Ray` values and vice versa. This aims to improve type safety and align with the semantics of Rust's `Into` trait. + +##### Key Changes + +1. Previously, `Into` scaled the value by 109 while `Into` scaled the value by 10-9. Both now perform direct type cast without value modification +2. Introduced `wad_to_ray()` and `ray_to_wad()` functions for value-preserving conversions + +##### Recommended Usage + +1. Prefer `wad_to_ray()` and `ray_to_wad()` for most conversions between `Wad` and `Ray`. +2. Use the `Into` trait only when a simple type cast is required (expected to be rare). + ### Signed The following functions are available for `SignedWad` and `SignedRay` via the `Signed` trait: @@ -118,7 +139,7 @@ To use this library, add the repository as a dependency in your `Scarb.toml`: ``` [dependencies] -wadray = { git = "https://github.com/lindy-labs/wadray.git" } +wadray = "0.5.0" ``` then add the following line in your `.cairo` file ```cairo diff --git a/Scarb.lock b/Scarb.lock index 0860951..5e35588 100644 --- a/Scarb.lock +++ b/Scarb.lock @@ -8,7 +8,7 @@ source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.27.0#2d9 [[package]] name = "wadray" -version = "0.3.0" +version = "0.5.0" dependencies = [ "snforge_std", ] diff --git a/Scarb.toml b/Scarb.toml index ed0bbc7..3a7fe3e 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -1,6 +1,6 @@ [package] name = "wadray" -version = "0.3.0" +version = "0.5.0" cairo-version = "2.7.0" edition = "2024_07" authors = ["Lindy Labs"]