space-units 0.1.2

Type-safe units of measure for aerospace quantities
Documentation
  • Coverage
  • 100%
    351 out of 351 items documented45 out of 47 items with examples
  • Size
  • Source code size: 319.68 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 47.35 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 20s Average build duration of successful builds.
  • all releases: 20s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • stateruntime/space-units
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • babilonczyk

space-units

Type-safe units of measure for aerospace quantities.

CI Crates.io Documentation MSRV License


The Problem

In 1999, NASA lost the Mars Climate Orbiter because one team produced pound-force-seconds and another expected newton-seconds. Cost: $327.6 million.

Most codebases still represent physical quantities as plain f64:

fn compute_orbit(radius: f64, velocity: f64, mass: f64) -> f64 {
    // Is radius in meters or km? Velocity in m/s or km/h?
    // The compiler has no idea. Neither does the next developer.
}

The Solution

use space_units::prelude::*;

let distance = 384_400.km();           // reads like "384,400 kilometers"
let time     = 3.5.hours();            // reads like "3.5 hours"
let velocity = distance / time;         // Length / Time  Velocity

// Read back in any unit
velocity.in_mps()                       // 30_514.3...
velocity.in_kmps()                      // 30.514...

// Only meaningful arithmetic compiles
let force = 1000.kg() * 9.81.mps2();   // Mass * Acceleration  Force
let accel = force / 500.kg();          // Force / Mass  Acceleration
// let oops = force + 500.kg();        //  COMPILE ERROR: can't add Force + Mass

Features

  • 40 quantity types covering mechanics, electromagnetics, thermodynamics, propulsion, radiation, and orbital mechanics
  • 80+ typed arithmetic operations so only physically valid math compiles
  • Zero dependencies and sub-1s compile time
  • no_std compatible for flight software
  • Const constructors for compile-time initialization

Why not uom?

space-units uom
Error messages "expected Length, found Mass" "PInt vs Z0" (typenum internals)
Syntax 384_400.km() Length::new::<kilometer>(384400.0)
Compile time < 1s ~6s
Const constructors Yes No (5+ years open)
nalgebra Clean wrappers (planned) Broken (4+ years open)
Dependencies 0 typenum, num-traits

Quick Start

use space_units::prelude::*;
use space_units::constants::*;

// Orbital velocity at ISS altitude
let r = 6_771.km();                         // Earth radius + 400 km
let v = (GM_EARTH / r).sqrt();              // sqrt(mu/r)  Velocity
println!("{:.2} km/s", v.in_kmps());        // "7.67 km/s"

// Propulsion
let isp = SpecificImpulse::from_s(311.0);   // Merlin 1D
let ve = isp.effective_exhaust_velocity();   // Isp * g0  Velocity
let mdot = 100.kgps();                      // mass flow rate
let thrust = mdot * ve;                     // MassFlowRate * Velocity  Force

// Electromagnetic
let v = 28.volts();
let i = 5.amperes();
let p = v * i;                              // Voltage * Current  Power

// All arithmetic works both ways
let scaled = 2.0 * distance;               // f64 * Quantity works
let ratio = distance / 100.km();           // Quantity / Quantity  f64
let mut d = distance;
d += 50.km();                               // compound assignment works

Supported Quantities

Mechanics

Length, Time, Mass, Velocity, Acceleration, Force, Impulse, Momentum

Angular Motion

Angle (with trig: sin/cos/tan/normalize), AngularVelocity, AngularAcceleration, Torque, AngularMomentum, MomentOfInertia, SolidAngle

Geometry

Area, Volume, Density

Energy & Thermodynamics

Energy, Power, Pressure, SpecificEnergy, Temperature (K/C/F/R), HeatFlux

Electromagnetic

Frequency (with period/wavelength), ElectricCurrent, Voltage, Resistance, ElectricCharge, MagneticFluxDensity, MagneticFlux, Capacitance, Inductance

Propulsion

SpecificImpulse, MassFlowRate

Orbital Mechanics

GravitationalParameter, SpecificAngularMomentum

Radiation

AbsorbedDose (Gy/rad), DoseEquivalent (Sv/rem)

Communications

DataRate

All types are accessible via use space_units::prelude::*. The NumericExt trait provides literal extensions on numeric types.

Typed Constants

use space_units::constants::*;

C            // Speed of light: Velocity
AU           // Astronomical unit: Length
G0           // Standard gravity: Acceleration
GM_EARTH     // Earth gravitational parameter: GravitationalParameter
GM_SUN       // Sun gravitational parameter
R_EARTH      // Earth mean radius: Length
M_EARTH      // Earth mass: Mass
// ... 34 total (all planets, Moon, fundamental constants)

Formatting

use space_units::prelude::*;

let d = 384_400.km();

// Default Display shows canonical unit
println!("{d}");                                        // "384400000 m"
println!("{d:.2}");                                     // "384400000.00 m"

// display_as() for specific units
println!("{}", d.display_as(LengthUnit::Kilometer));    // "384400 km"
println!("{:.1}", d.display_as(LengthUnit::AstronomicalUnit)); // "0.0 AU"

Installation

cargo add space-units

Or add to your Cargo.toml:

[dependencies]
space-units = "0.1"

Examples

Runnable examples in the examples/ directory:

cargo run --example basics              # Construction, conversion, arithmetic
cargo run --example orbital_mechanics   # ISS orbit, Hohmann transfer, escape velocity
cargo run --example propulsion          # Isp, thrust, Tsiolkovsky equation
cargo run --example electromagnetic     # Ohm's law, power, frequency/wavelength
cargo run --example formatting          # Display, display_as(), precision

Documentation

Security

Please report vulnerabilities privately. See SECURITY.md for details.

License

Licensed under the Apache License, Version 2.0. See LICENSE.