Skip to content

Commit

Permalink
Merge pull request #661 from dtolnay/elision
Browse files Browse the repository at this point in the history
Implement lifetime elision inside of pins
  • Loading branch information
dtolnay authored Jan 4, 2021
2 parents a2bd047 + 9bd65aa commit efb4506
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ fn expand_cxx_function_shim(efn: &ExternFn, types: &Types) -> TokenStream {
let receiver = efn.receiver.iter().map(|receiver| {
let var = receiver.var;
if receiver.pinned {
let ty = receiver.ty();
let ty = receiver.ty_self();
quote!(#var: #ty)
} else {
let ampersand = receiver.ampersand;
Expand Down
33 changes: 33 additions & 0 deletions syntax/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,18 @@ impl ToTokens for NamedType {
}

pub struct ReceiverType<'a>(&'a Receiver);
pub struct ReceiverTypeSelf<'a>(&'a Receiver);

impl Receiver {
// &TheType
pub fn ty(&self) -> ReceiverType {
ReceiverType(self)
}

// &Self
pub fn ty_self(&self) -> ReceiverTypeSelf {
ReceiverTypeSelf(self)
}
}

impl ToTokens for ReceiverType<'_> {
Expand Down Expand Up @@ -304,3 +310,30 @@ impl ToTokens for ReceiverType<'_> {
}
}
}

impl ToTokens for ReceiverTypeSelf<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let Receiver {
pinned: _,
ampersand,
lifetime,
mutable: _,
var: _,
ty,
shorthand: _,
pin_tokens,
mutability,
} = &self.0;
if let Some((pin, langle, _rangle)) = pin_tokens {
tokens.extend(quote_spanned!(pin.span=> ::std::pin::Pin));
langle.to_tokens(tokens);
}
ampersand.to_tokens(tokens);
lifetime.to_tokens(tokens);
mutability.to_tokens(tokens);
Token![Self](ty.rust.span()).to_tokens(tokens);
if let Some((_pin, _langle, rangle)) = pin_tokens {
rangle.to_tokens(tokens);
}
}
}
1 change: 1 addition & 0 deletions tests/ffi/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ pub mod ffi {
fn c_return_borrow(s: &CxxString) -> UniquePtr<Borrow>;

fn const_member(self: &Borrow);
fn nonconst_member(self: Pin<&mut Borrow>);
}

#[repr(u32)]
Expand Down
2 changes: 2 additions & 0 deletions tests/ffi/tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ Borrow::Borrow(const std::string &s) : s(s) {}

void Borrow::const_member() const {}

void Borrow::nonconst_member() {}

std::unique_ptr<Borrow> c_return_borrow(const std::string &s) {
return std::unique_ptr<Borrow>(new Borrow(s));
}
Expand Down
1 change: 1 addition & 0 deletions tests/ffi/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ enum COwnedEnum {
struct Borrow {
Borrow(const std::string &s);
void const_member() const;
void nonconst_member();
const std::string &s;
};

Expand Down

0 comments on commit efb4506

Please sign in to comment.