Skip to content

Commit

Permalink
Merge #75
Browse files Browse the repository at this point in the history
75: Minor clean up r=taiki-e a=taiki-e



Co-authored-by: Taiki Endo <te316e89@gmail.com>
  • Loading branch information
bors[bot] and taiki-e committed Sep 6, 2019
2 parents 5408954 + 87b8171 commit 69f8014
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 77 deletions.
6 changes: 5 additions & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
version = "Two"
use_small_heuristics = "Max"

# Set the default settings again to always apply the proper formatting without being affected by the editor settings.
# Refs: https://github.com/rust-lang/rls/issues/501#issuecomment-333717736
edition = "2018"
tab_spaces = 4
use_small_heuristics = "Max"
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ edition = "2018"
license = "Apache-2.0/MIT"
repository = "https://github.com/taiki-e/pin-project"
documentation = "https://docs.rs/pin-project/"
readme = "README.md"
keywords = ["pin", "macros", "attribute"]
categories = ["rust-patterns"]
readme = "README.md"
description = "A crate for safe and ergonomic pin-projection."

[package.metadata.docs.rs]
Expand Down
21 changes: 20 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,28 @@ jobs:
steps:
- template: ci/azure-install-rust.yml
parameters:
toolchain: stable
toolchain: nightly
- script: |
set +e
if rustup component add rustfmt; then
set -e
else
# If rustfmt is unavailable on the latest nightly,
# use the latest toolchain with rustfmt available.
# Refs: https://github.com/rust-lang/rustup-components-history#the-web-part
set -e
target=`curl https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu/rustfmt`
echo "'rustfmt' is unavailable on the toolchain 'nightly', use the toolchain 'nightly-$target' instead"
rustup toolchain install nightly-$target
rustup default nightly-$target
rustup component add rustfmt
rustup toolchain list
rustc -Vv
cargo -V
fi
cargo fmt --version
displayName: Install rustfmt
- script: |
cargo fmt --all -- --check
displayName: cargo fmt -- --check
Expand Down
10 changes: 5 additions & 5 deletions examples/enum-default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ enum __EnumProjection<'_pin, T, U> {
Unpinned(&'_pin mut U),
}

trait __EnumProjectionTrait<'_outer_pin, T, U> {
fn project<'_pin>(&'_pin mut self) -> __EnumProjection<'_pin, T, U>;
fn project_into(self) -> __EnumProjection<'_outer_pin, T, U>;
}

impl<'_outer_pin, T, U> __EnumProjectionTrait<'_outer_pin, T, U>
for ::core::pin::Pin<&'_outer_pin mut Enum<T, U>>
{
Expand All @@ -53,6 +48,11 @@ impl<'_outer_pin, T, U> __EnumProjectionTrait<'_outer_pin, T, U>
}
}

trait __EnumProjectionTrait<'_outer_pin, T, U> {
fn project<'_pin>(&'_pin mut self) -> __EnumProjection<'_pin, T, U>;
fn project_into(self) -> __EnumProjection<'_outer_pin, T, U>;
}

// Automatically create the appropriate conditional Unpin implementation.
impl<T, U> Unpin for Enum<T, U> where T: Unpin {}

Expand Down
10 changes: 5 additions & 5 deletions examples/struct-default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ struct __StructProjection<'_pin, T, U> {
unpinned: &'_pin mut U,
}

trait __StructProjectionTrait<'_outer_pin, T, U> {
fn project<'_pin>(&'_pin mut self) -> __StructProjection<'_pin, T, U>;
fn project_into(self) -> __StructProjection<'_outer_pin, T, U>;
}

impl<'_outer_pin, T, U> __StructProjectionTrait<'_outer_pin, T, U>
for ::core::pin::Pin<&'_outer_pin mut Struct<T, U>>
{
Expand All @@ -56,6 +51,11 @@ impl<'_outer_pin, T, U> __StructProjectionTrait<'_outer_pin, T, U>
}
}

