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
use subenum::subenum;
#[subenum(JustRef, JustConst, JustType)]
#[derive(Debug, PartialEq, Clone)]
enum KitchenSink<'a, T, const N: usize>
where
T: Copy + std::ops::Add<Output = T>,
{
// Uses only lifetime
#[subenum(JustRef)]
RefHolder(&'a str),
// Uses only const
#[subenum(JustConst)]
ArrayHolder([u8; N]),
// Uses only Type
#[subenum(JustType)]
ValueHolder(T),
}
#[test]
fn test_mixed_generics_isolation() {
const SIZE: usize = 16;
let val: i32 = 100;
// 1. JustRef: Should be JustRef<'a>
// It should NOT require T or N.
// If your macro incorrectly requires T, this might fail to compile
// if T doesn't satisfy bounds, or simply via signature mismatch.
let r: JustRef<'_> = JustRef::RefHolder("hello");
match r {
JustRef::RefHolder(s) => assert_eq!(s, "hello"),
}
// 2. JustConst: Should be JustConst<const N: usize>
// Should NOT require 'a or T.
let c: JustConst<SIZE> = JustConst::ArrayHolder([0; SIZE]);
match c {
JustConst::ArrayHolder(arr) => assert_eq!(arr.len(), 16),
}
// 3. JustType: Should be JustType<T>
// Crucial: It MUST preserve the bounds `where T: Copy + Add...`
// but MUST Drop the generic parameters 'a and N.
let v: JustType<i32> = JustType::ValueHolder(val);
match v {
JustType::ValueHolder(x) => assert_eq!(x, 100),
}
}