summaryrefslogtreecommitdiffstats
path: root/third_party/rust/zerofrom-derive/examples/zf_derive.rs
blob: a1bde83733c4c0801f767bf3f8a16a6d7420ea90 (plain)
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
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

#![allow(unused)]

use std::borrow::Cow;
use zerofrom::ZeroFrom;
use zerovec::{maps::ZeroMapKV, ule::AsULE, VarZeroVec, ZeroMap, ZeroVec};

#[derive(ZeroFrom, Copy, Clone)]
pub struct IntExample {
    x: u32,
}

#[derive(ZeroFrom, Copy, Clone)]
pub struct GenericsExample<T> {
    x: u32,
    y: T,
}

#[derive(ZeroFrom, Copy, Clone)]
pub struct GenericsExampleWithDefault<T, U = usize> {
    x: T,
    y: U,
}

#[derive(ZeroFrom)]
pub struct CowExample<'a> {
    x: u8,
    y: &'a str,
    z: Cow<'a, str>,
    w: Cow<'a, [u8]>,
}

#[derive(ZeroFrom)]
pub struct ZeroVecExample<'a> {
    var: VarZeroVec<'a, str>,
    vec: ZeroVec<'a, u16>,
}

#[derive(ZeroFrom)]
pub struct ZeroVecExampleWithGenerics<'a, T: AsULE> {
    gen: ZeroVec<'a, T>,
    vec: ZeroVec<'a, u16>,
    bare: T,
}

#[derive(ZeroFrom)]
pub struct HasTuples<'data> {
    pub bar: (&'data str, &'data str),
}

pub fn assert_zf_tuples<'b>(x: &'b HasTuples) -> HasTuples<'b> {
    HasTuples::zero_from(x)
}
pub fn assert_zf_generics<'a, 'b>(
    x: &'b ZeroVecExampleWithGenerics<'a, u8>,
) -> ZeroVecExampleWithGenerics<'b, u8> {
    ZeroVecExampleWithGenerics::<'b, u8>::zero_from(x)
}

#[derive(ZeroFrom)]
pub struct ZeroMapGenericExample<'a, T: for<'b> ZeroMapKV<'b> + ?Sized> {
    map: ZeroMap<'a, str, T>,
}

pub fn assert_zf_map<'b>(x: &'b ZeroMapGenericExample<str>) -> ZeroMapGenericExample<'b, str> {
    ZeroMapGenericExample::zero_from(x)
}

#[derive(Clone, ZeroFrom)]
pub struct CloningZF1 {
    #[zerofrom(clone)] // Vec is not ZeroFrom, so it needs to be cloned
    vec: Vec<u8>,
}

#[derive(Clone, ZeroFrom)]
pub struct CloningZF2<'data> {
    #[zerofrom(clone)] // Cow is ZeroFrom, but we force a clone
    cow: Cow<'data, str>,
}

#[derive(ZeroFrom)]
pub enum CloningZF3<'data> {
    Cow(#[zerofrom(clone)] Cow<'data, str>),
}

#[derive(ZeroFrom)]
#[zerofrom(may_borrow(T))] // instead of treating T as a copy type, we want to allow zerofromming T too
pub struct GenericsThatAreAlsoZf<T> {
    x: T,
    y: Option<T>,
}

pub fn assert_zf_generics_may_borrow<'a, 'b>(
    x: &'b GenericsThatAreAlsoZf<&'a str>,
) -> GenericsThatAreAlsoZf<&'b str> {
    GenericsThatAreAlsoZf::<&'b str>::zero_from(x)
}

#[derive(ZeroFrom)]
pub struct UsesGenericsThatAreAlsoZf<'a> {
    x: GenericsThatAreAlsoZf<&'a str>,
}

// Ensure it works with invariant types too
#[derive(ZeroFrom)]
pub struct UsesGenericsThatAreAlsoZfWithMap<'a> {
    x: GenericsThatAreAlsoZf<ZeroMap<'a, str, str>>,
}

fn main() {}