Skip to content

Rust Macros for Writing Numbers for Generic Functions

License

Notifications You must be signed in to change notification settings

p-sira/num-lazy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

num-lazy

License Crate Documentation

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.

fn circumference<T: Float>(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.

fn circumference<T: Float>(radius: T) -> T {
    two!() * pi!() * radius
}

Quick Start

Install num-lazy by:

>> cargo add num-lazy

Use declare_nums!{T} to bind num-lazy to generic type T.

use num_lazy::declare_nums;
use num_traits::Float;
declare_nums!{T}

fn circumference<T: Float>(radius: T) -> T {
    two!() * pi!() * radius
}

fn main() {
    assert!(circumference(1.0_f64) == 6.283185307179586);
}

About

Rust Macros for Writing Numbers for Generic Functions

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages