fp_library/classes/
ring.rs1#[fp_macros::document_module]
12mod inner {
13 use {
14 crate::classes::*,
15 fp_macros::*,
16 };
17
18 #[document_examples]
25 pub trait Ring: Semiring {
36 #[document_signature]
38 #[document_parameters("The value to subtract from.", "The value to subtract.")]
40 #[document_returns("The difference.")]
42 #[document_examples]
43 fn subtract(
50 a: Self,
51 b: Self,
52 ) -> Self;
53 }
54
55 #[document_signature]
59 #[document_type_parameters("The ring type.")]
61 #[document_parameters("The value to subtract from.", "The value to subtract.")]
63 #[document_returns("The difference.")]
65 #[document_examples]
66 pub fn subtract<R: Ring>(
73 a: R,
74 b: R,
75 ) -> R {
76 R::subtract(a, b)
77 }
78
79 #[document_signature]
83 #[document_type_parameters("The ring type.")]
85 #[document_parameters("The value to negate.")]
87 #[document_returns("The negated value.")]
89 #[document_examples]
90 pub fn negate<R: Ring>(a: R) -> R {
97 R::subtract(R::zero(), a)
98 }
99
100 macro_rules! impl_ring_int {
101 ($($t:ty),+) => {
102 $(
103 impl Ring for $t {
104 #[document_signature]
106 #[document_parameters("The value to subtract from.", "The value to subtract.")]
108 #[document_returns("The difference (wrapping on overflow).")]
110 #[document_examples]
111 #[doc = concat!("use fp_library::classes::Ring;")]
114 #[doc = concat!("assert_eq!(<", stringify!($t), ">::subtract(5 as ", stringify!($t), ", 3 as ", stringify!($t), "), 2 as ", stringify!($t), ");")]
116 fn subtract(a: Self, b: Self) -> Self { a.wrapping_sub(b) }
118 }
119 )+
120 };
121 }
122
123 impl_ring_int!(i8, i16, i32, i64, i128, isize);
124
125 macro_rules! impl_ring_float {
126 ($($t:ty),+) => {
127 $(
128 impl Ring for $t {
129 #[document_signature]
131 #[document_parameters("The value to subtract from.", "The value to subtract.")]
133 #[document_returns("The difference.")]
135 #[document_examples]
136 #[doc = concat!("use fp_library::classes::Ring;")]
139 #[doc = concat!("assert_eq!(<", stringify!($t), ">::subtract(5.0 as ", stringify!($t), ", 3.0 as ", stringify!($t), "), 2.0 as ", stringify!($t), ");")]
141 fn subtract(a: Self, b: Self) -> Self { a - b }
143 }
144 )+
145 };
146 }
147
148 impl_ring_float!(f32, f64);
149}
150
151pub use inner::*;