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

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

Open
uzytkownik opened this issue Sep 30, 2017 · 2 comments
Open
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@uzytkownik
Copy link

uzytkownik commented Sep 30, 2017

&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)

@retep998
Copy link
Member

The problem with Borrow enforcing the return type to be a &T is similar to the situation with Index where it is impossible to return any sort of actual value which isn't a &T.

@TimNN TimNN added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Oct 1, 2017
@bluss
Copy link
Member

bluss commented Oct 2, 2017

There's sort of a design for an extension of the "Borrow" mechanism for hash tables here: indexmap-rs/indexmap#10 (not in any packaged version yet). It's backwards compatible modulo type inference regressions.. :-/

The new trait allows this kind of key equivalence of Foo and FooBuf.

@Enselic Enselic added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants