1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright © 2023 Sven Moog
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
//! This module implements macros which are used to implement the [`From`] trait for data types.
/// Implements the [`From`] trait for a given type. It requires an already written
/// conversion function (e.g. [`Q::from_f64()`](crate::rational::Q::from_f64)).
///
/// Input parameters:
/// - `source_type`: the source identifier (e.g. [`f64`],[`u32`], ...).
/// - `destination_type`: the destination identifier
/// (e.g. [`Q`](crate::rational::Q), [`MatZ`](crate::integer::MatZ)).
/// - `function`: the function that needs to be called for the conversion
/// (e.g. [`Q::from_f64()`](crate::rational::Q::from_f64))
///
/// Returns the Implementation code for the [`From`] Trait with the signature:
/// ```impl From<*source_type*> for *destination_type*```
pub use from_trait;
/// Create a `from_<source_type>` function for `<destination_type>`.
///
/// The `from_<source_type>` function is just a wrapper for
/// `<function>(value as <bridge_type>)`.
///
/// This macro is intended to be used to quickly create implementations for
/// similar types that can be casted into each other.
/// For example, for [`i8`],[`i16`], and [`i32`] given a working conversion for [`i64`].
///
/// A short documentation is automatically included with the pattern:
/// > "Convert <source_type> to <destination_type> using < function>."
///
/// The macro is supposed to be used inside of an `impl` block for the destination type.
///
/// Input parameters:
/// - `source_type`: the source identifier (e.g. [`f64`],[`u32`], ...).
/// - `bridge_type`: type used for casting before calling the function.
/// - `destination_type`: return type of the generated function
/// (e.g. [`Q`](crate::rational::Q), [`MatZ`](crate::integer::MatZ)).
/// - `function`: the function that needs to be called for the conversion
/// (e.g. [`Q::from_f64()`](crate::rational::Q::from_f64)).
///
/// Returns the Implementation code for the function `from_<source_type>`.
///
/// # Examples
/// ```compile_fail
/// use qfall_math::macros;
/// use qfall_math::integer::Z;
///
/// impl Z {
/// pub fn from_i64(value: i64) -> Self { ... }
///
/// macros::from_type!(i32, i64, Z, Z::from_i64);
/// }
/// ```
/// check out the source code of [`crate::integer::Z::from`] for the full example.
pub use from_type;