// Copyright 2014 The Servo Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! A one-dimensional length, tagged with its units.
usecrate::num::One; #[cfg(feature = "bytemuck")] use bytemuck::{Pod, Zeroable}; use core::cmp::Ordering; use core::fmt; use core::hash::{Hash, Hasher}; use core::iter::Sum; use core::marker::PhantomData; use core::ops::{Add, Div, Mul, Neg, Sub}; use core::ops::{AddAssign, DivAssign, MulAssign, SubAssign}; use num_traits::{NumCast, Saturating}; #[cfg(feature = "serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// A one-dimensional distance, with value represented by `T` and unit of measurement `Unit`. /// /// `T` can be any numeric type, for example a primitive type like `u64` or `f32`. /// /// `Unit` is not used in the representation of a `Length` value. It is used only at compile time /// to ensure that a `Length` stored with one unit is converted explicitly before being used in an /// expression that requires a different unit. It may be a type without values, such as an empty /// enum. /// /// You can multiply a `Length` by a `scale::Scale` to convert it from one unit to /// another. See the [`Scale`] docs for an example. /// /// [`Scale`]: struct.Scale.html #[repr(C)] pubstruct Length<T, Unit>(pub T, #[doc(hidden)] pub PhantomData<Unit>);
#[test] fn test_length_serde() { let one_cm: Length<f32, Mm> = Length::new(10.0);
assert_tokens(&one_cm, &[Token::F32(10.0)]);
}
}
#[test] fn test_clone() { // A cloned Length is a separate length with the state matching the // original Length at the point it was cloned. letmut variable_length: Length<f32, Inch> = Length::new(12.0);
let one_foot = variable_length.clone();
variable_length.0 = 24.0;
#[test] fn test_saturating_add() { let length1: Length<u8, Mm> = Length::new(250); let length2: Length<u8, Mm> = Length::new(6);
let result = length1.saturating_add(length2);
assert_eq!(result.get(), 255);
}
#[test] fn test_saturating_sub() { let length1: Length<u8, Mm> = Length::new(5); let length2: Length<u8, Mm> = Length::new(10);
let result = length1.saturating_sub(length2);
assert_eq!(result.get(), 0);
}
#[test] fn test_division_by_length() { // Division results in a Scale from denominator units // to numerator units. let length: Length<f32, Cm> = Length::new(5.0); let duration: Length<f32, Second> = Length::new(10.0);
let result = length / duration;
let expected: Scale<f32, Second, Cm> = Scale::new(0.5);
assert_eq!(result, expected);
}
#[test] fn test_multiplication() { let length_mm: Length<f32, Mm> = Length::new(10.0); let cm_per_mm: Scale<f32, Mm, Cm> = Scale::new(0.1);
let result = length_mm * cm_per_mm;
let expected: Length<f32, Cm> = Length::new(1.0);
assert_eq!(result, expected);
}
#[test] fn test_multiplication_with_scalar() { let length_mm: Length<f32, Mm> = Length::new(10.0);
let result = length_mm * 2.0;
let expected: Length<f32, Mm> = Length::new(20.0);
assert_eq!(result, expected);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.