-
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
Special lifetime for downcasting ownership to reference. #1802
Comments
The memory representation of I think you’re looking for In the second impl, use the |
Thanks! This does indeed solve the code duplication, so it might render this feature useless. I still think using a When On the other hand, when I will try to think of another example where this is more hindering. |
As stated, there is a conflict in your problem statement : Your I suspect you want a trait more like
At least the [https://doc.rust-lang.org/nomicon/lifetime-elision.html](lifetime elision) rules say the |
I checked, and it doesn't work, because that lifetime parameter is really required there in The thing is, that my original |
You'll notice this errors with
so the same applies to I have used types with lifetime parameters used as associated types once before. I cannot find my code example right now, but I think exploring the
|
I believe this is going to be addressed by Generic Associated Types in rust-lang/rust#44265. trait Summarize {
type Summary<'a>;
fn summarize(&'a self) -> Self::Summary<'a>;
}
struct MyType;
struct MySummary<'a> {
obj: &'a MyType,
}
impl Summarize for MyType {
type Summary<'a> = MySummary<'a>;
fn summarize(&'a self) -> MySummary<'a> {
MySummary { obj: self }
}
} For now the closest stable workaround would be: trait Summarize<'a> {
type Summary;
fn summarize(&'a self) -> Self::Summary;
}
struct MyType;
struct MySummary<'a> {
obj: &'a MyType,
}
impl<'a> Summarize<'a> for MyType {
type Summary = MySummary<'a>;
fn summarize(&'a self) -> MySummary<'a> {
MySummary { obj: self }
}
} or: trait Summarize {
type Summary;
fn summarize(self) -> Self::Summary;
}
struct MyType;
struct MySummary<'a> {
obj: &'a MyType,
}
impl<'a> Summarize for &'a MyType {
type Summary = MySummary<'a>;
fn summarize(self) -> MySummary<'a> {
MySummary { obj: self }
}
} |
Closing per notes in #1802 (comment). |
Suppose I have a trait:
Now, for my type, summary is non-mutative, but does reference the original. So I have a struct:
and the helper summary function I implemented uses a reference:
Now I implement the
Summarize
trait for references:But I also want to implement it for owned copies. Currently, I can only reimplement everything with a struct that owns the object, a method that returns such a struct, and then implementation of the trait. What I would like to be able to do is:
with the special lifetime
'owned
assuming the lifetime of the ownership, which is also the lifetime of the reference implicitly created by that lastself.do_summarize()
call. And similarly for downcasting from ownership to a&mut
.The text was updated successfully, but these errors were encountered: