Remove build script from serde_derive

The current serde_derive's MSRV is 1.56, and both underscore consts and
ptr::addr_of! are always available.
This commit is contained in:
Taiki Endo 2023-05-06 05:34:38 +09:00
parent 99f165b45a
commit ef2a7c753f
5 changed files with 10 additions and 83 deletions

View File

@ -1,38 +0,0 @@
use std::env;
use std::process::Command;
use std::str;
// The rustc-cfg strings below are *not* public API. Please let us know by
// opening a GitHub issue if your build environment requires some way to enable
// these cfgs other than by executing our build script.
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let minor = match rustc_minor_version() {
Some(minor) => minor,
None => return,
};
// Underscore const names stabilized in Rust 1.37:
// https://blog.rust-lang.org/2019/08/15/Rust-1.37.0.html#using-unnamed-const-items-for-macros
if minor < 37 {
println!("cargo:rustc-cfg=no_underscore_consts");
}
// The ptr::addr_of! macro stabilized in Rust 1.51:
// https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html#stabilized-apis
if minor < 51 {
println!("cargo:rustc-cfg=no_ptr_addr_of");
}
}
fn rustc_minor_version() -> Option<u32> {
let rustc = env::var_os("RUSTC")?;
let output = Command::new(rustc).arg("--version").output().ok()?;
let version = str::from_utf8(&output.stdout).ok()?;
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
pieces.next()?.parse().ok()
}

View File

@ -69,8 +69,6 @@ pub fn expand_derive_deserialize(
Ok(dummy::wrap_in_const( Ok(dummy::wrap_in_const(
cont.attrs.custom_serde_path(), cont.attrs.custom_serde_path(),
"DESERIALIZE",
ident,
impl_block, impl_block,
)) ))
} }

View File

@ -1,23 +1,11 @@
use proc_macro2::{Ident, TokenStream}; use proc_macro2::TokenStream;
use quote::format_ident;
use syn; use syn;
use try; use try;
pub fn wrap_in_const( pub fn wrap_in_const(serde_path: Option<&syn::Path>, code: TokenStream) -> TokenStream {
serde_path: Option<&syn::Path>,
trait_: &str,
ty: &Ident,
code: TokenStream,
) -> TokenStream {
let try_replacement = try::replacement(); let try_replacement = try::replacement();
let dummy_const = if cfg!(no_underscore_consts) {
format_ident!("_IMPL_{}_FOR_{}", trait_, unraw(ty))
} else {
format_ident!("_")
};
let use_serde = match serde_path { let use_serde = match serde_path {
Some(path) => quote! { Some(path) => quote! {
use #path as _serde; use #path as _serde;
@ -31,14 +19,10 @@ pub fn wrap_in_const(
quote! { quote! {
#[doc(hidden)] #[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
const #dummy_const: () = { const _: () = {
#use_serde #use_serde
#try_replacement #try_replacement
#code #code
}; };
} }
} }
fn unraw(ident: &Ident) -> String {
ident.to_string().trim_start_matches("r#").to_owned()
}

View File

@ -97,29 +97,14 @@ fn pretend_fields_used_struct_packed(cont: &Container, fields: &[Field]) -> Toke
let members = fields.iter().map(|field| &field.member).collect::<Vec<_>>(); let members = fields.iter().map(|field| &field.member).collect::<Vec<_>>();
#[cfg(not(no_ptr_addr_of))] quote! {
{ match _serde::__private::None::<&#type_ident #ty_generics> {
quote! { _serde::__private::Some(__v @ #type_ident { #(#members: _),* }) => {
match _serde::__private::None::<&#type_ident #ty_generics> { #(
_serde::__private::Some(__v @ #type_ident { #(#members: _),* }) => { let _ = _serde::__private::ptr::addr_of!(__v.#members);
#( )*
let _ = _serde::__private::ptr::addr_of!(__v.#members);
)*
}
_ => {}
}
}
}
#[cfg(no_ptr_addr_of)]
{
let placeholders = (0usize..).map(|i| format_ident!("__v{}", i));
quote! {
match _serde::__private::None::<#type_ident #ty_generics> {
_serde::__private::Some(#type_ident { #(#members: #placeholders),* }) => {}
_ => {}
} }
_ => {}
} }
} }
} }

View File

@ -59,8 +59,6 @@ pub fn expand_derive_serialize(
Ok(dummy::wrap_in_const( Ok(dummy::wrap_in_const(
cont.attrs.custom_serde_path(), cont.attrs.custom_serde_path(),
"SERIALIZE",
ident,
impl_block, impl_block,
)) ))
} }