Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement lifetime elision inside of pins #661

Merged
merged 2 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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