Skip to content

Special lifetime for downcasting ownership to reference. #1802

Closed
@amosonn

Description

@amosonn

Suppose I have a trait:

pub trait Summarize {
    type Summary;
    fn summarize(self) -> Self::Summary;
}

Now, for my type, summary is non-mutative, but does reference the original. So I have a struct:

struct MySummary<'a> {
    obj: &'a MyType,
    /* and other things */
}

and the helper summary function I implemented uses a reference:

impl MyType {
    fn do_summarize<'a>(&'a self) -> MySummary<'a> { /* do it */ }
}

Now I implement the Summarize trait for references:

impl<'a> Summarize for &'a MyType {
    type Summary = MySummary<'a>;
    fn summarize(self) -> MySummary<'a> { self.do_summarize() };
}

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:

impl Summarize for MyType {
    type Summary = MySummary<'owned>;
    fn summarize(self) -> MySummary<'owned> { self.do_summarize() };
}

with the special lifetime 'owned assuming the lifetime of the ownership, which is also the lifetime of the reference implicitly created by that last self.do_summarize() call. And similarly for downcasting from ownership to a &mut.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions