MCPcopy Index your code
hub / github.com/SoftbearStudios/bitcode / field_impl

Method field_impl

bitcode_derive/src/encode.rs:28–81  ·  view source on GitHub ↗
(
        self,
        field_name: TokenStream,
        global_field_name: TokenStream,
        real_field_name: TokenStream,
        field_type: &Type,
    )

Source from the content-addressed store, hash-verified

26}
27impl crate::shared::Item for Item {
28 fn field_impl(
29 self,
30 field_name: TokenStream,
31 global_field_name: TokenStream,
32 real_field_name: TokenStream,
33 field_type: &Type,
34 ) -> TokenStream {
35 match self {
36 Self::Type => {
37 let static_type = replace_lifetimes(field_type, "static");
38 let private = private();
39 quote! {
40 #global_field_name: <#static_type as #private::Encode>::Encoder,
41 }
42 }
43 Self::Default => quote! {
44 #global_field_name: Default::default(),
45 },
46 Self::Encode | Self::EncodeVectored => {
47 let static_type = replace_lifetimes(field_type, "static");
48 let value = if &static_type != field_type {
49 let underscore_type = replace_lifetimes(field_type, "_");
50
51 // HACK: Since encoders don't have lifetimes we can't reference <T<'a> as Encode>::Encoder since 'a
52 // does not exist. Instead we replace this with <T<'static> as Encode>::Encoder and transmute it to
53 // T<'a>. No encoder actually encodes T<'static> any differently from T<'a> so this is sound.
54 quote! {
55 unsafe { std::mem::transmute::<&#underscore_type, &#static_type>(#field_name) }
56 }
57 } else {
58 quote! { #field_name }
59 };
60
61 if matches!(self, Self::EncodeVectored) {
62 quote! {
63 self.#global_field_name.encode_vectored(i.clone().map(|me| {
64 let #field_name = &me.#real_field_name;
65 #value
66 }));
67 }
68 } else {
69 quote! {
70 self.#global_field_name.encode(#value);
71 }
72 }
73 }
74 Self::CollectInto => quote! {
75 self.#global_field_name.collect_into(out);
76 },
77 Self::Reserve => quote! {
78 self.#global_field_name.reserve(__additional);
79 },
80 }
81 }
82
83 fn struct_impl(
84 self,

Callers 1

field_implsMethod · 0.45

Calls 2

replace_lifetimesFunction · 0.85
privateFunction · 0.85

Tested by

no test coverage detected