trait __StructProjectionTrait<'_outer_pin, T, U> {
fn project<'_pin>(&'_pin mut self) -> __StructProjection<'_pin, T, U>;
fn project_into(self) -> __StructProjection<'_outer_pin, T, U>;
}

// Automatically create the appropriate conditional `Unpin` implementation.
impl<T, U> Unpin for Struct<T, U> where T: Unpin {}

Expand Down
15 changes: 5 additions & 10 deletions pin-project-internal/src/pin_project/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,17 @@ pub(super) fn parse(cx: &mut Context, mut item: ItemEnum) -> Result<TokenStream>
};

let project_body = quote! {
unsafe {
match self.as_mut().get_unchecked_mut() {
#(#proj_arms,)*
}
match self.as_mut().get_unchecked_mut() {
#(#proj_arms,)*
}
};

let project_into_body = quote! {
unsafe {
match self.get_unchecked_mut() {
#(#proj_arms,)*
}
match self.get_unchecked_mut() {
#(#proj_arms,)*
}
};

proj_items.extend(cx.make_trait_impl(&project_body, &project_into_body));
proj_items.extend(cx.make_proj_impl(&project_body, &project_into_body));

let mut item = item.into_token_stream();
item.extend(proj_items);
Expand Down
80 changes: 38 additions & 42 deletions pin-project-internal/src/pin_project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Parse for Args {
return Err(error!(
arg,
"an invalid argument was passed to #[pin_project] attribute"
))
));
}
}

