1#[fp_macros::document_module]
15mod inner {
16 use fp_macros::*;
17
18 #[document_examples]
38 pub trait Semiring {
52 #[document_signature]
54 #[document_parameters("The first value.", "The second value.")]
56 #[document_returns("The sum of the two values.")]
58 #[document_examples]
59 fn add(
66 a: Self,
67 b: Self,
68 ) -> Self;
69
70 #[document_signature]
72 #[document_returns("The additive identity (zero).")]
74 #[document_examples]
75 fn zero() -> Self;
82
83 #[document_signature]
85 #[document_parameters("The first value.", "The second value.")]
87 #[document_returns("The product of the two values.")]
89 #[document_examples]
90 fn multiply(
97 a: Self,
98 b: Self,
99 ) -> Self;
100
101 #[document_signature]
103 #[document_returns("The multiplicative identity (one).")]
105 #[document_examples]
106 fn one() -> Self;
113 }
114
115 #[document_signature]
119 #[document_type_parameters("The semiring type.")]
121 #[document_parameters("The first value.", "The second value.")]
123 #[document_returns("The sum of the two values.")]
125 #[document_examples]
126 pub fn add<S: Semiring>(
133 a: S,
134 b: S,
135 ) -> S {
136 S::add(a, b)
137 }
138
139 #[document_signature]
143 #[document_type_parameters("The semiring type.")]
145 #[document_returns("The additive identity (zero).")]
147 #[document_examples]
148 pub fn zero<S: Semiring>() -> S {
155 S::zero()
156 }
157
158 #[document_signature]
162 #[document_type_parameters("The semiring type.")]
164 #[document_parameters("The first value.", "The second value.")]
166 #[document_returns("The product of the two values.")]
168 #[document_examples]
169 pub fn multiply<S: Semiring>(
176 a: S,
177 b: S,
178 ) -> S {
179 S::multiply(a, b)
180 }
181
182 #[document_signature]
186 #[document_type_parameters("The semiring type.")]
188 #[document_returns("The multiplicative identity (one).")]
190 #[document_examples]
191 pub fn one<S: Semiring>() -> S {
198 S::one()
199 }
200
201 macro_rules! impl_semiring_int {
202 ($($t:ty),+) => {
203 $(
204 impl Semiring for $t {
205 #[document_signature]
207 #[document_parameters("The first value.", "The second value.")]
209 #[document_returns("The sum (wrapping on overflow).")]
211 #[document_examples]
212 #[doc = concat!("use fp_library::classes::Semiring;")]
215 #[doc = concat!("assert_eq!(<", stringify!($t), ">::add(2 as ", stringify!($t), ", 3 as ", stringify!($t), "), 5 as ", stringify!($t), ");")]
217 fn add(
219 a: Self,
220 b: Self,
221 ) -> Self {
222 a.wrapping_add(b)
223 }
224
225 #[document_signature]
227 #[document_returns("Zero.")]
229 #[document_examples]
230 #[doc = concat!("use fp_library::classes::Semiring;")]
233 #[doc = concat!("assert_eq!(<", stringify!($t), ">::zero(), 0 as ", stringify!($t), ");")]
235 fn zero() -> Self {
237 0
238 }
239
240 #[document_signature]
242 #[document_parameters("The first value.", "The second value.")]
244 #[document_returns("The product (wrapping on overflow).")]
246 #[document_examples]
247 #[doc = concat!("use fp_library::classes::Semiring;")]
250 #[doc = concat!("assert_eq!(<", stringify!($t), ">::multiply(2 as ", stringify!($t), ", 3 as ", stringify!($t), "), 6 as ", stringify!($t), ");")]
252 fn multiply(
254 a: Self,
255 b: Self,
256 ) -> Self {
257 a.wrapping_mul(b)
258 }
259
260 #[document_signature]
262 #[document_returns("One.")]
264 #[document_examples]
265 #[doc = concat!("use fp_library::classes::Semiring;")]
268 #[doc = concat!("assert_eq!(<", stringify!($t), ">::one(), 1 as ", stringify!($t), ");")]
270 fn one() -> Self {
272 1
273 }
274 }
275 )+
276 };
277 }
278
279 impl_semiring_int!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);
280
281 macro_rules! impl_semiring_float {
282 ($($t:ty),+) => {
283 $(
284 impl Semiring for $t {
285 #[document_signature]
287 #[document_parameters("The first value.", "The second value.")]
289 #[document_returns("The sum.")]
291 #[document_examples]
292 #[doc = concat!("use fp_library::classes::Semiring;")]
295 #[doc = concat!("assert_eq!(<", stringify!($t), ">::add(2.0 as ", stringify!($t), ", 3.0 as ", stringify!($t), "), 5.0 as ", stringify!($t), ");")]
297 fn add(
299 a: Self,
300 b: Self,
301 ) -> Self {
302 a + b
303 }
304
305 #[document_signature]
307 #[document_returns("Zero.")]
309 #[document_examples]
310 #[doc = concat!("use fp_library::classes::Semiring;")]
313 #[doc = concat!("assert_eq!(<", stringify!($t), ">::zero(), 0.0 as ", stringify!($t), ");")]
315 fn zero() -> Self {
317 0.0
318 }
319
320 #[document_signature]
322 #[document_parameters("The first value.", "The second value.")]
324 #[document_returns("The product.")]
326 #[document_examples]
327 #[doc = concat!("use fp_library::classes::Semiring;")]
330 #[doc = concat!("assert_eq!(<", stringify!($t), ">::multiply(2.0 as ", stringify!($t), ", 3.0 as ", stringify!($t), "), 6.0 as ", stringify!($t), ");")]
332 fn multiply(
334 a: Self,
335 b: Self,
336 ) -> Self {
337 a * b
338 }
339
340 #[document_signature]
342 #[document_returns("One.")]
344 #[document_examples]
345 #[doc = concat!("use fp_library::classes::Semiring;")]
348 #[doc = concat!("assert_eq!(<", stringify!($t), ">::one(), 1.0 as ", stringify!($t), ");")]
350 fn one() -> Self {
352 1.0
353 }
354 }
355 )+
356 };
357 }
358
359 impl_semiring_float!(f32, f64);
360}
361
362pub use inner::*;