Skip to content

It's often not possible to implement ToOwned for user composite types #44950

Open
@uzytkownik

Description

@uzytkownik

&str and &[u8] seems to work by internal magic but it is hard to implement ToOwned for user composite types. Consider:

struct Foo<'a> {
    foo: &'a str,
    bar: &'a str,
    foobar: &'a str
}

struct FooBuf {
    foo: String,
    bar: String,
    foobar: String
}

It is not possible to mark that FooBuf is an owned version of Foo as it would require implementing Borrow for FooBuf. However it is not possible to implement fn borrow(&FooBuf) -> &Foo as we don't have anywhere we can store Foo. Built-in types workaround it by handling &str and similar types separately from &T and packing the actual structure there.

(I know too little about type system to create any RFR which would fix it)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions