-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove pin_project! function-like macro and add #[pinned_drop] attribute
- Loading branch information
Showing
33 changed files
with
679 additions
and
586 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
use proc_macro2::{Ident, Span, TokenStream}; | ||
use quote::{quote, quote_spanned}; | ||
use syn::{ | ||
parse::{Parse, ParseStream}, | ||
Attribute, Generics, Item, Meta, NestedMeta, Result, Type, | ||
}; | ||
|
||
use crate::utils::{crate_path, Nothing}; | ||
|
||
mod enums; | ||
mod structs; | ||
|
||
/// The annotation for pinned type. | ||
const PIN: &str = "pin"; | ||
|
||
pub(super) fn attribute(args: TokenStream, input: TokenStream) -> TokenStream { | ||
parse(args, input).unwrap_or_else(|e| e.to_compile_error()) | ||
} | ||
|
||
#[derive(Clone, Copy)] | ||
struct Args { | ||
pinned_drop: Option<Span>, | ||
unsafe_unpin: Option<Span>, | ||
} | ||
|
||
impl Args { | ||
fn impl_drop(self, generics: &Generics) -> ImplDrop<'_> { | ||
ImplDrop::new(generics, self.pinned_drop) | ||
} | ||
|
||
fn impl_unpin(self, generics: &Generics) -> ImplUnpin { | ||
ImplUnpin::new(generics, self.unsafe_unpin) | ||
} | ||
} | ||
|
||
impl Parse for Args { | ||
fn parse(input: ParseStream<'_>) -> Result<Self> { | ||
let mut pinned_drop = None; | ||
let mut unsafe_unpin = None; | ||
while !input.is_empty() { | ||
let i = input.parse::<Ident>()?; | ||
match &*i.to_string() { | ||
"PinnedDrop" => pinned_drop = Some(i.span()), | ||
"unsafe_Unpin" => unsafe_unpin = Some(i.span()), | ||
_ => return Err(error!(i, "an invalid argument was passed")), | ||
} | ||
} | ||
Ok(Self { pinned_drop, unsafe_unpin }) | ||
} | ||
} | ||
|
||
fn parse(args: TokenStream, input: TokenStream) -> Result<TokenStream> { | ||
let args = syn::parse2(args)?; | ||
match syn::parse2(input)? { | ||
Item::Struct(item) => { | ||
ensure_not_packed(&item.attrs)?; | ||
structs::parse(args, item) | ||
} | ||
Item::Enum(item) => { | ||
ensure_not_packed(&item.attrs)?; | ||
enums::parse(args, item) | ||
} | ||
item => Err(error!(item, "may only be used on structs or enums")), | ||
} | ||
} | ||
|
||
fn ensure_not_packed(attrs: &[Attribute]) -> Result<()> { | ||
for meta in attrs.iter().filter_map(|attr| attr.parse_meta().ok()) { | ||
if let Meta::List(l) = meta { | ||
if l.ident == "repr" { | ||
for repr in l.nested.iter() { | ||
if let NestedMeta::Meta(Meta::Word(w)) = repr { | ||
if w == "packed" { | ||
return Err(error!( | ||
w, | ||
"pin_project may not be used on #[repr(packed)] types" | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Makes the generics of projected type from the reference of the original generics. | ||
fn proj_generics(generics: &Generics) -> Generics { | ||
let mut generics = generics.clone(); | ||
generics.params.insert(0, syn::parse_quote!('__a)); | ||
generics | ||
} | ||
|
||
// ================================================================================================= | ||
// Drop implementation | ||
|
||
struct ImplDrop<'a> { | ||
generics: &'a Generics, | ||
pinned_drop: Option<Span>, | ||
} | ||
|
||
impl<'a> ImplDrop<'a> { | ||
fn new(generics: &'a Generics, pinned_drop: Option<Span>) -> Self { | ||
Self { generics, pinned_drop } | ||
} | ||
|
||
fn build(self, ident: &Ident) -> TokenStream { | ||
let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl(); | ||
|
||
if let Some(pinned_drop) = self.pinned_drop { | ||
let crate_path = crate_path(); | ||
let call = quote_spanned! { pinned_drop => | ||
::#crate_path::__private::UnsafePinnedDrop::pinned_drop(pinned_self) | ||
}; | ||
|
||
quote! { | ||
impl #impl_generics ::core::ops::Drop for #ident #ty_generics #where_clause { | ||
fn drop(&mut self) { | ||
// Safety - we're in 'drop', so we know that 'self' will | ||
// never move again | ||
let pinned_self = unsafe { ::core::pin::Pin::new_unchecked(self) }; | ||
// We call `pinned_drop` only once. Since `UnsafePinnedDrop::pinned_drop` | ||
// is an unsafe function and a private API, it is never called again in safe | ||
// code *unless the user uses a maliciously crafted macro*. | ||
unsafe { | ||
#call; | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
quote! { | ||
impl #impl_generics ::core::ops::Drop for #ident #ty_generics #where_clause { | ||
fn drop(&mut self) { | ||
// Do nothing. The precense of this Drop | ||
// impl ensures that the user can't provide one of their own | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// ================================================================================================= | ||
// conditional Unpin implementation | ||
|
||
struct ImplUnpin { | ||
generics: Generics, | ||
unsafe_unpin: bool, | ||
} | ||
|
||
impl ImplUnpin { | ||
fn new(generics: &Generics, unsafe_unpin: Option<Span>) -> Self { | ||
let mut generics = generics.clone(); | ||
if let Some(unsafe_unpin) = unsafe_unpin { | ||
let crate_path = crate_path(); | ||
generics.make_where_clause().predicates.push( | ||
syn::parse2(quote_spanned! { unsafe_unpin => | ||
::#crate_path::__private::Wrapper<Self>: ::#crate_path::__private::UnsafeUnpin | ||
}) | ||
.unwrap(), | ||
); | ||
} | ||
|
||
Self { generics, unsafe_unpin: unsafe_unpin.is_some() } | ||
} | ||
|
||
fn push(&mut self, ty: &Type) { | ||
// We only add bounds for automatically generated impls | ||
if !self.unsafe_unpin { | ||
self.generics | ||
.make_where_clause() | ||
.predicates | ||
.push(syn::parse_quote!(#ty: ::core::marker::Unpin)); | ||
} | ||
} | ||
|
||
/// Creates `Unpin` implementation. | ||
fn build(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 {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.