Debian Codegraph @

debian/rust-vendor/yoke-derive/examples/yoke_derive.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 #![allow(unused)]
6
7 use std::{borrow::Cow, marker::PhantomData};
8 use yoke::{Yoke, Yokeable};
9 use zerovec::{maps::ZeroMapKV, ule::AsULE, VarZeroVec, ZeroMap, ZeroVec};
10
11 #[derive(Yokeable)]
12 struct StringExample {
13 x: String,
14 }
15
16 #[derive(Yokeable)]
17 #[yoke(prove_covariance_manually)]
18 struct ManualStringExample {
19 x: String,
20 }
21
22 #[derive(Yokeable, Copy, Clone)]
23 struct IntExample {
24 x: u32,
25 }
26
27 #[derive(Yokeable, Copy, Clone)]
28 #[yoke(prove_covariance_manually)]
29 struct ManualIntExample {
30 x: u32,
31 }
32
33 #[derive(Yokeable, Copy, Clone)]
34 struct GenericsExample<T> {
35 x: u32,
36 y: T,
37 }
38
39 #[derive(Yokeable, Copy, Clone)]
40 #[yoke(prove_covariance_manually)]
41 struct ManualGenericsExample<T> {
42 x: u32,
43 y: T,
44 }
45
46 #[derive(Yokeable, Copy, Clone)]
47 struct GenericsExampleWithDefault<T, U = usize> {
48 x: T,
49 y: U,
50 }
51
52 #[derive(Yokeable, Copy, Clone)]
53 #[yoke(prove_covariance_manually)]
54 struct ManualGenericsExampleWithDefault<T, U = usize> {
55 x: T,
56 y: U,
57 }
58
59 #[derive(Yokeable)]
60 struct CowExample<'a> {
61 x: u8,
62 y: &'a str,
63 z: Cow<'a, str>,
64 w: Cow<'a, [u8]>,
65 }
66
67 #[derive(Yokeable)]
68 #[yoke(prove_covariance_manually)]
69 struct ManualCowExample<'a> {
70 x: u8,
71 y: &'a str,
72 z: Cow<'a, str>,
73 w: Cow<'a, [u8]>,
74 }
75
76 #[derive(Yokeable)]
77 struct ZeroVecExample<'not_a> {
78 var: VarZeroVec<'not_a, str>,
79 vec: ZeroVec<'not_a, u16>,
80 }
81
82 #[derive(Yokeable)]
83 #[yoke(prove_covariance_manually)]
84 struct ManualZeroVecExample<'not_a> {
85 var: VarZeroVec<'not_a, str>,
86 vec: ZeroVec<'not_a, u16>,
87 }
88
89 #[derive(Yokeable)]
90 struct ZeroVecExampleWithGenerics<'a, T: AsULE> {
91 gen: ZeroVec<'a, T>,
92 vec: ZeroVec<'a, u16>,
93 bare: T,
94 }
95
96 #[derive(Yokeable)]
97 #[yoke(prove_covariance_manually)]
98 struct ManualZeroVecExampleWithGenerics<'a, T: AsULE> {
99 gen: ZeroVec<'a, T>,
100 vec: ZeroVec<'a, u16>,
101 bare: T,
102 }
103
104 // Since ZeroMap has generic parameters, the Rust compiler cannot
105 // prove the covariance of the lifetimes. To use derive(Yokeable)
106 // with a type such as ZeroMap, you just add the attribute
107 // yoke(prove_covariance_manually)
108 #[derive(Yokeable)]
109 #[yoke(prove_covariance_manually)]
110 struct ManualZeroMapExample<'a> {
111 map: ZeroMap<'a, str, u16>,
112 }
113
114 #[derive(Yokeable)]
115 #[yoke(prove_covariance_manually)]
116 struct ManualZeroMapGenericExample<'a, T: for<'b> ZeroMapKV<'b> + ?Sized> {
117 map: ZeroMap<'a, str, T>,
118 }
119
120 #[derive(Yokeable)]
121 struct MaybeSizedWrap<T, Q: ?Sized, U: ?Sized> {
122 x: T,
123 y: Option<T>,
124 ignored: PhantomData<U>,
125 q: Q,
126 }
127
128 #[derive(Yokeable)]
129 #[yoke(prove_covariance_manually)]
130 struct ManualMaybeSizedWrap<T, Q: ?Sized, U: ?Sized> {
131 x: T,
132 y: Option<T>,
133 ignored: PhantomData<U>,
134 q: Q,
135 }
136
137 // TODO(#4119): Make this example compile
138 /*
139 #[derive(Yokeable)]
140 struct MaybeSizedWrapWithLifetime<'a, T, Q: ?Sized, U: ?Sized> {
141 x: T,
142 y: Option<T>,
143 ignored: &'a U,
144 q: Q,
145 }
146 */
147
148 trait Trait {}
149 impl Trait for u32 {}
150
151 #[derive(Yokeable)]
152 struct WithTraitBounds<T: Trait> {
153 x: T,
154 }
155
156 #[derive(Yokeable)]
157 #[yoke(prove_covariance_manually)]
158 struct ManualWithTraitBounds<T: Trait> {
159 x: T,
160 }
161
162 #[derive(Yokeable)]
163 struct WithTraitBoundsInWhere<T>
164 where
165 T: Trait,
166 {
167 x: T,
168 }
169
170 #[derive(Yokeable)]
171 #[yoke(prove_covariance_manually)]
172 struct ManualWithTraitBoundsInWhere<T>
173 where
174 T: Trait,
175 {
176 x: T,
177 }
178
179 #[derive(Yokeable)]
180 struct BoundFn<'a>(&'a (), for<'b> fn(&'b ()));
181
182 #[derive(Yokeable)]
183 #[yoke(prove_covariance_manually)]
184 struct ManualBoundFn<'a>(&'a (), for<'b> fn(&'b ()));
185
186 #[derive(Yokeable)]
187 struct Invariant<'a, T>(&'a mut T);
188
189 #[derive(Yokeable)]
190 struct InvariantStatic<'a> {
191 field: Invariant<'a, &'static str>,
192 }
193
194 #[derive(Yokeable)]
195 #[yoke(prove_covariance_manually)]
196 struct ManualInvariantStatic<'a> {
197 field: Invariant<'a, &'static str>,
198 }
199
200 #[derive(Yokeable)]
201 struct RawLifetime<'r#mod> {
202 field: &'r#mod str,
203 other: for<'r#yoke> fn(&'r#yoke ()),
204 }
205
206 #[derive(Yokeable)]
207 #[yoke(prove_covariance_manually)]
208 struct ManualRawLifetime<'r#mod> {
209 field: &'r#mod str,
210 other: for<'r#yoke> fn(&'r#yoke ()),
211 }
212
213 #[derive(Yokeable)]
214 struct MixedRawLifetime<'r#a> {
215 field: &'r#a &'a str,
216 }
217
218 #[derive(Yokeable)]
219 #[yoke(prove_covariance_manually)]
220 struct ManualMixedRawLifetime<'r#a> {
221 field: &'r#a &'a str,
222 }
223
224 #[derive(Yokeable)]
225 struct YokeLifetime<'r#_yoke>(&'r#_yoke str, for<'yoke> fn(&'yoke ()));
226
227 #[derive(Yokeable)]
228 #[yoke(prove_covariance_manually)]
229 struct ManualYokeLifetime<'r#_yoke>(&'r#_yoke str, for<'yoke> fn(&'yoke ()));
230
231 // TODO: make this compile
232 // #[derive(Yokeable)]
233 struct ConstGenerics<'a, const N: u32> {
234 field: &'a str,
235 }
236
237 // #[derive(Yokeable)]
238 // #[yoke(prove_covariance_manually)]
239 struct ManualConstGenerics<'a, const N: u32> {
240 field: &'a str,
241 }
242
243 #[derive(Yokeable)]
244 struct Variadic<'a> {
245 field: fn(extern "C" fn(&'a (), u32, u32, ...)),
246 }
247
248 #[derive(Yokeable)]
249 #[yoke(prove_covariance_manually)]
250 struct ManualVariadic<'a> {
251 // `fn(..)` does not implement `Yokeable`, even if the type is in fact covariant.
252 // Thus the extra `&'a`.
253 field: &'a fn(extern "C" fn(&'a (), u32, u32, ...)),
254 }
255
256 #[derive(Yokeable)]
257 enum Enum {
258 A,
259 B,
260 }
261
262 #[derive(Yokeable)]
263 #[yoke(prove_covariance_manually)]
264 enum ManualEnum {
265 A,
266 B,
267 }
268
269 #[derive(Yokeable)]
270 enum Empty {}
271
272 #[derive(Yokeable)]
273 #[yoke(prove_covariance_manually)]
274 enum ManualEmpty {}
275
276 #[derive(Yokeable)]
277 enum Uninhabited<'a> {
278 Variant(Empty, &'a ()),
279 }
280
281 #[derive(Yokeable)]
282 #[yoke(prove_covariance_manually)]
283 enum ManualUninhabited<'a> {
284 Variant(Empty, &'a ()),
285 }
286
287 // The random extra `r#`s are intentional
288 #[derive(Yokeable)]
289 enum AlmostEverything<'a, T, ULE: AsULE, r#W, /*const N: u32,*/ U = usize>
290 where
291 W: Trait,
292 {
293 X(String),
294 X2 {
295 x: u32,
296 },
297 Y(T),
298 Gen {
299 x: T,
300 y: U,
301 },
302 CowExample {
303 x: u8,
304 y: &'a str,
305 z: Cow<'a, str>,
306 w: Cow<'a, [u8]>,
307 },
308 ZeroVec {
309 var: VarZeroVec<'a, str>,
310 vec: ZeroVec<'a, u16>,
311 },
312 ZeroVecGen {
313 gen: r#ZeroVec<'a, ULE>,
314 vec: r#ZeroVec<'a, u16>,
315 bare: T,
316 },
317 Phantom(T, Option<T>, PhantomData<&'a ()>),
318 TraitBounds(W),
319 BoundFn(for<'yoke> fn(&'yoke ())),
320 RawLifetime(for<'r#_yoke> fn(&'r#_yoke ())),
321 MixedRawLifetime(&'r#a &'a str),
322 Variadic(fn(extern "C" fn(&'a (), u32, u32, ...))),
323 }
324
325 #[derive(Yokeable)]
326 #[yoke(prove_covariance_manually)]
327 enum ManualAlmostEverything<
328 'a,
329 T,
330 ULE: AsULE,
331 r#W,
332 Z: for<'b> ZeroMapKV<'b> + ?Sized,
333 // const N: u32,
334 U = usize,
335 > where
336 W: Trait,
337 {
338 X(String),
339 X2 {
340 x: u32,
341 },
342 Y(T),
343 Gen {
344 x: T,
345 y: U,
346 },
347 CowExample {
348 x: u8,
349 y: &'a str,
350 z: Cow<'a, str>,
351 w: Cow<'a, [u8]>,
352 },
353 ZeroVec {
354 var: VarZeroVec<'a, str>,
355 vec: ZeroVec<'a, u16>,
356 },
357 ZeroVecGen {
358 gen: r#ZeroVec<'a, ULE>,
359 vec: r#ZeroVec<'a, u16>,
360 bare: T,
361 },
362 Phantom(T, Option<T>, PhantomData<&'a ()>),
363 TraitBounds(W),
364 BoundFn(for<'yoke> fn(&'yoke ())),
365 RawLifetime(for<'r#_yoke> fn(&'r#_yoke ())),
366 MixedRawLifetime(&'r#a &'a str),
367 Variadic(&'a fn(extern "C" fn(&'a (), u32, u32, ...))),
368 Map(ZeroMap<'a, str, u16>, ZeroMap<'a, str, Z>),
369 }
370
371 struct AssertYokeable {
372 string: Yoke<StringExample, Box<[u8]>>,
373 int: Yoke<IntExample, Box<[u8]>>,
374 gen1: Yoke<GenericsExample<u32>, Box<[u8]>>,
375 gen2: Yoke<GenericsExample<String>, Box<[u8]>>,
376 gen_default1: Yoke<GenericsExampleWithDefault<String>, Box<[u8]>>,
377 gen_default2: Yoke<GenericsExampleWithDefault<String, u8>, Box<[u8]>>,
378 cow: Yoke<CowExample<'static>, Box<[u8]>>,
379 zv: Yoke<ZeroVecExample<'static>, Box<[u8]>>,
380 zv_gen1: Yoke<ZeroVecExampleWithGenerics<'static, u8>, Box<[u8]>>,
381 zv_gen2: Yoke<ZeroVecExampleWithGenerics<'static, char>, Box<[u8]>>,
382 maybe_sized_wrap: Yoke<MaybeSizedWrap<usize, usize, str>, Box<[u8]>>,
383 // TODO(#4119): Make this example compile
384 // maybe_sized_wrap_with_lt: Yoke<MaybeSizedWrapWithLifetime<'static, usize, usize, str>, Box<[u8]>>,
385 trait_bounds: Yoke<WithTraitBounds<u32>, Box<[u8]>>,
386 trait_bounds_where: Yoke<WithTraitBoundsInWhere<u32>, Box<[u8]>>,
387 bound_fn: Yoke<BoundFn<'static>, Box<[u8]>>,
388 invariant_static: Yoke<InvariantStatic<'static>, Box<[u8]>>,
389 raw: Yoke<RawLifetime<'static>, Box<[u8]>>,
390 mixed_raw: Yoke<MixedRawLifetime<'static>, Box<[u8]>>,
391 yoke_lt: Yoke<YokeLifetime<'static>, Box<[u8]>>,
392 // const_gen: Yoke<ConstGenerics<'static>, Box<[u8]>>,
393 variadic: Yoke<Variadic<'static>, Box<[u8]>>,
394 simple_enum: Yoke<Enum, Box<[u8]>>,
395 empty: Yoke<Empty, Box<[u8]>>,
396 uninhabited: Yoke<Uninhabited<'static>, Box<[u8]>>,
397 almost_everything: Yoke<AlmostEverything<'static, u8, u32, u32>, Box<[u8]>>,
398 }
399
400 struct AssertYokeableManual {
401 string: Yoke<ManualStringExample, Box<[u8]>>,
402 int: Yoke<ManualIntExample, Box<[u8]>>,
403 gen1: Yoke<ManualGenericsExample<u32>, Box<[u8]>>,
404 gen2: Yoke<ManualGenericsExample<String>, Box<[u8]>>,
405 gen_default1: Yoke<ManualGenericsExampleWithDefault<String>, Box<[u8]>>,
406 gen_default2: Yoke<ManualGenericsExampleWithDefault<String, u8>, Box<[u8]>>,
407 cow: Yoke<ManualCowExample<'static>, Box<[u8]>>,
408 zv: Yoke<ManualZeroVecExample<'static>, Box<[u8]>>,
409 zv_gen1: Yoke<ManualZeroVecExampleWithGenerics<'static, u8>, Box<[u8]>>,
410 zv_gen2: Yoke<ManualZeroVecExampleWithGenerics<'static, char>, Box<[u8]>>,
411 map: Yoke<ManualZeroMapExample<'static>, Box<[u8]>>,
412 map_gen1: Yoke<ManualZeroMapGenericExample<'static, u32>, Box<[u8]>>,
413 map_gen2: Yoke<ManualZeroMapGenericExample<'static, str>, Box<[u8]>>,
414 maybe_sized_wrap: Yoke<ManualMaybeSizedWrap<usize, usize, str>, Box<[u8]>>,
415 trait_bounds: Yoke<ManualWithTraitBounds<u32>, Box<[u8]>>,
416 trait_bounds_where: Yoke<ManualWithTraitBoundsInWhere<u32>, Box<[u8]>>,
417 bound_fn: Yoke<ManualBoundFn<'static>, Box<[u8]>>,
418 invariant_static: Yoke<ManualInvariantStatic<'static>, Box<[u8]>>,
419 raw: Yoke<ManualRawLifetime<'static>, Box<[u8]>>,
420 mixed_raw: Yoke<ManualMixedRawLifetime<'static>, Box<[u8]>>,
421 yoke_lt: Yoke<ManualYokeLifetime<'static>, Box<[u8]>>,
422 // const_gen: Yoke<ManualConstGenerics<'static>, Box<[u8]>>,
423 variadic: Yoke<ManualVariadic<'static>, Box<[u8]>>,
424 simple_enum: Yoke<ManualEnum, Box<[u8]>>,
425 empty: Yoke<ManualEmpty, Box<[u8]>>,
426 uninhabited: Yoke<ManualUninhabited<'static>, Box<[u8]>>,
427 almost_everything: Yoke<ManualAlmostEverything<'static, u8, u32, u32, str>, Box<[u8]>>,
428 }
429
430 fn main() {}