2017-02-13 01:17:09 +08:00
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
|
|
|
//! Traits and tags for identifying the dimension of all algebraic entities.
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
use std::any::{Any, TypeId};
|
2017-08-03 01:37:44 +08:00
|
|
|
use std::cmp;
|
2018-08-13 12:48:55 +08:00
|
|
|
use std::fmt::Debug;
|
2018-02-02 19:26:35 +08:00
|
|
|
use std::ops::{Add, Div, Mul, Sub};
|
2021-01-03 22:20:34 +08:00
|
|
|
use typenum::{self, Diff, Max, Maximum, Min, Minimum, Prod, Quot, Sum, Unsigned};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-04-12 18:14:16 +08:00
|
|
|
#[cfg(feature = "serde-serialize-no-std")]
|
2018-02-02 19:26:35 +08:00
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
2017-05-04 10:02:30 +08:00
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Dim of dynamically-sized algebraic entities.
|
2020-01-26 03:17:49 +08:00
|
|
|
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
|
2016-12-05 05:44:42 +08:00
|
|
|
pub struct Dynamic {
|
2018-02-02 19:26:35 +08:00
|
|
|
value: usize,
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Dynamic {
|
|
|
|
/// A dynamic size equal to `value`.
|
|
|
|
#[inline]
|
2019-02-17 05:29:41 +08:00
|
|
|
pub fn new(value: usize) -> Self {
|
2020-10-11 16:57:26 +08:00
|
|
|
Self { value }
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 18:14:16 +08:00
|
|
|
#[cfg(feature = "serde-serialize-no-std")]
|
2017-05-04 10:02:30 +08:00
|
|
|
impl Serialize for Dynamic {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
2017-05-04 10:02:30 +08:00
|
|
|
self.value.serialize(serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 18:14:16 +08:00
|
|
|
#[cfg(feature = "serde-serialize-no-std")]
|
2017-05-04 10:02:30 +08:00
|
|
|
impl<'de> Deserialize<'de> for Dynamic {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
2017-05-04 10:02:30 +08:00
|
|
|
usize::deserialize(deserializer).map(|x| Dynamic { value: x })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Trait implemented by `Dynamic`.
|
2018-02-02 19:26:35 +08:00
|
|
|
pub trait IsDynamic {}
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Trait implemented by `Dynamic` and type-level integers different from `U1`.
|
2018-02-02 19:26:35 +08:00
|
|
|
pub trait IsNotStaticOne {}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
impl IsDynamic for Dynamic {}
|
|
|
|
impl IsNotStaticOne for Dynamic {}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Trait implemented by any type that can be used as a dimension. This includes type-level
|
|
|
|
/// integers and `Dynamic` (for dimensions not known at compile-time).
|
2018-08-13 12:48:55 +08:00
|
|
|
pub trait Dim: Any + Debug + Copy + PartialEq + Send + Sync {
|
2017-08-03 01:37:44 +08:00
|
|
|
#[inline(always)]
|
|
|
|
fn is<D: Dim>() -> bool {
|
|
|
|
TypeId::of::<Self>() == TypeId::of::<D>()
|
|
|
|
}
|
|
|
|
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Gets the compile-time value of `Self`. Returns `None` if it is not known, i.e., if `Self =
|
|
|
|
/// Dynamic`.
|
2016-12-05 05:44:42 +08:00
|
|
|
fn try_to_usize() -> Option<usize>;
|
2017-02-13 01:17:09 +08:00
|
|
|
|
|
|
|
/// Gets the run-time value of `self`. For type-level integers, this is the same as
|
|
|
|
/// `Self::try_to_usize().unwrap()`.
|
2016-12-05 05:44:42 +08:00
|
|
|
fn value(&self) -> usize;
|
2017-02-13 01:17:09 +08:00
|
|
|
|
|
|
|
/// Builds an instance of `Self` from a run-time value. Panics if `Self` is a type-level
|
|
|
|
/// integer and `dim != Self::try_to_usize().unwrap()`.
|
2016-12-05 05:44:42 +08:00
|
|
|
fn from_usize(dim: usize) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Dim for Dynamic {
|
|
|
|
#[inline]
|
|
|
|
fn try_to_usize() -> Option<usize> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn from_usize(dim: usize) -> Self {
|
2019-02-17 05:29:41 +08:00
|
|
|
Self::new(dim)
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn value(&self) -> usize {
|
|
|
|
self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
impl Add<usize> for Dynamic {
|
|
|
|
type Output = Dynamic;
|
|
|
|
|
|
|
|
#[inline]
|
2019-02-17 05:29:41 +08:00
|
|
|
fn add(self, rhs: usize) -> Self {
|
|
|
|
Self::new(self.value + rhs)
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub<usize> for Dynamic {
|
|
|
|
type Output = Dynamic;
|
|
|
|
|
|
|
|
#[inline]
|
2019-02-17 05:29:41 +08:00
|
|
|
fn sub(self, rhs: usize) -> Self {
|
|
|
|
Self::new(self.value - rhs)
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Operations.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
macro_rules! dim_ops(
|
|
|
|
($($DimOp: ident, $DimNameOp: ident,
|
2017-08-03 01:37:44 +08:00
|
|
|
$Op: ident, $op: ident, $op_path: path,
|
2016-12-05 05:44:42 +08:00
|
|
|
$DimResOp: ident, $DimNameResOp: ident,
|
|
|
|
$ResOp: ident);* $(;)*) => {$(
|
|
|
|
pub type $DimResOp<D1, D2> = <D1 as $DimOp<D2>>::Output;
|
|
|
|
|
|
|
|
pub trait $DimOp<D: Dim>: Dim {
|
|
|
|
type Output: Dim;
|
|
|
|
|
|
|
|
fn $op(self, other: D) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
impl<const A: usize, const B: usize> $DimOp<Const<B>> for Const<A>
|
|
|
|
where
|
|
|
|
Const<A>: ToTypenum,
|
|
|
|
Const<B>: ToTypenum,
|
|
|
|
<Const<A> as ToTypenum>::Typenum: $Op<<Const<B> as ToTypenum>::Typenum>,
|
|
|
|
$ResOp<<Const<A> as ToTypenum>::Typenum, <Const<B> as ToTypenum>::Typenum>: ToConst,
|
|
|
|
{
|
|
|
|
type Output =
|
|
|
|
<$ResOp<<Const<A> as ToTypenum>::Typenum, <Const<B> as ToTypenum>::Typenum> as ToConst>::Const;
|
|
|
|
|
|
|
|
fn $op(self, _: Const<B>) -> Self::Output {
|
2016-12-05 05:44:42 +08:00
|
|
|
Self::Output::name()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<D: Dim> $DimOp<D> for Dynamic {
|
|
|
|
type Output = Dynamic;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn $op(self, other: D) -> Dynamic {
|
2017-08-03 01:37:44 +08:00
|
|
|
Dynamic::new($op_path(self.value, other.value()))
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
// TODO: use Const<T> instead of D: DimName?
|
2016-12-05 05:44:42 +08:00
|
|
|
impl<D: DimName> $DimOp<Dynamic> for D {
|
|
|
|
type Output = Dynamic;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn $op(self, other: Dynamic) -> Dynamic {
|
2017-08-03 01:37:44 +08:00
|
|
|
Dynamic::new($op_path(self.value(), other.value))
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type $DimNameResOp<D1, D2> = <D1 as $DimNameOp<D2>>::Output;
|
|
|
|
|
|
|
|
pub trait $DimNameOp<D: DimName>: DimName {
|
|
|
|
type Output: DimName;
|
|
|
|
|
|
|
|
fn $op(self, other: D) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
impl<const A: usize, const B: usize> $DimNameOp<Const<B>> for Const<A>
|
|
|
|
where
|
|
|
|
Const<A>: ToTypenum,
|
|
|
|
Const<B>: ToTypenum,
|
|
|
|
<Const<A> as ToTypenum>::Typenum: $Op<<Const<B> as ToTypenum>::Typenum>,
|
|
|
|
$ResOp<<Const<A> as ToTypenum>::Typenum, <Const<B> as ToTypenum>::Typenum>: ToConst,
|
|
|
|
{
|
|
|
|
type Output =
|
|
|
|
<$ResOp<<Const<A> as ToTypenum>::Typenum, <Const<B> as ToTypenum>::Typenum> as ToConst>::Const;
|
|
|
|
|
|
|
|
fn $op(self, _: Const<B>) -> Self::Output {
|
2016-12-05 05:44:42 +08:00
|
|
|
Self::Output::name()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*}
|
|
|
|
);
|
|
|
|
|
|
|
|
dim_ops!(
|
2017-08-03 01:37:44 +08:00
|
|
|
DimAdd, DimNameAdd, Add, add, Add::add, DimSum, DimNameSum, Sum;
|
|
|
|
DimMul, DimNameMul, Mul, mul, Mul::mul, DimProd, DimNameProd, Prod;
|
|
|
|
DimSub, DimNameSub, Sub, sub, Sub::sub, DimDiff, DimNameDiff, Diff;
|
|
|
|
DimDiv, DimNameDiv, Div, div, Div::div, DimQuot, DimNameQuot, Quot;
|
2018-12-05 01:40:08 +08:00
|
|
|
DimMin, DimNameMin, Min, min, cmp::min, DimMinimum, DimNameMinimum, Minimum;
|
2017-08-03 01:37:44 +08:00
|
|
|
DimMax, DimNameMax, Max, max, cmp::max, DimMaximum, DimNameMaximum, Maximum;
|
2016-12-05 05:44:42 +08:00
|
|
|
);
|
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct Const<const R: usize>;
|
|
|
|
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Trait implemented exclusively by type-level integers.
|
2016-12-05 05:44:42 +08:00
|
|
|
pub trait DimName: Dim {
|
2021-04-07 20:29:20 +08:00
|
|
|
const USIZE: usize;
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
/// The name of this dimension, i.e., the singleton `Self`.
|
|
|
|
fn name() -> Self;
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: this is not a very idiomatic name.
|
2016-12-05 05:44:42 +08:00
|
|
|
/// The value of this dimension.
|
2021-01-03 22:20:34 +08:00
|
|
|
fn dim() -> usize;
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-04-12 23:43:04 +08:00
|
|
|
#[cfg(feature = "serde-serialize-no-std")]
|
|
|
|
impl<const D: usize> Serialize for Const<D> {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
().serialize(serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde-serialize-no-std")]
|
|
|
|
impl<'de, const D: usize> Deserialize<'de> for Const<D> {
|
|
|
|
fn deserialize<Des>(deserializer: Des) -> Result<Self, Des::Error>
|
|
|
|
where
|
|
|
|
Des: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
<()>::deserialize(deserializer).map(|_| Const::<D>)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
pub trait ToConst {
|
|
|
|
type Const: DimName;
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
pub trait ToTypenum {
|
|
|
|
type Typenum: Unsigned;
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
impl<const T: usize> Dim for Const<T> {
|
2016-12-05 05:44:42 +08:00
|
|
|
fn try_to_usize() -> Option<usize> {
|
2021-01-03 22:20:34 +08:00
|
|
|
Some(T)
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
fn value(&self) -> usize {
|
|
|
|
T
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
fn from_usize(dim: usize) -> Self {
|
|
|
|
assert_eq!(dim, T);
|
|
|
|
Self
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
impl<const T: usize> DimName for Const<T> {
|
2021-04-07 20:29:20 +08:00
|
|
|
const USIZE: usize = T;
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn name() -> Self {
|
2021-01-03 22:20:34 +08:00
|
|
|
Self
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
#[inline]
|
|
|
|
fn dim() -> usize {
|
|
|
|
T
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
pub type U1 = Const<1>;
|
2020-01-26 03:17:49 +08:00
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
impl ToTypenum for Const<{ typenum::U1::USIZE }> {
|
|
|
|
type Typenum = typenum::U1;
|
|
|
|
}
|
2020-01-26 03:17:49 +08:00
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
impl ToConst for typenum::U1 {
|
|
|
|
type Const = Const<{ typenum::U1::USIZE }>;
|
|
|
|
}
|
2020-01-26 03:17:49 +08:00
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
macro_rules! from_to_typenum (
|
|
|
|
($($D: ident),* $(,)*) => {$(
|
|
|
|
pub type $D = Const<{ typenum::$D::USIZE }>;
|
2020-01-26 03:17:49 +08:00
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
impl ToTypenum for Const<{ typenum::$D::USIZE }> {
|
|
|
|
type Typenum = typenum::$D;
|
2020-01-26 03:17:49 +08:00
|
|
|
}
|
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
impl ToConst for typenum::$D {
|
|
|
|
type Const = Const<{ typenum::$D::USIZE }>;
|
2020-01-26 03:17:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IsNotStaticOne for $D { }
|
|
|
|
)*}
|
|
|
|
);
|
|
|
|
|
2021-01-03 22:20:34 +08:00
|
|
|
from_to_typenum!(
|
2020-01-26 03:17:49 +08:00
|
|
|
U0, /*U1,*/ U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18,
|
|
|
|
U19, U20, U21, U22, U23, U24, U25, U26, U27, U28, U29, U30, U31, U32, U33, U34, U35, U36, U37,
|
|
|
|
U38, U39, U40, U41, U42, U43, U44, U45, U46, U47, U48, U49, U50, U51, U52, U53, U54, U55, U56,
|
|
|
|
U57, U58, U59, U60, U61, U62, U63, U64, U65, U66, U67, U68, U69, U70, U71, U72, U73, U74, U75,
|
|
|
|
U76, U77, U78, U79, U80, U81, U82, U83, U84, U85, U86, U87, U88, U89, U90, U91, U92, U93, U94,
|
|
|
|
U95, U96, U97, U98, U99, U100, U101, U102, U103, U104, U105, U106, U107, U108, U109, U110,
|
|
|
|
U111, U112, U113, U114, U115, U116, U117, U118, U119, U120, U121, U122, U123, U124, U125, U126,
|
|
|
|
U127
|
|
|
|
);
|