src/metaprogramming.rs

Fri, 13 Dec 2024 22:37:12 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 13 Dec 2024 22:37:12 -0500
branch
dev
changeset 57
1b3b1687b9ed
child 59
9226980e45a7
permissions
-rw-r--r--

Add direct products (Pair, RowOp, ColOp, DiagOp)

57
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 /*!
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 Metaprogramming tools
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 */
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 /// Reference `x` if so indicated by the first parameter.
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 /// Typically to be used from another macro. See the implementation of
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 /// [power][crate::vectorspace::powerspace] and [product spaces][crate::vectorspace::productspace].
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 ///
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 /// ```ignore
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 /// maybe_ref!(ref, V) // ➡ &V
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 /// maybe_ref!(noref, V) // ➡ V
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 /// ```
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 #[macro_export]
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 macro_rules! maybe_ref {
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 (ref, $x:expr) => { &$x };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 (noref, $x:expr) => { $x };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 (ref, $x:ty) => { &$x };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 (noref, $x:ty) => { $x };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 }
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 /// Choose `a` if first argument is the literal `ref`, otherwise `b`.
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 #[macro_export]
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 macro_rules! ifref {
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 (noref, $a:expr, $b:expr) => { $b };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 (ref, $a:expr, $b:expr) => { $a };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 }
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 /// Annotate `x` with a lifetime if the first parameter
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 /// Typically to be used from another macro. See the implementation of
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 /// [power][crate::vectorspace::powerspace] and [product spaces][crate::vectorspace::productspace].
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 ///
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 /// ```ignore
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 /// maybe_ref!(ref, &'a V) // ➡ &'a V
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 /// maybe_ref!(noref, &'a V) // ➡ V
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 /// ```
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 #[macro_export]
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 macro_rules! maybe_lifetime {
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 (ref, $x:ty) => { $x };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 (noref, &$lt:lifetime $x:ty) => { $x };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 (noref, &$x:ty) => { $x };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 }
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 /// Use as
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 /// ```ignore
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 /// impl_vectorspace_ops!(impl_binop, impl_assignop, impl_scalarop, impl_scalar_assignop, impl_unaryop);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 /// ```
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 /// with `impl_binop`, `impl_assignop`, `impl_scalarop`, and `impl_unaryop` macros provided.
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 /// For example usage see the [power][crate::vectorspace::powerspace] and
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 /// [product spaces][crate::vectorspace::productspace] implementations.
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 #[macro_export]
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 macro_rules! impl_vectorspace_ops {
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 ($impl_binop:ident, $impl_assignop:ident, $impl_scalarop:ident, $impl_scalarlhs_op:ident,
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 $impl_scalar_assignop:ident, $impl_unaryop:ident) => {
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 impl_vectorspace_ops!($impl_binop, $impl_assignop, $impl_scalarop, $impl_scalarlhs_op,
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 $impl_scalar_assignop, $impl_unaryop,
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 (f32, f64,
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 num::complex::Complex<f32>,
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 num::complex::Complex<f64>));
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 ($impl_binop:ident, $impl_assignop:ident, $impl_scalarop:ident, $impl_scalarlhs_op:ident,
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 $impl_scalar_assignop:ident, $impl_unaryop:ident, ($($field:ty),+)) => {
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63 impl_vectorspace_ops!(@binary, $impl_binop, Add, add);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 impl_vectorspace_ops!(@binary, $impl_binop, Sub, sub);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65 impl_vectorspace_ops!(@assign, $impl_assignop, AddAssign, add_assign);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
66 impl_vectorspace_ops!(@assign, $impl_assignop, SubAssign, sub_assign);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 impl_vectorspace_ops!(@scalar, $impl_scalarop, Mul, mul);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68 impl_vectorspace_ops!(@scalar, $impl_scalarop, Div, div);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 // Compiler overflow
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
70 // $(
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
71 // impl_vectorspace_ops!(@scalar_lhs, $impl_scalarlhs_op, Mul, mul, $field);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
72 // )*
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
73 impl_vectorspace_ops!(@scalar_assign, $impl_scalar_assignop, MulAssign, mul_assign);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
74 impl_vectorspace_ops!(@scalar_assign, $impl_scalar_assignop, DivAssign, div_assign);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
75 impl_vectorspace_ops!(@unary, $impl_unaryop, Neg, neg);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76 };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
77 (@binary, $impl:ident, $trait : ident, $fn : ident) => {
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
78 $impl!($trait, $fn, ref, ref);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
79 $impl!($trait, $fn, ref, noref);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
80 $impl!($trait, $fn, noref, ref);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
81 $impl!($trait, $fn, noref, noref);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
82 };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83 (@assign, $impl:ident, $trait : ident, $fn :ident) => {
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84 $impl!($trait, $fn, ref);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
85 $impl!($trait, $fn, noref);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
86 };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
87 (@scalar, $impl:ident, $trait : ident, $fn :ident) => {
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
88 $impl!($trait, $fn, ref);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
89 $impl!($trait, $fn, noref);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
90 };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
91 (@scalar_lhs, $impl:ident, $trait : ident, $fn : ident, $field: ty) => {
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
92 // These operators need workarounds
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
93 $impl!($trait, $fn, ref, $field);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
94 $impl!($trait, $fn, noref, $field);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
95 };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
96 (@scalar_assign, $impl:ident, $trait : ident, $fn :ident) => {
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
97 $impl!($trait, $fn);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
98 };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
99 (@unary, $impl:ident, $trait : ident, $fn :ident) => {
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
100 $impl!($trait, $fn, ref);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
101 $impl!($trait, $fn, noref);
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
102 };
1b3b1687b9ed Add direct products (Pair, RowOp, ColOp, DiagOp)
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
103 }

mercurial