https://github.com/p-sira/num-lazy
Rust Macros for Writing Numbers for Generic Functions
https://github.com/p-sira/num-lazy
macro num numerics rust
Last synced: about 1 month ago
JSON representation
Rust Macros for Writing Numbers for Generic Functions
- Host: GitHub
- URL: https://github.com/p-sira/num-lazy
- Owner: p-sira
- License: bsd-3-clause
- Created: 2025-02-08T10:20:48.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-02-14T15:39:01.000Z (3 months ago)
- Last Synced: 2025-03-10T09:51:28.240Z (2 months ago)
- Topics: macro, num, numerics, rust
- Language: Rust
- Homepage:
- Size: 850 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# num-lazy
**num-lazy** helps you write numbers for generic-typed functions, reduce typing, and improve readability!
## Why Num-Lazy
Let's write a generic circumference function using `num-trait`.
```rust
fn circumference(radius: T) -> T {
T::from(2.0).unwrap() * T::from(std::f64::consts::PI).unwrap() * radius
}
```
This doesn't look too bad. But you can imagine it getting out of hand for more complex functions. This is where num-lazy comes to the rescue! Let's implement using `num-lazy`.```rust
fn circumference(radius: T) -> T {
two!() * pi!() * radius
}
```## Quick Start
Install num-lazy by:
```shell
>> cargo add num-lazy
```Use `declare_nums!{T}` to bind num-lazy to generic type `T`.
```rust
use num_lazy::declare_nums;
use num_traits::Float;
declare_nums!{T}fn circumference(radius: T) -> T {
two!() * pi!() * radius
}fn main() {
assert!(circumference(1.0_f64) == 6.283185307179586);
}
```