millimeter

Crates.iomillimeter
lib.rsmillimeter
version0.1.0
sourcesrc
created_at2022-01-18 14:44:19.65082
updated_at2022-01-18 14:44:19.65082
descriptionPrimitive type with millimeter unit attached
homepage
repositoryhttps://github.com/kicad-rs/millimeter
max_upload_size
id516226
size14,954
(msrd0)

documentation

README

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.

Commit count: 14

cargo fmt