curve25519_dalek/
macros.rs

1// -*- mode: rust; -*-
2//
3// This file is part of curve25519-dalek.
4// Copyright (c) 2016-2021 isis agora lovecruft
5// Copyright (c) 2016-2019 Henry de Valence
6// See LICENSE for licensing information.
7//
8// Authors:
9// - isis agora lovecruft <isis@patternsinthevoid.net>
10// - Henry de Valence <hdevalence@hdevalence.ca>
11
12//! Internal macros.
13
14/// Define borrow and non-borrow variants of `Add`.
15macro_rules! define_add_variants {
16    (LHS = $lhs:ty, RHS = $rhs:ty, Output = $out:ty) => {
17        impl<'b> Add<&'b $rhs> for $lhs {
18            type Output = $out;
19            fn add(self, rhs: &'b $rhs) -> $out {
20                &self + rhs
21            }
22        }
23
24        impl<'a> Add<$rhs> for &'a $lhs {
25            type Output = $out;
26            fn add(self, rhs: $rhs) -> $out {
27                self + &rhs
28            }
29        }
30
31        impl Add<$rhs> for $lhs {
32            type Output = $out;
33            fn add(self, rhs: $rhs) -> $out {
34                &self + &rhs
35            }
36        }
37    };
38}
39
40/// Define non-borrow variants of `AddAssign`.
41macro_rules! define_add_assign_variants {
42    (LHS = $lhs:ty, RHS = $rhs:ty) => {
43        impl AddAssign<$rhs> for $lhs {
44            fn add_assign(&mut self, rhs: $rhs) {
45                *self += &rhs;
46            }
47        }
48    };
49}
50
51/// Define borrow and non-borrow variants of `Sub`.
52macro_rules! define_sub_variants {
53    (LHS = $lhs:ty, RHS = $rhs:ty, Output = $out:ty) => {
54        impl<'b> Sub<&'b $rhs> for $lhs {
55            type Output = $out;
56            fn sub(self, rhs: &'b $rhs) -> $out {
57                &self - rhs
58            }
59        }
60
61        impl<'a> Sub<$rhs> for &'a $lhs {
62            type Output = $out;
63            fn sub(self, rhs: $rhs) -> $out {
64                self - &rhs
65            }
66        }
67
68        impl Sub<$rhs> for $lhs {
69            type Output = $out;
70            fn sub(self, rhs: $rhs) -> $out {
71                &self - &rhs
72            }
73        }
74    };
75}
76
77/// Define non-borrow variants of `SubAssign`.
78macro_rules! define_sub_assign_variants {
79    (LHS = $lhs:ty, RHS = $rhs:ty) => {
80        impl SubAssign<$rhs> for $lhs {
81            fn sub_assign(&mut self, rhs: $rhs) {
82                *self -= &rhs;
83            }
84        }
85    };
86}
87
88/// Define borrow and non-borrow variants of `Mul`.
89macro_rules! define_mul_variants {
90    (LHS = $lhs:ty, RHS = $rhs:ty, Output = $out:ty) => {
91        impl<'b> Mul<&'b $rhs> for $lhs {
92            type Output = $out;
93            fn mul(self, rhs: &'b $rhs) -> $out {
94                &self * rhs
95            }
96        }
97
98        impl<'a> Mul<$rhs> for &'a $lhs {
99            type Output = $out;
100            fn mul(self, rhs: $rhs) -> $out {
101                self * &rhs
102            }
103        }
104
105        impl Mul<$rhs> for $lhs {
106            type Output = $out;
107            fn mul(self, rhs: $rhs) -> $out {
108                &self * &rhs
109            }
110        }
111    };
112}
113
114/// Define non-borrow variants of `MulAssign`.
115macro_rules! define_mul_assign_variants {
116    (LHS = $lhs:ty, RHS = $rhs:ty) => {
117        impl MulAssign<$rhs> for $lhs {
118            fn mul_assign(&mut self, rhs: $rhs) {
119                *self *= &rhs;
120            }
121        }
122    };
123}