#meter #unit #mm #inch

no-std millimeter

Primitive type with millimeter unit attached

1 unstable release

0.1.0 Jan 18, 2022

#1437 in Math

0BSD license

15KB
312 lines

millimeter License 0BSD GitHub

This crate provides mm and mm2 newtype structs. These can be used both as an indication that a value is expected to have a certain unit, as well as to prove at compile time that your computation yields the unit you expect it to.

Example

use millimeter::{mm, mm2, Unit};

#[derive(Clone, Copy, Default)]
pub struct Point {
	x: mm,
	y: mm
}

#[derive(Clone, Copy)]
pub struct Rectangle {
	top_left: Point,
	bottom_right: Point
}

impl Rectangle {
	pub fn one_inch_square(top_left: Point) -> Self {
		Self {
			top_left,
			bottom_right: Point {
				x: top_left.x + 1.0.inch(),
				y: top_left.y + 1.0.inch()
			}
		}
	}

	pub fn area(&self) -> mm2 {
		(self.bottom_right.x - self.top_left.x) * (self.bottom_right.y - self.top_left.y)
	}

	pub fn diagonal_len(&self) -> mm {
		let a = self.bottom_right.x - self.top_left.x;
		let b = self.bottom_right.y - self.top_left.y;
		(a*a + b*b).sqrt()
	}
}

License

Licensed under the BSD Zero Clause License.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache License, Version 2.0, shall be licensed as above, without any additional terms or conditions.

Dependencies

~175KB