Skip to content
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
47 changes: 37 additions & 10 deletions faux_macros/src/methods/morphed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,11 @@ impl<'a> Signature<'a> {
if self.is_async {
proxy_real.extend(quote! { .await })
}
if let Some(wrapped_self) = self.wrap_self(morphed_ty, real_self, &proxy_real)? {
proxy_real = wrapped_self;

if let Some(output) = self.output {
if let Some(wrapped_self) = Self::wrap_self(output, morphed_ty, real_self, &proxy_real)? {
proxy_real = wrapped_self;
}
}

let ret = match &self.method_data {
Expand Down Expand Up @@ -255,26 +258,24 @@ impl<'a> Signature<'a> {
}

fn wrap_self(
&self,
ty: &Type,
morphed_ty: &syn::TypePath,
real_self: SelfType,
block: &TokenStream,
) -> darling::Result<Option<TokenStream>> {
// TODO: use let-else once we bump MSRV to 1.65.0
let output = match self.output {
Some(output) => output,
None => return Ok(None),
};
if !contains_self(output, morphed_ty) {
if !contains_self(ty, morphed_ty) {
return Ok(None);
}

let is_self = |ty: &syn::TypePath| {
ty == morphed_ty || (ty.qself.is_none() && ty.path.is_ident("Self"))
};

let output = match output {
let output = match ty {
syn::Type::Path(output) => output,
syn::Type::Tuple(tuple) => {
return Self::wrap_self_tuple(block, tuple, morphed_ty, real_self);
},
output => return Err(unhandled_self_return(output)),
};

Expand Down Expand Up @@ -341,6 +342,32 @@ impl<'a> Signature<'a> {

Ok(Some(wrapped))
}

fn wrap_self_tuple(
block: &TokenStream,
tuple: &syn::TypeTuple,
morphed_ty: &syn::TypePath,
real_self: SelfType) -> darling::Result<Option<TokenStream>> {
let elements = tuple.elems
.iter()
.enumerate()
.map(|e| {
let index = syn::Index::from(e.0);
let ty = e.1;

let tuple_index = quote! { tuple.#index };
let wrapped = Self::wrap_self(ty, morphed_ty, real_self, &tuple_index)?;

Ok(wrapped.unwrap_or(tuple_index))
})
.collect::<darling::Result<Vec<TokenStream>>>()?;

Ok(Some(quote! {{
let tuple = #block;

(# ( #elements),*)
}}))
}
}

impl MethodData<'_> {
Expand Down
38 changes: 38 additions & 0 deletions tests/return_self_tuple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::sync::Arc;
use std::rc::Rc;
use std::result::Result;
use std::error::Error;

#[faux::create]
pub struct Foo;

#[faux::methods]
impl Foo {
pub fn self_self() -> (Self, Self) {
(Foo, Foo)
}

pub fn self_path() -> (Self, i32) {
(Foo, 1)
}

pub fn self_box() -> (Self, Box<Self>) {
(Foo, Box::new(Foo))
}

pub fn self_rc() -> (Self, Rc<Self>) {
(Foo, Rc::new(Foo))
}

pub fn self_arc() -> (Self, Arc<Self>) {
(Foo, Arc::new(Foo))
}

pub fn self_result() -> (Self, Result<Self, Box<dyn Error>>) {
(Foo, Ok(Foo))
}

pub fn self_option() -> (Self, Option<Self>) {
(Foo, Some(Foo))
}
}