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
/// 为构建器模式提供辅助方法的宏。
///
/// 此宏为指定的构建器类型添加一个 `when_some` 方法,允许条件性地设置可选值。
/// 当 `Option<T>` 为 `Some` 时,会调用提供的函数来设置值;当为 `None` 时,直接返回构建器本身。
///
/// # 用法
///
/// 有两种调用方式:
/// 1. 为 `Self` 类型实现:
/// ```rust
/// use rust_patterns::builder_helper;
///
/// struct MyBuilder1;
/// struct MyBuilder2;
///
/// builder_helper!(Self, MyBuilder1, MyBuilder2);
///
/// // 现在 MyBuilder1 和 MyBuilder2 都有 when_some 方法
/// ```
/// 2. 为 `&mut Self` 类型实现:
/// ```rust
/// use rust_patterns::builder_helper;
///
/// struct MyBuilder1;
/// struct MyBuilder2;
///
/// builder_helper!(&mut Self, MyBuilder1, MyBuilder2);
///
/// // 现在 MyBuilder1 和 MyBuilder2 都有 when_some 方法
/// ```
///
/// # 生成的方法
///
/// 宏会为每个指定的构建器类型生成一个 `when_some` 方法:
/// ```rust
/// # use rust_patterns::builder_helper;
/// # struct MyBuilder;
/// # builder_helper!(Self, MyBuilder);
/// # impl MyBuilder {
/// fn when_some<T>(self, value: Option<T>, func: impl FnOnce(Self, T) -> Self) -> Self
/// where
/// Self: Sized;
/// # }
/// ```
///
/// 或者对于 `&mut Self` 版本:
/// ```rust
/// # use rust_patterns::builder_helper;
/// # struct MyBuilder;
/// # builder_helper!(&mut Self, MyBuilder);
/// # impl MyBuilder {
/// fn when_some<T>(&mut self, value: Option<T>, func: impl FnOnce(&mut Self, T) -> &mut Self) -> &mut Self
/// where
/// Self: Sized;
/// # }
/// ```