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
use quote::quote;
use syn::{Data, DeriveInput, Meta};
use super::{
TraitHandler,
models::{FieldAttributeBuilder, FieldName, TypeAttributeBuilder, TypeName},
};
use crate::{supported_traits::Trait, trait_handlers::TraitHandlerContext};
/// Generates the `Debug` implementation for a union.
pub(crate) struct DebugUnionHandler;
impl TraitHandler for DebugUnionHandler {
fn trait_meta_handler(
ast: &DeriveInput,
_ctx: &mut TraitHandlerContext,
token_stream: &mut proc_macro2::TokenStream,
traits: &[Trait],
meta: &Meta,
) -> syn::Result<()> {
let generated_impl_attributes =
crate::common::attributes::generated_impl_attributes(&ast.attrs);
let type_attribute = TypeAttributeBuilder {
enable_flag: true,
enable_unsafe: true,
enable_name: true,
enable_named_field: false,
enable_bound: false,
name: TypeName::Default,
named_field: false,
}
.build_from_debug_meta(meta)?;
if !type_attribute.has_unsafe {
return Err(super::panic::union_without_unsafe(meta));
}
let name = type_attribute.name.to_ident_by_ident(&ast.ident);
let mut builder_token_stream = proc_macro2::TokenStream::new();
if let Data::Union(data) = &ast.data {
for field in data.fields.named.iter() {
let _ = FieldAttributeBuilder {
enable_name: false,
enable_ignore: false,
enable_method: false,
name: FieldName::Default,
}
.build_from_attributes(&field.attrs, traits)?;
}
if let Some(name) = name {
builder_token_stream.extend(quote!(
let mut builder = f.debug_tuple(stringify!(#name));
let size = ::core::mem::size_of::<Self>();
// SAFETY: A union does not track its active field at runtime, so the whole value is intentionally read as a `u8` slice; because `self` is a live reference, the pointer is non-null, aligned, and valid for reads of `size` bytes within a single allocation.
// Those bytes may include padding that is not guaranteed to be initialized, so the output can expose uninitialized memory, a trade-off the user explicitly accepted through the `unsafe` keyword in the attribute.
let data = unsafe { ::core::slice::from_raw_parts(self as *const Self as *const u8, size) };
builder.field(&data);
builder.finish()
));
} else {
builder_token_stream.extend(quote!(
let size = ::core::mem::size_of::<Self>();
// SAFETY: A union does not track its active field at runtime, so the whole value is intentionally read as a `u8` slice; because `self` is a live reference, the pointer is non-null, aligned, and valid for reads of `size` bytes within a single allocation.
// Those bytes may include padding that is not guaranteed to be initialized, so the output can expose uninitialized memory, a trade-off the user explicitly accepted through the `unsafe` keyword in the attribute.
let data = unsafe { ::core::slice::from_raw_parts(self as *const Self as *const u8, size) };
::core::fmt::Debug::fmt(data, f)
));
}
}
let ident = &ast.ident;
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
token_stream.extend(quote! {
#generated_impl_attributes
impl #impl_generics ::core::fmt::Debug for #ident #ty_generics #where_clause {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
#builder_token_stream
}
}
});
Ok(())
}
}