Debian Codegraph @

third_party/rust/zerofrom-derive/examples/zf_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;
8 use zerofrom::ZeroFrom;
9 use zerovec::{maps::ZeroMapKV, ule::AsULE, VarZeroVec, ZeroMap, ZeroVec};
10
11 #[derive(ZeroFrom, Copy, Clone)]
12 pub struct IntExample {
13 x: u32,
14 }
15
16 #[derive(ZeroFrom, Copy, Clone)]
17 pub struct GenericsExample<T> {
18 x: u32,
19 y: T,
20 }
21
22 #[derive(ZeroFrom, Copy, Clone)]
23 pub struct GenericsExampleWithDefault<T, U = usize> {
24 x: T,
25 y: U,
26 }
27
28 #[derive(ZeroFrom)]
29 pub struct CowExample<'a> {
30 x: u8,
31 y: &'a str,
32 z: Cow<'a, str>,
33 w: Cow<'a, [u8]>,
34 }
35
36 #[derive(ZeroFrom)]
37 pub struct ZeroVecExample<'a> {
38 var: VarZeroVec<'a, str>,
39 vec: ZeroVec<'a, u16>,
40 }
41
42 #[derive(ZeroFrom)]
43 pub struct ZeroVecExampleWithGenerics<'a, T: AsULE> {
44 gen: ZeroVec<'a, T>,
45 vec: ZeroVec<'a, u16>,
46 bare: T,
47 }
48
49 #[derive(ZeroFrom)]
50 pub struct HasTuples<'data> {
51 pub bar: (&'data str, &'data str),
52 }
53
54 pub fn assert_zf_tuples<'b>(x: &'b HasTuples) -> HasTuples<'b> {
55 HasTuples::zero_from(x)
56 }
57 pub fn assert_zf_generics<'a, 'b>(
58 x: &'b ZeroVecExampleWithGenerics<'a, u8>,
59 ) -> ZeroVecExampleWithGenerics<'b, u8> {
60 ZeroVecExampleWithGenerics::<'b, u8>::zero_from(x)
61 }
62
63 #[derive(ZeroFrom)]
64 pub struct ZeroMapGenericExample<'a, T: for<'b> ZeroMapKV<'b> + ?Sized> {
65 map: ZeroMap<'a, str, T>,
66 }
67
68 pub fn assert_zf_map<'b>(x: &'b ZeroMapGenericExample<str>) -> ZeroMapGenericExample<'b, str> {
69 ZeroMapGenericExample::zero_from(x)
70 }
71
72 #[derive(Clone, ZeroFrom)]
73 pub struct CloningZF1 {
74 #[zerofrom(clone)] // Vec is not ZeroFrom, so it needs to be cloned
75 vec: Vec<u8>,
76 }
77
78 #[derive(Clone, ZeroFrom)]
79 pub struct CloningZF2<'data> {
80 #[zerofrom(clone)] // Cow is ZeroFrom, but we force a clone
81 cow: Cow<'data, str>,
82 }
83
84 #[derive(ZeroFrom)]
85 pub enum CloningZF3<'data> {
86 Cow(#[zerofrom(clone)] Cow<'data, str>),
87 }
88
89 #[derive(ZeroFrom)]
90 #[zerofrom(may_borrow(T))] // instead of treating T as a copy type, we want to allow zerofromming T too
91 pub struct GenericsThatAreAlsoZf<T> {
92 x: T,
93 y: Option<T>,
94 }
95
96 pub fn assert_zf_generics_may_borrow<'a, 'b>(
97 x: &'b GenericsThatAreAlsoZf<&'a str>,
98 ) -> GenericsThatAreAlsoZf<&'b str> {
99 GenericsThatAreAlsoZf::<&'b str>::zero_from(x)
100 }
101
102 #[derive(ZeroFrom)]
103 pub struct UsesGenericsThatAreAlsoZf<'a> {
104 x: GenericsThatAreAlsoZf<&'a str>,
105 }
106
107 // Ensure it works with invariant types too
108 #[derive(ZeroFrom)]
109 pub struct UsesGenericsThatAreAlsoZfWithMap<'a> {
110 x: GenericsThatAreAlsoZf<ZeroMap<'a, str, str>>,
111 }
112
113 fn main() {}