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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! Contains various commonly-needed supertraits and supporting types.
use crate::*;
/// Implements [`CustomTypeId`] for the specified type and sets its type id to the specified
/// literal u64 value., e.g. `impl_custom_type_id!(bool, 10681234549081409806);`.
///
/// Specifying that two distinct types have the same `TYPE_ID` can lead to UB, so it is up to
/// the implementer to ensure these remain globally unique.
///
/// Used by [`ConstInto`].
/// A trait containing a default assocaited type `TYPE_ID` which is supposed to contain a
/// globally unique `u64` value for all types that implement [`CustomTypeId`].
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
impl_custom_type_id!;
set_supertrait_path!;
/// This supertrait can be used as a somewhat less useful substitute for being able to make use
/// of `From<T>` / `Into<T>` in const scenarios.
///
/// Because supertrait uses inherent impls as the backend for const fn trait items that are
/// impled, you cannot implement `ConstInto` more than once for the same type.
///
/// Thus the best way to support multiple types is to write a match statement based on
/// `TYPE_ID` and use unsafe pointer dereferencing to handle each of the cases, such as the
/// following:
///
/// ```
/// use supertrait::*;
/// use supertrait::traits::*;
///
/// pub struct MyStruct {
/// bool: bool,
/// i32: i32,
/// char: char,
/// }
///
/// #[impl_supertrait]
/// impl ConstInto for MyStruct {
/// const fn const_into<T: CustomTypeId>(&self) -> &T {
/// match T::TYPE_ID {
/// bool::TYPE_ID => unsafe { &*((&self.bool as *const bool) as *const T) },
/// i32::TYPE_ID => unsafe { &*((&self.i32 as *const i32) as *const T) },
/// char::TYPE_ID => unsafe { &*((&self.char as *const char) as *const T) },
/// _ => panic!("unsupported type"),
/// }
/// }
/// }
/// ```
/// This supertrait allows you to write const-compatible implementations of [`Clone`].
///
/// A bound is included ensuring that you cannot implement this for types that don't already
/// implement [`Clone`].
///
/// Types that implement [`Copy`] are trivial to support here, since they can be dereferenced
/// to create a copy that can be used as the return result for [`ConstClone`].