Skip to content

Commit

Permalink
Try constructing projection types first
Browse files Browse the repository at this point in the history
This ensures that we bail out early if we encounter an error
  • Loading branch information
Aaron1011 committed Aug 23, 2019
1 parent 19e6d69 commit c3be220
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
6 changes: 3 additions & 3 deletions pin-project-internal/src/pin_project/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::utils::VecExt;

use super::{proj_generics, Context, PIN};

pub(super) fn parse(mut cx: Context, mut item: ItemEnum) -> Result<TokenStream> {
pub(super) fn parse(cx: &mut Context, mut item: ItemEnum) -> Result<TokenStream> {
if item.variants.is_empty() {
return Err(error!(item, "cannot be implemented for enums without variants"));
}
Expand All @@ -23,9 +23,9 @@ pub(super) fn parse(mut cx: Context, mut item: ItemEnum) -> Result<TokenStream>
return Err(error!(item.variants, "cannot be implemented for enums that have no field"));
}

let (proj_variants, proj_arms) = variants(&mut cx, &mut item)?;
let (proj_variants, proj_arms) = variants(cx, &mut item)?;

let impl_drop = cx.impl_drop(&item.generics);
let mut impl_drop = cx.impl_drop(&item.generics);
let Context { original, projected, lifetime, impl_unpin, .. } = cx;
let proj_generics = proj_generics(&item.generics, &lifetime);
let proj_ty_generics = proj_generics.split_for_impl().1;
Expand Down
15 changes: 7 additions & 8 deletions pin-project-internal/src/pin_project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,19 @@ fn parse(args: TokenStream, input: TokenStream) -> Result<TokenStream> {
match syn::parse2(input)? {
Item::Struct(item) => {
let mut cx = Context::new(args, item.ident.clone(), &item.generics)?;
let mut res = make_proj_trait(&mut cx)?;

let packed_check = ensure_not_packed(&item)?;
res.extend(structs::parse(cx, item)?);
res.extend(packed_check);
let mut res = structs::parse(&mut cx, item.clone())?;
res.extend(ensure_not_packed(&item)?);
res.extend(make_proj_trait(&mut cx)?);
Ok(res)
}
Item::Enum(item) => {
let mut cx = Context::new(args, item.ident.clone(), &item.generics)?;
let mut res = make_proj_trait(&mut cx)?;

// We don't need to check for '#[repr(packed)]',
// since it does not apply to enums
res.extend(enums::parse(cx, item));
let mut res = enums::parse(&mut cx, item.clone())?;
res.extend(make_proj_trait(&mut cx)?);
Ok(res)
}
item => Err(error!(item, "may only be used on structs or enums")),
Expand Down Expand Up @@ -253,7 +252,7 @@ impl<'a> ImplDrop<'a> {
Self { generics, pinned_drop }
}

fn build(self, ident: &Ident) -> TokenStream {
fn build(&mut self, ident: &Ident) -> TokenStream {
let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl();

if let Some(pinned_drop) = self.pinned_drop {
Expand Down Expand Up @@ -325,7 +324,7 @@ impl ImplUnpin {
}

/// Creates `Unpin` implementation.
fn build(self, ident: &Ident) -> TokenStream {
fn build(&mut self, ident: &Ident) -> TokenStream {
let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl();
quote! {
impl #impl_generics ::core::marker::Unpin for #ident #ty_generics #where_clause {}
Expand Down
8 changes: 4 additions & 4 deletions pin-project-internal/src/pin_project/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::utils::VecExt;

use super::{proj_generics, Context, PIN};

pub(super) fn parse(mut cx: Context, mut item: ItemStruct) -> Result<TokenStream> {
pub(super) fn parse(cx: &mut Context, mut item: ItemStruct) -> Result<TokenStream> {
let (proj_fields, proj_init) = match &mut item.fields {
Fields::Named(FieldsNamed { named: fields, .. })
| Fields::Unnamed(FieldsUnnamed { unnamed: fields, .. })
Expand All @@ -16,14 +16,14 @@ pub(super) fn parse(mut cx: Context, mut item: ItemStruct) -> Result<TokenStream
}
Fields::Unit => return Err(error!(item, "cannot be implemented for structs with units")),

Fields::Named(fields) => named(&mut cx, fields)?,
Fields::Unnamed(fields) => unnamed(&mut cx, fields)?,
Fields::Named(fields) => named(cx, fields)?,
Fields::Unnamed(fields) => unnamed(cx, fields)?,
};

let orig_ident = &cx.original;
let proj_ident = &cx.projected;
let lifetime = &cx.lifetime;
let impl_drop = cx.impl_drop(&item.generics);
let mut impl_drop = cx.impl_drop(&item.generics);
let proj_generics = proj_generics(&item.generics, &cx.lifetime);
let proj_ty_generics = proj_generics.split_for_impl().1;
let proj_trait = &cx.projected_trait;
Expand Down

0 comments on commit c3be220

Please sign in to comment.