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

Support self: Rc #21

Closed
audunhalland opened this issue Jan 23, 2020 · 10 comments
Closed

Support self: Rc #21

audunhalland opened this issue Jan 23, 2020 · 10 comments

Comments

@audunhalland
Copy link

Thanks for this really cool library!

I can't get this to work:

#[faux::methods]
impl Struct {
    pub fn foo(self: Rc<Self>) {
       ...
    }
}
@nrxus
Copy link
Owner

nrxus commented Jan 23, 2020

arbitrary self types are not supported yet unfortunately.

Since I have personally never used that feature I did not give it much of a priority but I now see I was mistaken. I will definitely take a look!

@audunhalland
Copy link
Author

I sometimes use this pattern with async methods. Rc or Arc.

@nrxus
Copy link
Owner

nrxus commented Jan 23, 2020

Unfortunately It seems like supporting arbitrary self types is actually pretty hard without any guarantees of soundness. I will keep this open in case I get ideas but until then only self, &self, and &mut self methods unfortunately.

@audunhalland
Copy link
Author

audunhalland commented Jan 24, 2020

Interesting read. So the "mock"/"parent" owns the real object by normal ownership now, so you can do & and &mut. What about: If you see a method taking Rc<Self> - then own the real data by an Rc. If you see a method taking Arc<Self> - own the real data through an Arc. If you see both, then no way to recover. If you see any of those + a &mut method, probably also no way to recover. Not sure whether this is feasible, but in my mind impls taking arbitrary self types will typically do so on all methods, and at least not mix/match with &mut self.

@nrxus
Copy link
Owner

nrxus commented Jan 24, 2020

Hm... if they are all Rc<Self> or Arc<Self> then it could be do-able. It would have to be a manual configuration in the #[faux::create] attribute, something like #[faux::create(receiver_type = Rc)] since it is not #[faux::methods]'s job to morph the struct, that's that #[faux::create] does. That is a pretty do-able stop gap for now as an MVP although not very ergonomic.

If we had a module level attribute this would be easier since it could scan the entire module and check all the receiver types but oh well.

Thanks for the idea! I'll mull it over some and see how I feel about it.

@audunhalland
Copy link
Author

Cool, it just might work. Would it still work with non-mutable &self?

@nrxus
Copy link
Owner

nrxus commented Jan 24, 2020

I believe so, since Rc<Self> will deref to &self and according to the docs:

T implicitly implements all the (immutable) methods of the type U.

Where T is Rc<Self> and U is &self

@nrxus
Copy link
Owner

nrxus commented Jan 27, 2020

addressed in: e3545ef

Not closing until the next version it's released but feel free to take a look at the test for what will be possible.

@audunhalland
Copy link
Author

Looks like it'll work very well, I'm more than happy with this 😃 Thank you!

@nrxus
Copy link
Owner

nrxus commented Feb 12, 2020

v0.0.4 is released with this feature 🎉

@nrxus nrxus closed this as completed Feb 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants