dana_macros

Crates.iodana_macros
lib.rsdana_macros
version0.4.0
sourcesrc
created_at2024-08-05 01:24:48.011906
updated_at2024-08-10 23:49:34.74573
descriptionProcedural macro crate for `dana`.
homepage
repositoryhttps://github.com/jsdreim/dana
max_upload_size
id1325397
size47,033
JS Dreim (jsdreim)

documentation

https://docs.rs/dana

README

Dana

Crates.io docs.rs

Compile-time dimensional analysis via generic types.

Overview

Dimensional analysis is a method to ensure the correctness of calculations, by tracking the relationships between units. This can also be useful in working out relationships without having to look them up. For example, say you have a time t (T) and a speed v (L/T), and you want to find a distance d (L). Simple algebra can tell you that L = (L/T) × T, and accordingly, d = v × t.

This library implements these checks using Rust's static type system. As a result, any incompatibility between units becomes a compile-time error, ensuring that all code is dimensionally sound.


This function compiles successfully:

use dana::{Quantity, units::{Length, Speed, Time}};

fn speed(dist: Quantity<Length>, time: Quantity<Time>) -> Quantity<Speed> {
    dist / time
}

Whereas this function, because Speed is defined as UnitDiv<Length, Time>, will not compile, raising a mismatched types error because the expression is the wrong way around:

use dana::{Quantity, units::{Length, Speed, Time}};

fn speed(dist: Quantity<Length>, time: Quantity<Time>) -> Quantity<Speed> {
    time / dist
}
error[E0308]: mismatched types
 --> src/main.rs:4:5
  |
3 | fn speed(dist: Quantity<Length>, time: Quantity<Time>) -> Quantity<Speed> {
  |                                                           --------------- expected `Quantity<UnitDiv<Length, Time>>` because of return type
4 |     time / dist
  |     ^^^^^^^^^^^ expected `Quantity<UnitDiv<Length, Time>>`, found `Quantity<UnitDiv<Time, Length>>`
  |
  = note: expected struct `Quantity<UnitDiv<Length, Time>>`
             found struct `Quantity<UnitDiv<Time, Length>>`

See the library documentation for further information.

Cargo Features

The following Cargo features are available to enable additional functionality:

  • chrono: Support for conversion between Quantity<Time> and chrono::TimeDelta
  • rand: Support for rand sampling traits
  • serde: Support for serde traits
  • simd: Experimental QtySimd arrays using the unstable portable_simd feature
Commit count: 0

cargo fmt