-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Some way to simulate &mut
reborrows in user code
#1403
Comments
In one of the designs for rust-cpython that I explored; I ran into a similar issue: In my case, I ended up going with a different design where the struct could be |
+1. Commenting to track. |
We could handle this similarly to unsizing coercions - see So if we generalized, And Should it work on multiple fields, including mixed coercions, e.g. one unsizing and one reborrowing, in different fields? FWIW, we currently kind of support reborrowing combined with unsizing due to the way the |
@eddyb In my case, my type has multiple fields. If I understand your comment, then implementing |
@carllerche implementing Personally, I think |
Yes, please. In one piece of code that really should have used |
cc me |
This is sort of possible today. Consider the following (messy, off-the-top-of-my-head, borderline embarrassing) associated-type-encoded-HKTs-based formulation: pub trait Reborrowable<'a> {
type Result;
}
pub trait ReborrowMut<'self_>: for<'a> Reborrowable<'a> + Reborrowable<'self_, Result=Self> {
fn reborrow<'reborrow>(&'reborrow mut self)
-> <Self as Reborrowable<'reborrow>>::Result
where 'self_: 'reborrow;
} This can be used as so: https://gist.github.com/anonymous/4aa1bfddaf5efdb53b15 (sans the type-identity trait bound to ensure I'm not saying that this is a solution - far from it. Still, it's possible to within a single function call on a trait (which is just about what the |
This is part of what I was getting at in http://dwrensha.github.io/capnproto-rust/2014/12/27/custom-mutable-references.html |
Ran into a need for this today. |
Edit: better example |
This would also be super helpful for |
Ran into it too. It's really annoying since the workarounds are very verbose and hard to understand. |
I've also run into it. Is there a reason why a simple |
Not to bikeshed, but what about (an incredibly verbose) |
@newpavlov I would honestly recommend just using a reborrow method for now, like this one: impl<'a, DS: DrawSharedImpl> DrawIface<'a, DS> {
/// Reborrow with a new lifetime
pub fn re<'b>(&'b mut self) -> DrawIface<'b, DS>
where
'a: 'b,
{
DrawIface {
draw: &mut *self.draw,
shared: &mut *self.shared,
pass: self.pass,
}
}
} Having an official trait for this and having it used automatically might be nice but isn't a big deal. BTW we ran into it years ago with the Rand project (something to do with passing @ShadowJonathan I don't see the point of special syntax. As far as I'm concerned the point is to make the usual syntax for reborrows work for "reference wrappers" so that users don't have to think about it. |
This is custom DSTs. I believe there’s a lot more language design involved than "just allow Please let’s keep this issue about re-borrowing of existing types like |
@carllerche wanted a way to make a newtyped
&mut
that would reborrow in the same implicit way that reborrows do today. That is, with an&mut
,foo(ptr)
is roughly equivalent tofoo(&mut *ptr)
, but ifptr
isMyRef<'a>(&'a mut ...)
, then this will not coerce fromMyRef<'a>
toMyRef<'b>
(where'a:'b
).The text was updated successfully, but these errors were encountered: