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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//! Contains items for parameterizing types by whether they need dropping.
//!
//! Refer to [`DropFlavor`] for more details
use ManuallyDrop;
/// For parameterizing types by whether they need dropping.
///
/// This trait is sealed, it's implemented by [`NonDrop`] and [`MayDrop`],
/// it cannot be implemented by any other type.
///
/// # Motivation
///
/// The reason this whole module exists is to allow containers to
/// need dropping only when the type they wrap doesn't need dropping.
///
/// # Example
///
/// As you can see here, to make the container drop conditionally, you need to have
/// a `Foo` that wraps a `FooInner` in this particular way,
/// and then define constructors for types that need dropping (what `of_drop` does here)
/// and types that don't (what `of_copy` does here).
///
/// ```rust
/// use konst::drop_flavor::{self, DropFlavor, MayDrop, NonDrop};
///
///
/// const fn using_nondrop<T: Copy>(val: T) {
/// let mut container = Container::of_copy(val);
///
/// let by_ref: &T = container.get();
/// let by_mut: &mut T = container.get_mut();
///
/// // some code that does something useful with Container
/// }
///
/// struct Container<T, D: DropFlavor>(D::Wrap<ContainerInner<T>>);
///
/// struct ContainerInner<T>(T);
///
/// impl<T> Container<T, NonDrop> {
/// const fn of_copy(val: T) -> Self
/// where
/// T: Copy
/// {
/// Container(drop_flavor::wrap(ContainerInner(val)))
/// }
/// }
/// impl<T> Container<T, MayDrop> {
/// const fn of_drop(val: T) -> Self {
/// Container(drop_flavor::wrap(ContainerInner(val)))
/// }
/// }
///
/// impl<T, D: DropFlavor> Container<T, D> {
/// const fn get(&self) -> &T {
/// &drop_flavor::as_inner::<D, _>(&self.0).0
/// }
/// const fn get_mut(&mut self) -> &mut T {
/// &mut drop_flavor::as_inner_mut::<D, _>(&mut self.0).0
/// }
/// }
///
/// impl<T> Drop for ContainerInner<T> {
/// // only ran if `Container<T, D>` is `Container<T, MayDrop>`
/// fn drop(&mut self) {
/// println!("dropping {}!", std::any::type_name::<T>());
/// }
/// }
///
/// ```
///
/// Attempting to construct `Container` above with `of_drop` instead of `of_copy`
/// produces this compile-time error:
/// ```text
/// error[E0493]: destructor of `Container<T, MayDrop>` cannot be evaluated at compile-time
/// --> konst/src/drop_flavor.rs:25:13
/// |
/// 10 | let mut container = Container::of_drop(val);
/// | ^^^^^^^^^ the destructor for this type cannot be evaluated in constant functions
/// ...
/// 13 | }
/// | - value is dropped here
/// ```
///
///
/// Type argument for types that may need dropping.
/// Type argument for types that don't need dropping-
/// Trait for the types that [`DropFlavor::Wrap`] can produce.
/// Unwraps [`D::Wrap<T>`](DropFlavor::Wrap) into `T`
///
/// # Example
///
/// ```rust
/// use konst::drop_flavor::{self, DropFlavor, MayDrop, NonDrop};
///
/// use std::mem::ManuallyDrop;
///
///
/// assert_eq!(unwrap_foo::<_, MayDrop>(Foo(10)), 10);
///
/// assert_eq!(unwrap_foo::<_, NonDrop>(Foo(ManuallyDrop::new(10))), 10);
///
///
/// fn unwrap_foo<T, D: DropFlavor>(foo: Foo<T, D>) -> T {
/// drop_flavor::unwrap::<D, _>(foo.0)
/// }
///
/// struct Foo<T, D: DropFlavor>(D::Wrap<T>);
/// ```
///
pub const
/// Coerces [`&D::Wrap<T>`](DropFlavor::Wrap) into its contained `&T`
///
/// # Example
///
/// ```rust
/// use konst::drop_flavor::{self, DropFlavor, MayDrop, NonDrop};
///
/// use std::mem::ManuallyDrop;
///
///
/// assert_eq!(foo_as_inner::<_, MayDrop>(&Foo(10)), &10);
///
/// assert_eq!(foo_as_inner::<_, NonDrop>(&Foo(ManuallyDrop::new(10))), &10);
///
///
/// fn foo_as_inner<T, D: DropFlavor>(foo: &Foo<T, D>) -> &T {
/// drop_flavor::as_inner::<D, _>(&foo.0)
/// }
///
/// struct Foo<T, D: DropFlavor>(D::Wrap<T>);
/// ```
///
pub const
/// Coerces [`&mut D::Wrap<T>`](DropFlavor::Wrap) into its contained `&mut T`
///
/// # Example
///
/// ```rust
/// use konst::drop_flavor::{self, DropFlavor, MayDrop, NonDrop};
///
/// use std::mem::ManuallyDrop;
///
///
/// assert_eq!(foo_as_inner_mut::<_, MayDrop>(&mut Foo(10)), &mut 10);
///
/// assert_eq!(foo_as_inner_mut::<_, NonDrop>(&mut Foo(ManuallyDrop::new(10))), &mut 10);
///
///
/// fn foo_as_inner_mut<T, D: DropFlavor>(foo: &mut Foo<T, D>) -> &mut T {
/// drop_flavor::as_inner_mut::<D, _>(&mut foo.0)
/// }
///
/// struct Foo<T, D: DropFlavor>(D::Wrap<T>);
/// ```
///
pub const
/// Converts `T` into either `T` or `ManuallyDrop<T>` as determined by the return type.
///
/// # Example
///
/// ```rust
/// use konst::drop_flavor::{self, DropFlavor, MayDrop, NonDrop};
///
/// use std::mem::ManuallyDrop;
///
///
/// assert_eq!(make_foo::<MayDrop, _>(3), Foo(3));
///
/// assert_eq!(make_foo::<NonDrop, _>(5), Foo(ManuallyDrop::new(5)));
///
///
/// const fn make_foo<D: DropFlavor, T>(val: T) -> Foo<D, T> {
/// Foo(drop_flavor::wrap(val))
/// }
///
/// #[derive(Debug, PartialEq, Eq)]
/// struct Foo<D: DropFlavor, T>(D::Wrap<T>);
/// ```
///
pub const