mod sealed { /// A trait for defining the ratio of two units of time. /// /// This trait is used to implement the `per` method on the various structs. #[diagnostic::on_unimplemented(message = "`{Self}` is not an integer multiple of `{T}`")] pubtrait MultipleOf<T, Output> { /// The number of one unit of time in the other. const VALUE: Output;
}
/// A trait for defining the default output type for the `per` method. pubtrait DefaultOutput<T> { /// The default output type for the `per` method. type Output;
}
}
/// Given the list of types, stringify them as a list.
macro_rules! stringify_outputs {
(@inner $first:ty) => {
concat!("or `", stringify!($first), "`")
};
(@inner $first:ty, $($t:ty),+) => {
concat!(stringify_outputs!($first), ", ", stringify_outputs!(@inner $($t),+))
};
($first:ty) => {
concat!("`", stringify!($first), "`")
};
($($t:ty),+) => {
stringify_outputs!(@inner $($t),+)
};
}
// Split this out to a separate function to permit naming `T` while also using `impl Trait` as a // parameter in the public API.` constfn multiple_of_value<T, U, Output>(_: T) -> Output where
T: MultipleOf<U, Output> + Copy,
{
T::VALUE
}
/// Declare and implement `Per` for all relevant types. Identity implementations are automatic.
macro_rules! impl_per {
($($t:ident ($str:literal) per {$(
$larger:ident : [$default_output:ty]
$($int_output:ty)|+ = $int_value:expr;
$($float_output:ty)|+ = $float_value:expr;
)+})*) => {$( #[doc = concat!("A unit of time representing exactly one ", $str, ".")] #[derive(Debug, Clone, Copy)] pubstruct $t;
impl $t { #[doc = concat!("Obtain the number of times `", stringify!($t), "` can fit into `T`.")] #[doc = concat!("If `T` is smaller than `", stringify!($t), "`, the code will fail to")] /// compile. The return type is the smallest unsigned integer type that can represent /// the value. /// /// Valid calls: ///
$(#[doc = concat!( " - `", stringify!($t), "::per(", stringify!($larger), ")` (returns `",
stringify!($default_output), "`)"
)])+ #[inline] pubconstfn per<T>(_larger: T) -> <T as DefaultOutput<Self>>::Output where
T: MultipleOf<Self, T::Output> + DefaultOutput<Self> + Copy,
{
T::VALUE
}
#[doc = concat!("Obtain the number of times `", stringify!($t), "` can fit into `T`.")] #[doc = concat!("If `T` is smaller than `", stringify!($t), "`, the code will fail to")] /// compile. The return type is any primitive numeric type that can represent the value. /// /// Valid calls: ///
$(#[doc = concat!( " - `", stringify!($t), "::per(", stringify!($larger), ")` (returns ",
stringify_outputs!($($int_output),+ , $($float_output),+), ")"
)])+ #[inline] pubconstfn per_t<Output>(larger: impl MultipleOf<Self, Output> + Copy) -> Output {
multiple_of_value(larger)
}
}
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.