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

Borrow, AsRef, others suboptimal for enums / specific structs #39125

Closed
sdroege opened this issue Jan 17, 2017 · 2 comments
Closed

Borrow, AsRef, others suboptimal for enums / specific structs #39125

sdroege opened this issue Jan 17, 2017 · 2 comments

Comments

@sdroege
Copy link
Contributor

sdroege commented Jan 17, 2017

Consider something like the following two enums

enum Foo {
  Bar(i32),
  Baz(String),
}

enum FooBorrowed<'a> {
  Bar(&'a i32),
  Baz(&'a str),
}

In theory one would like to be able to implement Deref, AsRef, Borrow, IntoOwned, etc for this pair of enums (or a similar pair of structs) and e.g. also use it in a Cow. However that's not possible as Deref, AsRef, Borrow explicitly return a reference from their functions. If it was instead a "normal" type, one could implement it for the above pair and still cover the existing implementations that return references.

Is there any chance to extend this by new traits, or is there another solution?

@Mark-Simulacrum
Copy link
Member

We can't change these traits in any way today, so I'm going to close this. I'm not entirely sure what the problem here is either.

@ratijas
Copy link
Contributor

ratijas commented Mar 28, 2020

The problem still exists. I'm trapped in the same situation: having two enums, one for owned data, other for borrowed counterparts. And I can't seem to be able to benefit from the AsRef / Borrow infrastructure with those. Any ideas or workarounds?

This is my particular usecase:

/// Owned wrapper for counter value.
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum CounterValue {
    Dword(DWORD),
    Large(ULONGLONG),
    TextUnicode(U16CString),
    TextAscii(String),
    Zero,
}

/// Borrowed wrapper for counter value.
/// It is to the `CounterValue` as a str is to the String.
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum CounterVal<'a> {
    Dword(DWORD),
    Large(ULONGLONG),
    TextUnicode(&'a U16CStr),
    TextAscii(&'a str),
    Zero,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants