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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) 2021 Sebastien Braun
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//! Type-level machinery to allow [`Eso::unify`] to work.
//!
//! The [`Unify`] trait specifies the rules how two [`Maybe`](crate::maybe::Maybe)
//! types can be merged:
//!
//! | This ... | ... unifies with ... | ... containing ... | ... producing ... |
//! |-------------|----------------------|--------------------|-------------------|
//! | [`An<A>`] | [`An<A>`] | same inner type | [`An<A>`] |
//! | [`No<A>`] | [`An<B>`] | any type | [`An<B>`] |
//! | [`An<A>`] | [`No<B>`] | any type | [`An<A>`] |
//! | [`No<A>`] | [`No<B>`] | any type | [`No<A>`] |
//!
//! This module also provides the utility trait [`Unify3`] that
//! contains the ugly type manipulations to apply the [`Unify`] rules
//! between three types.
use crateEso;
use crate;
/// A type `A` that can be unified with another type `B`.
///
/// ## `Maybe`s
///
/// Possible values can be unified with possible values of the
/// same type:
///
/// ```
/// # use eso::maybe::*;
/// # use eso::unify::*;
/// let one: An<i32> = <An<i32> as Unify<An<i32>>>::inject_a(An(1));
/// let two: An<i32> = <An<i32> as Unify<An<i32>>>::inject_b(An(1));
/// ```
///
/// When selecting between a possible and an impossible value,
/// the result must be the possible value:
///
/// ```
/// # use eso::maybe::*;
/// # use eso::unify::*;
/// type Yes = An<i32>;
/// type Nope = No<i32>;
/// type Merged = <Yes as Unify<Nope>>::Out;
/// let yes: Yes = An(1);
/// let merged: An<i32> = <Yes as Unify<Nope>>::inject_a(yes);
///
/// // reflectively:
/// let yes: Yes = An(1);
/// let merged: An<i32> = <Nope as Unify<Yes>>::inject_b(yes);
/// ```
///
/// The inner type of a [`No`] does not matter, it will always
/// unify to the [`An`]:
///
/// ```
/// # use eso::maybe::*;
/// # use eso::unify::*;
/// type Yes = An<i32>;
/// type Nope = No<String>;
/// type Merged = <Yes as Unify<Nope>>::Out;
/// let yes: Yes = An(1);
/// let merged: An<i32> = <Yes as Unify<Nope>>::inject_a(yes);
///
/// // reflectively:
/// let yes: Yes = An(1);
/// let merged: An<i32> = <Nope as Unify<Yes>>::inject_b(yes);
/// ```
///
/// Two [`No`]s will arbitrarily unify to the one on which this
/// trait is invoked. The asymmetry does not matter however,
/// because a [`No`] can always be converted to any other type
/// via [`Impossible::absurd`]:
///
/// ```
/// # use eso::maybe::*;
/// # use eso::unify::*;
/// fn merge_nopes_a(a: No<i32>, b: No<String>) -> No<i32> {
/// <No<i32> as Unify<No<String>>>::inject_a(a)
/// }
///
/// fn merge_nopes_b(a: No<i32>, b: No<String>) -> No<i32> {
/// <No<i32> as Unify<No<String>>>::inject_b(b)
/// }
/// ```
///
/// ## `Eso`s
///
/// Two [`Eso`]s can be unified, if their respective `E`, `S` and `O`
/// type parameters can be unified:
///
/// ```
/// # use eso::{eso::*, maybe::*, unify::*, shorthand::t};
/// type ESO1 = t::S<&'static str, &'static str, String>;
/// type ESO2 = t::O<&'static str, &'static str, String>;
/// type Merged = t::SO<&'static str, &'static str, String>;
/// let eso1: Merged = <ESO1 as Unify<ESO2>>::inject_a(ESO1::from_static("Hello"));
/// let eso2: Merged = <ESO1 as Unify<ESO2>>::inject_b(ESO2::from_owned("Hello".to_string()));
/// ```
///
/// Shorthand for unifying three types by applying [`Unify`]
/// twice.