-
Notifications
You must be signed in to change notification settings - Fork 3
Rust's suggested API guidelines for conversion method naming #4
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
Comments
I think that As far as the |
Also, just a related thought, if we're going to be doing things by naming convention, would it make sense to update the derive names to match? |
Naming them that way sounds like a great idea. |
Well, if you want to add this, go for it. Otherwise, I'll probably add it this weekend. |
Not sure I'll be able to get to it this weekend so it's all yours! I was thinking - maybe it's best to not have the |
I'm fine with leaving |
A conflict occurs: I see a few options:
After typing this out, I'm leaning towards the second option. 1see rust-lang/rust#25893 for more details regarding "known" copy types |
Well, into methods should be moves, so copy shouldn't matter there right? as methods shouldn't have to worry about copy/clone either if it returns an `Option<&T>`. Because the user can get a reference to the inner data, and if it so happens it is copy or clone, then they can take advantage of that naturally.
IE `*foobar.as_foo().unwrap()` should copy automatically if you try and move it and it is a copy type and similarly `foobar.as_foo().unwrap().clone()` should work if the inner data happens to be clone?
…On Jul 22, 2017, 12:14 PM, at 12:14 PM, Alek Ratzloff ***@***.***> wrote:
A conflict occurs: `EnumGetters` currently implements special getters
for known<sup>1</sup> `Copy` types, which will just copy the type.
I see a few options:
* Keep `EnumGetters` as it is and add `EnumAsGetters` and
`EnumIntoGetters`
* Remove `EnumGetters` and remove copying behavior completely
* Remove `EnumGetters` and transfer copying behavior to `EnumAsGetters`
(i.e. `as_foo()` where foo is a known copy type would return
`Option<T>` instead of `Option<&T>`).
After typing this out, I'm leaning towards the second option.
<sup>1</sup>see rust-lang/rust#25893 for more details regarding "known"
copy types
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#4 (comment)
|
That said, I'm not sure how structure/tuple variants fit into the picture since they are sort of anonymous
…On Jul 22, 2017, 12:14 PM, at 12:14 PM, Alek Ratzloff ***@***.***> wrote:
A conflict occurs: `EnumGetters` currently implements special getters
for known<sup>1</sup> `Copy` types, which will just copy the type.
I see a few options:
* Keep `EnumGetters` as it is and add `EnumAsGetters` and
`EnumIntoGetters`
* Remove `EnumGetters` and remove copying behavior completely
* Remove `EnumGetters` and transfer copying behavior to `EnumAsGetters`
(i.e. `as_foo()` where foo is a known copy type would return
`Option<T>` instead of `Option<&T>`).
After typing this out, I'm leaning towards the second option.
<sup>1</sup>see rust-lang/rust#25893 for more details regarding "known"
copy types
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#4 (comment)
|
Yeah, that all makes sense, the more I think about it. The user can just copy/clone how they want, and we don't need to provide any shortcuts. Works for me 👍 Also, I'm starting to think that Does that make sense? What do you think? |
Yeah, I guess that's fair. And as far as tuple variants, I think it's not unreasonable to return a tuple enum Foo {
Bar(bool, u32, f32),
}
fn into_bar(self) -> (bool, u32, f32);
fn as_bar(&self) -> &(bool, u32, f32);
// Or this one, but I think this may be equivalent to the one above
fn as_bar(&self) -> (&bool, &u32, &f32); Struct variants seem like the most ambiguous, maybe it's best to not support enums with them altogether or to just not provide methods for those variants? At the very, least adding support for them in the future if someone thinks of a good way to do so, would be backwards compatible since you're just adding new methods. |
Uh oh!
There was an error while loading. Please reload this page.
Although not required, rust's API best practice guideline suggests conversion guidelines for
as_*
,to_*
, andinto_*
methods.So, I think this would look like:
Not sure about to, maybe that's just taking a borrowed
Clone
type and cloning it? And copying aCopy
? Basically like into but taking a&self
?But looking at the
str::to_owned() -> String
maybe it should instead beFooBar.to_foo_bar_enum
?What do you think?
The text was updated successfully, but these errors were encountered: