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
128
129
130
//! Bridge between [`typenum`](https://docs.rs/typenum) type-level naturals
//! and [`reify_reflect_core::RuntimeValue`].
//!
//! Available when the `typenum` feature is enabled. The bridge is provided
//! as helper functions and an extension trait (rather than `From` impls)
//! because both [`reify_reflect_core::RuntimeValue`] and the `typenum::Uxxx`
//! types are foreign to this crate, which would make a direct `From`
//! impl violate the orphan rule.
//!
//! # Examples
//!
//! ```
//! use reflect_nat::typenum_bridge::{reflect_unsigned, reflect_bit};
//! use reify_reflect_core::RuntimeValue;
//! use typenum::{U0, U7, B0, B1};
//!
//! assert_eq!(reflect_unsigned::<U0>(), RuntimeValue::Nat(0));
//! assert_eq!(reflect_unsigned::<U7>(), RuntimeValue::Nat(7));
//! assert_eq!(reflect_bit::<B0>(), false);
//! assert_eq!(reflect_bit::<B1>(), true);
//! ```
use RuntimeValue;
use ;
/// Reflect a [`typenum::Unsigned`] type-level natural to a
/// [`RuntimeValue::Nat`].
///
/// # Examples
///
/// ```
/// use reflect_nat::typenum_bridge::reflect_unsigned;
/// use reify_reflect_core::RuntimeValue;
/// use typenum::U42;
///
/// assert_eq!(reflect_unsigned::<U42>(), RuntimeValue::Nat(42));
/// ```
/// Reflect a [`typenum::Bit`] (`B0` or `B1`) to a runtime `bool`.
///
/// # Examples
///
/// ```
/// use reflect_nat::typenum_bridge::reflect_bit;
/// use typenum::{B0, B1};
///
/// assert!(!reflect_bit::<B0>());
/// assert!( reflect_bit::<B1>());
/// ```
/// Extension trait giving every [`typenum::Unsigned`] a `.reflect()`
/// method that returns a [`RuntimeValue::Nat`]. Implemented blanket-style
/// over all `T: Unsigned`.
///
/// # Examples
///
/// ```
/// use reflect_nat::typenum_bridge::TypenumReflect;
/// use reify_reflect_core::RuntimeValue;
/// use typenum::U5;
///
/// assert_eq!(U5::reflect_runtime(), RuntimeValue::Nat(5));
/// ```
/// Convert one of our [`crate::Nat`] type-level naturals to its runtime
/// `u64` value (the input shape that `typenum`'s `U*` types accept via
/// constants like `T::U64`).
///
/// This is the dual of [`reflect_unsigned`]: it goes our-encoding → runtime;
/// from there callers can pick a `typenum::U*` based on the value if needed.
///
/// # Examples
///
/// ```
/// use reflect_nat::{N3, typenum_bridge::nat_to_u64};
///
/// assert_eq!(nat_to_u64::<N3>(), 3);
/// ```