Skip to content

Commit 4a66c5f

Browse files
committed
Support packed remote struct without destructuring
1 parent 714c8a5 commit 4a66c5f

4 files changed

Lines changed: 103 additions & 42 deletions

File tree

‎serde/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ mod lib {
157157
pub use std::*;
158158
}
159159

160-
pub use self::core::{cmp, iter, mem, num, slice, str};
160+
pub use self::core::{cmp, iter, mem, num, ptr, slice, str};
161161
pub use self::core::{f32, f64};
162162
pub use self::core::{i16, i32, i64, i8, isize};
163163
pub use self::core::{u16, u32, u64, u8, usize};

‎serde/src/private/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub use lib::default::Default;
1414
pub use lib::fmt::{self, Formatter};
1515
pub use lib::marker::PhantomData;
1616
pub use lib::option::Option::{self, None, Some};
17+
pub use lib::ptr;
1718
pub use lib::result::Result::{self, Err, Ok};
1819

1920
pub use self::string::from_utf8_lossy;

‎serde_derive/build.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ fn main() {
1616
if minor >= 37 {
1717
println!("cargo:rustc-cfg=underscore_consts");
1818
}
19+
20+
// The ptr::addr_of! macro stabilized in Rust 1.51:
21+
// https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html#stabilized-apis
22+
if minor >= 51 {
23+
println!("cargo:rustc-cfg=ptr_addr_of");
24+
}
1925
}
2026