Expand Down Expand Up @@ -247,6 +247,8 @@ impl Context {

full_where_clause.predicates.push(unpin_clause);

let attrs = if cfg!(proc_macro_def_site) { quote!() } else { quote!(#[doc(hidden)]) };

let inner_data = quote! {
struct #always_unpin_ident <T: ?Sized> {
val: ::core::marker::PhantomData<T>
Expand All @@ -265,19 +267,9 @@ impl Context {
// steps to ensure that the user can never actually reference
// this 'public' type. These steps are described below.
//
/// A struct generated by pin-project to correctly document the
/// automatically generated `Unpin` implementation.
///
/// This struct exists to correctly document the actual trait
/// implementation bounds of the `Unpin` implementation of the
/// original type. Note that users cannot access this struct,
/// even if it is public.
///
/// See [taiki-e/pin-project#53] and [rust-lang/rust#63281] for more details.
///
/// [taiki-e/pin-project#53]: https://github.com/taiki-e/pin-project/pull/53#issuecomment-525906867
/// [rust-lang/rust#63281]: https://github.com/rust-lang/rust/issues/63281
// See also https://github.com/taiki-e/pin-project/pull/53.
#[allow(dead_code)]
#attrs
#vis struct #struct_ident #full_generics #where_clause {
__pin_project_use_generics: #always_unpin_ident <(#(#type_params),*)>,

Expand Down Expand Up @@ -373,7 +365,28 @@ impl Context {
}

/// Creates a definition of the projection trait.
fn make_trait_impl(
fn make_proj_trait(&self) -> TokenStream {
let Self { proj_ident, proj_trait, lifetime, .. } = self;
let proj_generics = self.proj_generics();
let proj_ty_generics = proj_generics.split_for_impl().1;

// Add trait lifetime to trait generics.
let mut trait_generics = self.generics.clone();
utils::proj_generics(&mut trait_generics, self.trait_lifetime.clone());

let (trait_generics, trait_ty_generics, orig_where_clause) =
trait_generics.split_for_impl();

quote! {
trait #proj_trait #trait_generics {
fn project<#lifetime>(&#lifetime mut self) -> #proj_ident #proj_ty_generics #orig_where_clause;
fn project_into(self) -> #proj_ident #trait_ty_generics #orig_where_clause;
}
}
}

/// Creates an implementation of the projection trait.
fn make_proj_impl(
&self,
project_body: &TokenStream,
project_into_body: &TokenStream,
Expand All @@ -392,35 +405,18 @@ impl Context {
for ::core::pin::Pin<&#trait_lifetime mut #orig_ident #ty_generics> #where_clause
{
fn project<#lifetime>(&#lifetime mut self) -> #proj_ident #proj_ty_generics #where_clause {
#project_body
unsafe {
#project_body
}
}
fn project_into(self) -> #proj_ident #project_into_ty_generics #where_clause {
#project_into_body
unsafe {
#project_into_body
}
}
}
}
}

/// Creates an implementation of the projection trait.
fn make_proj_trait(&self) -> TokenStream {
let Self { proj_ident, proj_trait, lifetime, .. } = self;
let proj_generics = self.proj_generics();
let proj_ty_generics = proj_generics.split_for_impl().1;

// Add trait lifetime to trait generics.
let mut trait_generics = self.generics.clone();
utils::proj_generics(&mut trait_generics, self.trait_lifetime.clone());

let (trait_generics, trait_ty_generics, orig_where_clause) =
trait_generics.split_for_impl();

quote! {
trait #proj_trait #trait_generics {
fn project<#lifetime>(&#lifetime mut self) -> #proj_ident #proj_ty_generics #orig_where_clause;
fn project_into(self) -> #proj_ident #trait_ty_generics #orig_where_clause;
}
}
}
}

fn parse(args: TokenStream, input: TokenStream) -> Result<TokenStream> {
Expand All @@ -430,10 +426,10 @@ fn parse(args: TokenStream, input: TokenStream) -> Result<TokenStream> {

let packed_check = ensure_not_packed(&item)?;
let mut res = structs::parse(&mut cx, item)?;
res.extend(packed_check);
res.extend(cx.make_drop_impl());
res.extend(cx.make_unpin_impl());
res.extend(cx.make_proj_trait());
res.extend(cx.make_unpin_impl());
res.extend(cx.make_drop_impl());
res.extend(packed_check);
Ok(res)
}
Item::Enum(item) => {
Expand All @@ -442,9 +438,9 @@ fn parse(args: TokenStream, input: TokenStream) -> Result<TokenStream> {
// We don't need to check for '#[repr(packed)]',
// since it does not apply to enums.
let mut res = enums::parse(&mut cx, item)?;
res.extend(cx.make_drop_impl());
res.extend(cx.make_unpin_impl());
res.extend(cx.make_proj_trait());
res.extend(cx.make_unpin_impl());
res.extend(cx.make_drop_impl());
Ok(res)
}
item => Err(error!(item, "#[pin_project] attribute may only be used on structs or enums")),
Expand Down
19 changes: 7 additions & 12 deletions pin-project-internal/src/pin_project/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ pub(super) fn parse(cx: &mut Context, mut item: ItemStruct) -> Result<TokenStrea
return Err(error!(
item.fields,
"#[pin_project] attribute may not be used on structs with zero fields"
))
));
}
Fields::Unit => {
return Err(error!(
item,
"#[pin_project] attribute may not be used on structs with units"
))
));
}

Fields::Named(fields) => named(cx, fields)?,
Expand All @@ -39,20 +39,15 @@ pub(super) fn parse(cx: &mut Context, mut item: ItemStruct) -> Result<TokenStrea
};

let project_body = quote! {
unsafe {
let this = self.as_mut().get_unchecked_mut();
#proj_ident #proj_init
}
let this = self.as_mut().get_unchecked_mut();
#proj_ident #proj_init
};

let project_into_body = quote! {
unsafe {
let this = self.get_unchecked_mut();
#proj_ident #proj_init
}
let this = self.get_unchecked_mut();
#proj_ident #proj_init
};

proj_items.extend(cx.make_trait_impl(&project_body, &project_into_body));
proj_items.extend(cx.make_proj_impl(&project_body, &project_into_body));

let mut item = item.into_token_stream();
item.extend(proj_items);
Expand Down

0 comments on commit 69f8014

Please sign in to comment.