rust: pin-init: internal: extract utility code to new module

Create a new `util.rs` to host utility code that are generic and can be
shared by multiple macros.

Link: https://patch.msgid.link/20260904-tuple-struct-v1-1-72c50bd037fd@garyguo.net
Signed-off-by: Gary Guo <gary@garyguo.net>
This commit is contained in:
Gary Guo 2026-09-04 15:12:29 +01:00
parent fd73f4a665
commit c2bc9bd605
3 changed files with 33 additions and 12 deletions

View File

@ -18,6 +18,7 @@
mod init;
mod pin_data;
mod pinned_drop;
mod util;
mod zeroable;
#[proc_macro_attribute]

View File

@ -10,7 +10,10 @@
Field, Fields, Generics, Ident, Item, PathSegment, Type, TypePath, Visibility, WhereClause,
};
use crate::diagnostics::{DiagCtxt, ErrorGuaranteed};
use crate::{
diagnostics::{DiagCtxt, ErrorGuaranteed},
util::*,
};
pub(crate) mod kw {
syn::custom_keyword!(PinnedDrop);
@ -81,21 +84,11 @@ pub(crate) fn pin_data(
//
// We need to perform this after parsing so we can reliably detect field cfgs.
for (field_idx, field) in struct_.fields.iter_mut().enumerate() {
let cfg: Vec<_> = field
.attrs
.iter()
.filter(|a| a.path().is_ident("cfg"))
.map(|a| {
a.parse_args::<TokenStream>()
.expect("parse as token stream cannot fail")
})
.collect();
let cfg = field.attrs.extract_cfg_attrs();
if cfg.is_empty() {
continue;
}
field.attrs.retain(|a| !a.path().is_ident("cfg"));
let cfg_true_struct = quote!(#struct_);
let punctuated = match &mut struct_.fields {

View File

@ -0,0 +1,27 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT
use proc_macro2::TokenStream;
use syn::Attribute;
pub(crate) trait AttrListExt {
fn extract_cfg_attrs(&mut self) -> Vec<TokenStream>;
}
impl AttrListExt for Vec<Attribute> {
fn extract_cfg_attrs(&mut self) -> Vec<TokenStream> {
let cfg: Vec<_> = self
.iter()
.filter(|a| a.path().is_ident("cfg"))
.map(|a| {
a.parse_args::<TokenStream>()
.expect("parse as token stream cannot fail")
})
.collect();
if !cfg.is_empty() {
self.retain(|a| !a.path().is_ident("cfg"));
}
cfg
}
}