use proc_macro2::{Delimiter, Group, Span, TokenStream};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use syn::{
parse_quote, token, visit_mut::VisitMut, Attribute, Data, DataEnum, DeriveInput, Error, Field,
Fields, FieldsNamed, FieldsUnnamed, Generics, Ident, Index, Lifetime, LifetimeDef, Meta,
MetaList, NestedMeta, Result, Token, Type, Variant, Visibility, WhereClause,
};
use super::{
args::{parse_args, Args, ProjReplace, UnpinImpl},
PIN,
};
use crate::utils::{
determine_lifetime_name, determine_visibility, insert_lifetime_and_bound, ReplaceReceiver,
SliceExt, Variants,
};
pub(super) fn parse_derive(input: TokenStream) -> Result<TokenStream> {
let mut input: DeriveInput = syn::parse2(input)?;
let mut cx;
let mut generate = GenerateTokens::default();
let ident = &input.ident;
let ty_generics = input.generics.split_for_impl().1;
let self_ty = parse_quote!(#ident #ty_generics);
let mut visitor = ReplaceReceiver(&self_ty);
visitor.visit_generics_mut(&mut input.generics);
visitor.visit_data_mut(&mut input.data);
match &input.data {
Data::Struct(data) => {
cx = Context::new(&input.attrs, &input.vis, ident, &mut input.generics, Struct)?;
parse_struct(&mut cx, &data.fields, &mut generate)?;
}
Data::Enum(data) => {
cx = Context::new(&input.attrs, &input.vis, ident, &mut input.generics, Enum)?;
parse_enum(&mut cx, data, &mut generate)?;
}
Data::Union(_) => {
return Err(error!(
input,
"#[pin_project] attribute may only be used on structs or enums"
));
}
}
Ok(generate.into_tokens(&cx))
}
#[derive(Default)]
struct GenerateTokens {
exposed: TokenStream,
scoped: TokenStream,
}
impl GenerateTokens {
fn extend(&mut self, expose: bool, tokens: TokenStream) {
if expose {
self.exposed.extend(tokens);
} else {
self.scoped.extend(tokens);
}
}
fn into_tokens(self, cx: &Context<'_>) -> TokenStream {
let mut tokens = self.exposed;
let scoped = self.scoped;
let unpin_impl = make_unpin_impl(cx);
let drop_impl = make_drop_impl(cx);
let allowed_lints = global_allowed_lints();
tokens.extend(quote! {
#allowed_lints
#[allow(clippy::used_underscore_binding)]
const _: () = {
#scoped
#unpin_impl
#drop_impl
};
});
tokens
}
}
fn global_allowed_lints() -> TokenStream {
quote! {
#[allow(box_pointers)]
#[allow(deprecated)]
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(unreachable_pub)]
#[allow(clippy::unknown_clippy_lints)]
#[allow(clippy::pattern_type_mismatch)]
#[allow(clippy::redundant_pub_crate)]
}
}
fn proj_allowed_lints(kind: TypeKind) -> (TokenStream, TokenStream, TokenStream) {
let large_enum_variant = if kind == Enum {
Some(quote! {
#[allow(variant_size_differences)]
#[allow(clippy::large_enum_variant)]
})
} else {
None
};
let global_allowed_lints = global_allowed_lints();
let proj_mut = quote! {
#global_allowed_lints
#[allow(dead_code)]
#[allow(clippy::mut_mut)]
#[allow(clippy::type_repetition_in_bounds)]
};
let proj_ref = quote! {
#global_allowed_lints
#[allow(dead_code)]
#[allow(clippy::ref_option_ref)]
#[allow(clippy::type_repetition_in_bounds)]
};
let proj_own = quote! {
#global_allowed_lints
#[allow(dead_code)]
#large_enum_variant
};
(proj_mut, proj_ref, proj_own)
}
struct Context<'a> {
orig: OriginalType<'a>,
proj: ProjectedType,
pinned_fields: Vec<Type>,
kind: TypeKind,
pinned_drop: Option<Span>,
unpin_impl: UnpinImpl,
project: bool,
project_ref: bool,
project_replace: ProjReplace,
}
impl<'a> Context<'a> {
fn new(
attrs: &'a [Attribute],
vis: &'a Visibility,
ident: &'a Ident,
generics: &'a mut Generics,
kind: TypeKind,
) -> Result<Self> {
let Args { pinned_drop, unpin_impl, project, project_ref, project_replace } =
parse_args(attrs)?;
if let Some(name) = [project.as_ref(), project_ref.as_ref(), project_replace.ident()]
.iter()
.filter_map(Option::as_ref)
.find(|name| **name == ident)
{
return Err(error!(name, "name `{}` is the same as the original type name", name));
}
let mut lifetime_name = String::from("'pin");
determine_lifetime_name(&mut lifetime_name, generics);
let lifetime = Lifetime::new(&lifetime_name, Span::call_site());
let ty_generics = generics.split_for_impl().1;
let ty_generics_as_generics = parse_quote!(#ty_generics);
let mut proj_generics = generics.clone();
let pred = insert_lifetime_and_bound(
&mut proj_generics,
lifetime.clone(),
&ty_generics_as_generics,
ident,
);
let mut where_clause = generics.make_where_clause().clone();
where_clause.predicates.push(pred);
let own_ident = project_replace
.ident()
.cloned()
.unwrap_or_else(|| format_ident!("__{}ProjectionOwned", ident));
Ok(Self {
kind,
pinned_drop,
unpin_impl,
project: project.is_some(),
project_ref: project_ref.is_some(),
project_replace,
proj: ProjectedType {
vis: determine_visibility(vis),
mut_ident: project.unwrap_or_else(|| format_ident!("__{}Projection", ident)),
ref_ident: project_ref.unwrap_or_else(|| format_ident!("__{}ProjectionRef", ident)),
own_ident,
lifetime,
generics: proj_generics,
where_clause,
},
orig: OriginalType { attrs, vis, ident, generics },
pinned_fields: Vec::new(),
})
}
}
#[derive(Copy, Clone, Eq, PartialEq)]
enum TypeKind {
Enum,
Struct,
}
use TypeKind::{Enum, Struct};
struct OriginalType<'a> {
attrs: &'a [Attribute],
vis: &'a Visibility,
ident: &'a Ident,
generics: &'a Generics,
}
struct ProjectedType {
vis: Visibility,
mut_ident: Ident,
ref_ident: Ident,
own_ident: Ident,
lifetime: Lifetime,
generics: Generics,
where_clause: WhereClause,
}
struct ProjectedVariants {
proj_variants: TokenStream,
proj_ref_variants: TokenStream,
proj_own_variants: TokenStream,
proj_arms: TokenStream,
proj_ref_arms: TokenStream,
proj_own_arms: TokenStream,
}
#[derive(Default)]
struct ProjectedFields {
proj_pat: TokenStream,
proj_body: TokenStream,
proj_own_body: TokenStream,
proj_fields: TokenStream,
proj_ref_fields: TokenStream,
proj_own_fields: TokenStream,
}
fn validate_struct(ident: &Ident, fields: &Fields) -> Result<()> {
if fields.is_empty() {
let msg = "#[pin_project] attribute may not be used on structs with zero fields";
if let Fields::Unit = fields { Err(error!(ident, msg)) } else { Err(error!(fields, msg)) }
} else {
Ok(())
}
}
fn validate_enum(brace_token: token::Brace, variants: &Variants) -> Result<()> {
if variants.is_empty() {
return Err(Error::new(
brace_token.span,
"#[pin_project] attribute may not be used on enums without variants",
));
}
let has_field = variants.iter().try_fold(false, |has_field, v| {
if let Some((_, e)) = &v.discriminant {
Err(error!(e, "#[pin_project] attribute may not be used on enums with discriminants"))
} else if let Some(attr) = v.attrs.find(PIN) {
Err(error!(attr, "#[pin] attribute may only be used on fields of structs or variants"))
} else if v.fields.is_empty() {
Ok(has_field)
} else {
Ok(true)
}
})?;
if has_field {
Ok(())
} else {
Err(error!(variants, "#[pin_project] attribute may not be used on enums with zero fields"))
}
}
fn parse_struct(
cx: &mut Context<'_>,
fields: &Fields,
generate: &mut GenerateTokens,
) -> Result<()> {
let packed_check = ensure_not_packed(&cx.orig, fields)?;
validate_struct(cx.orig.ident, fields)?;
let ProjectedFields {
proj_pat,
proj_body,
proj_fields,
proj_ref_fields,
proj_own_fields,
proj_own_body,
} = match fields {
Fields::Named(_) => visit_fields(cx, None, fields, Delimiter::Brace)?,
Fields::Unnamed(_) => visit_fields(cx, None, fields, Delimiter::Parenthesis)?,
Fields::Unit => unreachable!(),
};
let proj_ident = &cx.proj.mut_ident;
let proj_ref_ident = &cx.proj.ref_ident;
let proj_own_ident = &cx.proj.own_ident;
let vis = &cx.proj.vis;
let mut orig_generics = cx.orig.generics.clone();
let orig_where_clause = orig_generics.where_clause.take();
let proj_generics = &cx.proj.generics;
let proj_where_clause = &cx.proj.where_clause;
let (where_clause_fields, where_clause_ref_fields, where_clause_own_fields) = match fields {
Fields::Named(_) => (
quote!(#proj_where_clause #proj_fields),
quote!(#proj_where_clause #proj_ref_fields),
quote!(#orig_where_clause #proj_own_fields),
),
Fields::Unnamed(_) => (
quote!(#proj_fields #proj_where_clause;),
quote!(#proj_ref_fields #proj_where_clause;),
quote!(#proj_own_fields #orig_where_clause;),
),
Fields::Unit => unreachable!(),
};
let (proj_attrs, proj_ref_attrs, proj_own_attrs) = proj_allowed_lints(cx.kind);
generate.extend(cx.project, quote! {
#proj_attrs
#vis struct #proj_ident #proj_generics #where_clause_fields
});
generate.extend(cx.project_ref, quote! {
#proj_ref_attrs
#vis struct #proj_ref_ident #proj_generics #where_clause_ref_fields
});
if cx.project_replace.span().is_some() {
generate.extend(cx.project_replace.ident().is_some(), quote! {
#proj_own_attrs
#vis struct #proj_own_ident #orig_generics #where_clause_own_fields
});
}
let proj_mut_body = quote! {
let Self #proj_pat = self.get_unchecked_mut();
#proj_ident #proj_body
};
let proj_ref_body = quote! {
let Self #proj_pat = self.get_ref();
#proj_ref_ident #proj_body
};
let proj_own_body = quote! {
let Self #proj_pat = &mut *__self_ptr;
#proj_own_body
};
generate.extend(false, make_proj_impl(cx, &proj_mut_body, &proj_ref_body, &proj_own_body));
generate.extend(false, packed_check);
Ok(())
}
fn parse_enum(
cx: &mut Context<'_>,
DataEnum { brace_token, variants, .. }: &DataEnum,
generate: &mut GenerateTokens,
) -> Result<()> {
if let ProjReplace::Unnamed { span } = &cx.project_replace {
return Err(Error::new(
*span,
"`project_replace` argument requires a value when used on enums",
));
}
validate_enum(*brace_token, variants)?;
let ProjectedVariants {
proj_variants,
proj_ref_variants,
proj_own_variants,
proj_arms,
proj_ref_arms,
proj_own_arms,
} = visit_variants(cx, variants)?;
let proj_ident = &cx.proj.mut_ident;
let proj_ref_ident = &cx.proj.ref_ident;
let proj_own_ident = &cx.proj.own_ident;
let vis = &cx.proj.vis;
let mut orig_generics = cx.orig.generics.clone();
let orig_where_clause = orig_generics.where_clause.take();
let proj_generics = &cx.proj.generics;
let proj_where_clause = &cx.proj.where_clause;
let (proj_attrs, proj_ref_attrs, proj_own_attrs) = proj_allowed_lints(cx.kind);
if cx.project {
generate.extend(true, quote! {
#proj_attrs
#vis enum #proj_ident #proj_generics #proj_where_clause {
#proj_variants
}
});
}
if cx.project_ref {
generate.extend(true, quote! {
#proj_ref_attrs
#vis enum #proj_ref_ident #proj_generics #proj_where_clause {
#proj_ref_variants
}
});
}
if cx.project_replace.ident().is_some() {
generate.extend(true, quote! {
#proj_own_attrs
#vis enum #proj_own_ident #orig_generics #orig_where_clause {
#proj_own_variants
}
});
}
let proj_mut_body = quote! {
match self.get_unchecked_mut() {
#proj_arms
}
};
let proj_ref_body = quote! {
match self.get_ref() {
#proj_ref_arms
}
};
let proj_own_body = quote! {
match &mut *__self_ptr {
#proj_own_arms
}
};
generate.extend(false, make_proj_impl(cx, &proj_mut_body, &proj_ref_body, &proj_own_body));
Ok(())
}
fn visit_variants(cx: &mut Context<'_>, variants: &Variants) -> Result<ProjectedVariants> {
let mut proj_variants = TokenStream::new();
let mut proj_ref_variants = TokenStream::new();
let mut proj_own_variants = TokenStream::new();
let mut proj_arms = TokenStream::new();
let mut proj_ref_arms = TokenStream::new();
let mut proj_own_arms = TokenStream::new();
for Variant { ident, fields, .. } in variants {
let ProjectedFields {
proj_pat,
proj_body,
proj_fields,
proj_ref_fields,
proj_own_fields,
proj_own_body,
} = match fields {
Fields::Named(_) => visit_fields(cx, Some(ident), fields, Delimiter::Brace)?,
Fields::Unnamed(_) => visit_fields(cx, Some(ident), fields, Delimiter::Parenthesis)?,
Fields::Unit => ProjectedFields {
proj_own_body: proj_own_body(cx, Some(ident), None, &[]),
..ProjectedFields::default()
},
};
let proj_ident = &cx.proj.mut_ident;
let proj_ref_ident = &cx.proj.ref_ident;
proj_variants.extend(quote! {
#ident #proj_fields,
});
proj_ref_variants.extend(quote! {
#ident #proj_ref_fields,
});
proj_own_variants.extend(quote! {
#ident #proj_own_fields,
});
proj_arms.extend(quote! {
Self::#ident #proj_pat => #proj_ident::#ident #proj_body,
});
proj_ref_arms.extend(quote! {
Self::#ident #proj_pat => #proj_ref_ident::#ident #proj_body,
});
proj_own_arms.extend(quote! {
Self::#ident #proj_pat => { #proj_own_body }
});
}
Ok(ProjectedVariants {
proj_variants,
proj_ref_variants,
proj_own_variants,
proj_arms,
proj_ref_arms,
proj_own_arms,
})
}
fn visit_fields(
cx: &mut Context<'_>,
variant_ident: Option<&Ident>,
fields: &Fields,
delim: Delimiter,
) -> Result<ProjectedFields> {
fn surround(delim: Delimiter, tokens: TokenStream) -> TokenStream {
Group::new(delim, tokens).into_token_stream()
}
let mut proj_pat = TokenStream::new();
let mut proj_body = TokenStream::new();
let mut proj_fields = TokenStream::new();
let mut proj_ref_fields = TokenStream::new();
let mut proj_own_fields = TokenStream::new();
let mut proj_move = TokenStream::new();
let mut pinned_bindings = Vec::with_capacity(fields.len());
for (i, Field { attrs, vis, ident, colon_token, ty }) in fields.iter().enumerate() {
let binding = ident.clone().unwrap_or_else(|| format_ident!("_{}", i));
proj_pat.extend(quote!(#binding,));
if attrs.position_exact(PIN)?.is_some() {
let lifetime = &cx.proj.lifetime;
proj_fields.extend(quote! {
#vis #ident #colon_token ::pin_project::__private::Pin<&#lifetime mut (#ty)>,
});
proj_ref_fields.extend(quote! {
#vis #ident #colon_token ::pin_project::__private::Pin<&#lifetime (#ty)>,
});
proj_own_fields.extend(quote! {
#vis #ident #colon_token ::pin_project::__private::PhantomData<#ty>,
});
proj_body.extend(quote! {
#ident #colon_token ::pin_project::__private::Pin::new_unchecked(#binding),
});
proj_move.extend(quote! {
#ident #colon_token ::pin_project::__private::PhantomData,
});
cx.pinned_fields.push(ty.clone());
pinned_bindings.push(binding);
} else {
let lifetime = &cx.proj.lifetime;
proj_fields.extend(quote! {
#vis #ident #colon_token &#lifetime mut (#ty),
});
proj_ref_fields.extend(quote! {
#vis #ident #colon_token &#lifetime (#ty),
});
proj_own_fields.extend(quote! {
#vis #ident #colon_token #ty,
});
proj_body.extend(quote! {
#binding,
});
proj_move.extend(quote! {
#ident #colon_token ::pin_project::__private::ptr::read(#binding),
});
}
}
let proj_pat = surround(delim, proj_pat);
let proj_body = surround(delim, proj_body);
let proj_fields = surround(delim, proj_fields);
let proj_ref_fields = surround(delim, proj_ref_fields);
let proj_own_fields = surround(delim, proj_own_fields);
let proj_move = Group::new(delim, proj_move);
let proj_own_body = proj_own_body(cx, variant_ident, Some(proj_move), &pinned_bindings);
Ok(ProjectedFields {
proj_pat,
proj_body,
proj_own_body,
proj_fields,
proj_ref_fields,
proj_own_fields,
})
}
fn proj_own_body(
cx: &Context<'_>,
variant_ident: Option<&Ident>,
proj_move: Option<Group>,
pinned_fields: &[Ident],
) -> TokenStream {
let ident = &cx.proj.own_ident;
let proj_own = match variant_ident {
Some(variant_ident) => quote!(#ident::#variant_ident),
None => quote!(#ident),
};
let pinned_fields = pinned_fields.iter().rev();
quote! {
let __result = #proj_own #proj_move;
{
#(
let __guard = ::pin_project::__private::UnsafeDropInPlaceGuard(#pinned_fields);
)*
}
__result
}
}
fn make_unpin_impl(cx: &Context<'_>) -> TokenStream {
match cx.unpin_impl {
UnpinImpl::Unsafe(span) => {
let mut proj_generics = cx.proj.generics.clone();
let orig_ident = cx.orig.ident;
let lifetime = &cx.proj.lifetime;
proj_generics.make_where_clause().predicates.push(parse_quote_spanned! { span =>
::pin_project::__private::Wrapper<#lifetime, Self>: ::pin_project::UnsafeUnpin
});
let (impl_generics, _, where_clause) = proj_generics.split_for_impl();
let ty_generics = cx.orig.generics.split_for_impl().1;
quote_spanned! { span =>
impl #impl_generics ::pin_project::__private::Unpin for #orig_ident #ty_generics
#where_clause
{
}
}
}
UnpinImpl::Negative(span) => {
let mut proj_generics = cx.proj.generics.clone();
let orig_ident = cx.orig.ident;
let lifetime = &cx.proj.lifetime;
proj_generics.make_where_clause().predicates.push(parse_quote! {
::pin_project::__private::Wrapper<
#lifetime, ::pin_project::__private::PhantomPinned
>: ::pin_project::__private::Unpin
});
let (proj_impl_generics, _, proj_where_clause) = proj_generics.split_for_impl();
let ty_generics = cx.orig.generics.split_for_impl().1;
let unsafety = <Token![unsafe]>::default();
quote_spanned! { span =>
impl #proj_impl_generics ::pin_project::__private::Unpin
for #orig_ident #ty_generics
#proj_where_clause
{
}
#[doc(hidden)]
#unsafety impl #proj_impl_generics ::pin_project::UnsafeUnpin
for #orig_ident #ty_generics
#proj_where_clause
{
}
}
}
UnpinImpl::Default => {
let mut full_where_clause = cx.orig.generics.where_clause.clone().unwrap();
let fields = cx.pinned_fields.iter().enumerate().map(|(i, ty)| {
let field_ident = format_ident!("__field{}", i);
quote!(#field_ident: #ty)
});
let lifetime_fields = cx.orig.generics.lifetimes().enumerate().map(
|(i, LifetimeDef { lifetime, .. })| {
let field_ident = format_ident!("__lifetime{}", i);
quote!(#field_ident: &#lifetime ())
},
);
let orig_ident = cx.orig.ident;
let struct_ident = format_ident!("__{}", orig_ident);
let vis = cx.orig.vis;
let lifetime = &cx.proj.lifetime;
let type_params = cx.orig.generics.type_params().map(|t| &t.ident);
let proj_generics = &cx.proj.generics;
let (proj_impl_generics, proj_ty_generics, _) = proj_generics.split_for_impl();
let (_, ty_generics, where_clause) = cx.orig.generics.split_for_impl();
full_where_clause.predicates.push(parse_quote! {
#struct_ident #proj_ty_generics: ::pin_project::__private::Unpin
});
quote! {
#[allow(missing_debug_implementations)]
#vis struct #struct_ident #proj_generics #where_clause {
__pin_project_use_generics: ::pin_project::__private::AlwaysUnpin<
#lifetime, (#(::pin_project::__private::PhantomData<#type_params>),*)
>,
#(#fields,)*
#(#lifetime_fields,)*
}
impl #proj_impl_generics ::pin_project::__private::Unpin
for #orig_ident #ty_generics
#full_where_clause
{
}
#[doc(hidden)]
unsafe impl #proj_impl_generics ::pin_project::UnsafeUnpin
for #orig_ident #ty_generics
#full_where_clause
{
}
}
}
}
}
fn make_drop_impl(cx: &Context<'_>) -> TokenStream {
let ident = cx.orig.ident;
let (impl_generics, ty_generics, where_clause) = cx.orig.generics.split_for_impl();
if let Some(span) = cx.pinned_drop {
let unsafety = <Token![unsafe]>::default();
quote_spanned! { span =>
impl #impl_generics ::pin_project::__private::Drop for #ident #ty_generics
#where_clause
{
fn drop(&mut self) {
#unsafety {
let __pinned_self = ::pin_project::__private::Pin::new_unchecked(self);
::pin_project::__private::PinnedDrop::drop(__pinned_self);
}
}
}
}
} else {
let trait_ident = format_ident!("{}MustNotImplDrop", ident);
quote! {
trait #trait_ident {}
#[allow(clippy::drop_bounds, drop_bounds)]
impl<T: ::pin_project::__private::Drop> #trait_ident for T {}
impl #impl_generics #trait_ident for #ident #ty_generics #where_clause {}
#[doc(hidden)]
impl #impl_generics ::pin_project::__private::PinnedDrop for #ident #ty_generics
#where_clause
{
unsafe fn drop(self: ::pin_project::__private::Pin<&mut Self>) {}
}
}
}
}
fn make_proj_impl(
cx: &Context<'_>,
proj_body: &TokenStream,
proj_ref_body: &TokenStream,
proj_own_body: &TokenStream,
) -> TokenStream {
let vis = &cx.proj.vis;
let lifetime = &cx.proj.lifetime;
let orig_ident = cx.orig.ident;
let proj_ident = &cx.proj.mut_ident;
let proj_ref_ident = &cx.proj.ref_ident;
let proj_own_ident = &cx.proj.own_ident;
let orig_ty_generics = cx.orig.generics.split_for_impl().1;
let proj_ty_generics = cx.proj.generics.split_for_impl().1;
let (impl_generics, ty_generics, where_clause) = cx.orig.generics.split_for_impl();
let mut project = Some(quote! {
#vis fn project<#lifetime>(
self: ::pin_project::__private::Pin<&#lifetime mut Self>,
) -> #proj_ident #proj_ty_generics {
unsafe {
#proj_body
}
}
});
let mut project_ref = Some(quote! {
#[allow(clippy::missing_const_for_fn)]
#vis fn project_ref<#lifetime>(
self: ::pin_project::__private::Pin<&#lifetime Self>,
) -> #proj_ref_ident #proj_ty_generics {
unsafe {
#proj_ref_body
}
}
});
let mut project_replace = cx.project_replace.span().map(|span| {
let sig = quote_spanned! { span =>
#vis fn project_replace(
self: ::pin_project::__private::Pin<&mut Self>,
__replacement: Self,
) -> #proj_own_ident #orig_ty_generics
};
quote! {
#sig {
unsafe {
let __self_ptr: *mut Self = self.get_unchecked_mut();
let __guard = ::pin_project::__private::UnsafeOverwriteGuard {
target: __self_ptr,
value: ::pin_project::__private::ManuallyDrop::new(__replacement),
};
#proj_own_body
}
}
}
});
if cx.kind == Enum {
if !cx.project {
project = None;
}
if !cx.project_ref {
project_ref = None;
}
if cx.project_replace.ident().is_none() {
project_replace = None;
}
}
quote! {
impl #impl_generics #orig_ident #ty_generics #where_clause {
#project
#project_ref
#project_replace
}
}
}
fn ensure_not_packed(orig: &OriginalType<'_>, fields: &Fields) -> Result<TokenStream> {
for meta in orig.attrs.iter().filter_map(|attr| attr.parse_meta().ok()) {
if let Meta::List(list) = meta {
if list.path.is_ident("repr") {
for repr in list.nested.iter() {
match repr {
NestedMeta::Meta(Meta::Path(path))
| NestedMeta::Meta(Meta::List(MetaList { path, .. }))
if path.is_ident("packed") =>
{
return Err(error!(
repr,
"#[pin_project] attribute may not be used on #[repr(packed)] types"
));
}
_ => {}
}
}
}
}
}
let mut field_refs = vec![];
match fields {
Fields::Named(FieldsNamed { named, .. }) => {
for Field { ident, .. } in named {
field_refs.push(quote!(&this.#ident));
}
}
Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => {
for (index, _) in unnamed.iter().enumerate() {
let index = Index::from(index);
field_refs.push(quote!(&this.#index));
}
}
Fields::Unit => {}
}
let (impl_generics, ty_generics, where_clause) = orig.generics.split_for_impl();
let ident = orig.ident;
Ok(quote! {
#[forbid(safe_packed_borrows)]
fn __assert_not_repr_packed #impl_generics (this: &#ident #ty_generics) #where_clause {
#(let _ = #field_refs;)*
}
})
}