2127
fn rustc_minor_version() -> Option<u32> {

‎serde_derive/src/pretend.rs‎

Lines changed: 95 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use proc_macro2::{Span, TokenStream};
2-
use syn::Ident;
1+
use proc_macro2::TokenStream;
2+
use quote::format_ident;
33

4-
use internals::ast::{Container, Data, Field, Style};
4+
use internals::ast::{Container, Data, Field, Style, Variant};
55

66
// Suppress dead_code warnings that would otherwise appear when using a remote
77
// derive. Other than this pretend code, a struct annotated with remote derive
@@ -32,49 +32,115 @@ pub fn pretend_used(cont: &Container, is_packed: bool) -> TokenStream {
3232

3333
// For structs with named fields, expands to:
3434
//
35+
// match None::<&T> {
36+
// Some(T { a: __v0, b: __v1 }) => {}
37+
// _ => {}
38+
// }
39+
//
40+
// For packed structs on sufficiently new rustc, expands to:
41+
//
42+
// match None::<&T> {
43+
// Some(__v @ T { a: _, b: _ }) => {
44+
// let _ = addr_of!(__v.a);
45+
// let _ = addr_of!(__v.b);
46+
// }
47+
// _ => {}
48+
// }
49+
//
50+
// For packed structs on older rustc, we assume Sized and !Drop, and expand to:
51+
//
3552
// match None::<T> {
36-
// Some(T { a: ref __v0, b: ref __v1 }) => {}
53+
// Some(T { a: __v0, b: __v1 }) => {}
3754
// _ => {}
3855
// }
3956
//
4057
// For enums, expands to the following but only including struct variants:
4158
//
42-
// match None::<T> {
43-
// Some(T::A { a: ref __v0 }) => {}
44-
// Some(T::B { b: ref __v0 }) => {}
59+
// match None::<&T> {
60+
// Some(T::A { a: __v0 }) => {}
61+
// Some(T::B { b: __v0 }) => {}
4562
// _ => {}
4663
// }
4764
//
48-
// The `ref` is important in case the user has written a Drop impl on their
49-
// type. Rust does not allow destructuring a struct or enum that has a Drop
50-
// impl.
5165
fn pretend_fields_used(cont: &Container, is_packed: bool) -> TokenStream {
66+
match &cont.data {
67+
Data::Enum(variants) => pretend_fields_used_enum(cont, variants),
68+
Data::Struct(Style::Struct, fields) => if is_packed {
69+
pretend_fields_used_struct_packed(cont, fields)
70+
} else {
71+
pretend_fields_used_struct(cont, fields)
72+
},
73+
Data::Struct(_, _) => quote!(),
74+
}
75+
}
76+
77+
fn pretend_fields_used_struct(cont: &Container, fields: &[Field]) -> TokenStream {
5278
let type_ident = &cont.ident;
5379
let (_, ty_generics, _) = cont.generics.split_for_impl();
5480

55-
let patterns = match &cont.data {
56-
Data::Enum(variants) => variants
57-
.iter()
58-
.filter_map(|variant| match variant.style {
59-
Style::Struct => {
60-
let variant_ident = &variant.ident;
61-
let pat = struct_pattern(&variant.fields, is_packed);
62-
Some(quote!(#type_ident::#variant_ident #pat))
81+
let members = fields.iter().map(|field| &field.member);
82+
let placeholders = (0usize..).map(|i| format_ident!("__v{}", i));
83+
84+
quote! {
85+
match _serde::__private::None::<&#type_ident #ty_generics> {
86+
_serde::__private::Some(#type_ident { #(#members: #placeholders),* }) => {}
87+
_ => {}
88+
}
89+
}
90+
}
91+
92+
fn pretend_fields_used_struct_packed(cont: &Container, fields: &[Field]) -> TokenStream {
93+
let type_ident = &cont.ident;
94+
let (_, ty_generics, _) = cont.generics.split_for_impl();
95+
96+
let members = fields.iter().map(|field| &field.member).collect::<Vec<_>>();
97+
98+
#[cfg(ptr_addr_of)]
99+
{
100+
quote! {
101+
match _serde::__private::None::<&#type_ident #ty_generics> {
102+
_serde::__private::Some(__v @ #type_ident { #(#members: _),* }) => {
103+
#(
104+
let _ = _serde::__private::ptr::addr_of!(__v.#members);
105+
)*
63106
}
64-
_ => None,
65-
})
66-
.collect::<Vec<_>>(),
67-
Data::Struct(Style::Struct, fields) => {
68-
let pat = struct_pattern(fields, is_packed);
69-
vec![quote!(#type_ident #pat)]
107+
_ => {}
108+
}
70109
}
71-
Data::Struct(_, _) => {
72-
return quote!();
110+
}
111+
112+
#[cfg(not(ptr_addr_of))]
113+
{
114+
let placeholders = (0usize..).map(|i| format_ident!("__v{}", i));
115+
116+
quote! {
117+
match _serde::__private::None::<#type_ident #ty_generics> {
118+
_serde::__private::Some(#type_ident { #(#members: #placeholders),* }) => {}
119+
_ => {}
120+
}
73121
}
74-
};
122+
}
123+
}
124+
125+
fn pretend_fields_used_enum(cont: &Container, variants: &[Variant]) -> TokenStream {
126+
let type_ident = &cont.ident;
127+
let (_, ty_generics, _) = cont.generics.split_for_impl();
128+
129+
let patterns = variants
130+
.iter()
131+
.filter_map(|variant| match variant.style {
132+
Style::Struct => {
133+
let variant_ident = &variant.ident;
134+
let members = variant.fields.iter().map(|field| &field.member);
135+
let placeholders = (0usize..).map(|i| format_ident!("__v{}", i));
136+
Some(quote!(#type_ident::#variant_ident { #(#members: #placeholders),* }))
137+
}
138+
_ => None,
139+
})
140+
.collect::<Vec<_>>();
75141

76142
quote! {
77-
match _serde::__private::None::<#type_ident #ty_generics> {
143+
match _serde::__private::None::<&#type_ident #ty_generics> {
78144
#(
79145
_serde::__private::Some(#patterns) => {}
80146
)*
@@ -107,7 +173,7 @@ fn pretend_variants_used(cont: &Container) -> TokenStream {
107173
let cases = variants.iter().map(|variant| {
108174
let variant_ident = &variant.ident;
109175
let placeholders = &(0..variant.fields.len())
110-
.map(|i| Ident::new(&format!("__v{}", i), Span::call_site()))
176+
.map(|i| format_ident!("__v{}", i))
111177
.collect::<Vec<_>>();
112178

113179
let pat = match variant.style {
@@ -131,15 +197,3 @@ fn pretend_variants_used(cont: &Container) -> TokenStream {
131197

132198
quote!(#(#cases)*)
133199
}
134-
135-
fn struct_pattern(fields: &[Field], is_packed: bool) -> TokenStream {
136-
let members = fields.iter().map(|field| &field.member);
137-
let placeholders =
138-
(0..fields.len()).map(|i| Ident::new(&format!("__v{}", i), Span::call_site()));
139-
let take_reference = if is_packed {
140-
None
141-
} else {
142-
Some(quote!(ref))
143-
};
144-
quote!({ #(#members: #take_reference #placeholders),* })
145-
}

0 commit comments

Comments
 (0)