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

Decide on extension trait convention #187

Closed
aturon opened this issue Apr 24, 2019 · 13 comments
Closed

Decide on extension trait convention #187

aturon opened this issue Apr 24, 2019 · 13 comments
Labels
design Open design question

Comments

@aturon
Copy link
Collaborator

aturon commented Apr 24, 2019

Tide uses many "extension traits" (which extend an existing type with new methods, and have no other impls). We should standardize on a naming convention.

@aturon aturon added the design Open design question label Apr 24, 2019
@aturon
Copy link
Collaborator Author

aturon commented Apr 24, 2019

One possibility would be FooExt where Foo is the name of the type being extended -- including the type in the name helps the reader quickly see what is being extended.

A downside is that we want to provide many extensions to Context. We'd end up with cookies::ContextExt and forms::ContextExt etc. But given that you only use these trait names on import, where they will be qualified by module, perhaps this is fine? The paths do contain the full set of relevant info: what is the extension, and what is being extended?

@aturon aturon mentioned this issue Apr 24, 2019
8 tasks
@bIgBV
Copy link
Contributor

bIgBV commented Apr 24, 2019

I think using ContextExt would be a good idea. This would be the common type name across the entire ecosystem which would signal that this type extends something on tide's Context. I think the recognizability this achieves will be really useful to convey the idea of how it's supposed to be used. Overall it provides a consistent name for the core extension trait in a given crate across all such crates.

@aturon I agree with you, given that the type itself will only be present at the top of the file for the most part and the type will not be used anywhere else, the import path will give enough context as to where this is coming from.

@secretfader
Copy link

secretfader commented Apr 24, 2019

Edit: see updated comment below.

secretfader pushed a commit to secretfader/tide that referenced this issue Apr 25, 2019
Match new specification in http-rs#187
@fairingrey
Copy link
Contributor

fairingrey commented May 3, 2019

I'm in support of using the *Ext naming scheme -- it's what I see a lot in the ecosystem already and feels the most familiar (namely in the futures crate).

EDIT: updated comment

@secretfader
Copy link

secretfader commented May 3, 2019

@fairingrey Ignore my comment above. I completely misunderstood the convention. A more appropriate naming scheme for the extension provided by Tide's querystring module is tide::querystring::ContextExt.

@fairingrey
Copy link
Contributor

Oh, sorry! Yeah, that makes sense.

secretfader pushed a commit to secretfader/tide that referenced this issue May 13, 2019
As discussed in http-rs#187, context extension traits should have an import
path that explains their functionality, while being named after the
traits they extend.

Signed-off-by: Nicholas Young <nyoung@uptime.ventures>
secretfader pushed a commit to secretfader/tide that referenced this issue May 13, 2019
As discussed in http-rs#187, context extension traits should have an import
path that explains their functionality, while being named after the
traits they extend.

Signed-off-by: Nicholas Young <nyoung@uptime.ventures>
secretfader pushed a commit to secretfader/tide that referenced this issue May 13, 2019
As discussed in http-rs#187, context extension traits should have an import
path that explains their functionality, while being named after the
traits they extend.

Signed-off-by: Nicholas Young <nyoung@uptime.ventures>
yoshuawuyts pushed a commit that referenced this issue May 20, 2019
@Nemo157
Copy link
Contributor

Nemo157 commented May 23, 2019

A downside of the chosen convention I recently ran into is not being able to actually bring multiple ContextExt into scope. A workaround is to use the relatively recent use foo::Foo as _; feature to import the traits methods into scope without having the trait nameable:

use tide::{cookies::ContextExt as _, forms::ContextExt as _};

This can also be mitigated in Tide for prelude users, if Tide has

pub mod prelude {
    use crate::{cookies::ContextExt as _, forms::ContextExt as _};
}

then a user adding use tide::prelude::*; will still get the effect of importing the methods without the traits.

@bIgBV
Copy link
Contributor

bIgBV commented May 23, 2019

Having a prelude is certainly useful, and I think we eventually should have one for the most common extension traits, but not all of them would be a part of the prelude right.

So we might have to rethink the naming scheme itself.

@Nemo157
Copy link
Contributor

Nemo157 commented May 23, 2019

Personally I don't mind the as _, and am anti-preludes, so I'm happy to keep using that. (Although I've also had to use it in other places to force importing crates when I don't use any of their items before, so I may be pre-biased).

@bIgBV
Copy link
Contributor

bIgBV commented May 24, 2019

But don't you think as the ecosystem grows, the number of such imports would be an annoyance to type always.

Though to be fair, most would only be typed once and then forgotten.

@yoshuawuyts
Copy link
Member

A few data points:

Given the prior discussion on this thread also, I feel we should go with the schema laid out in RFC-0445, and keep the as _ use in mind if we ever decide to define a prelude.

@Nemo157
Copy link
Contributor

Nemo157 commented Jun 6, 2019

One thing to keep in mind is that examples should use as _ as well if that's how users are expected to use them (relevant to async-rs/futures-timer#18 as that is currently shadowing futures::prelude::TryFutureExt in the example, which could really confuse users as to why their method calls suddenly stop working when introducing futures-timer (EDIT: I'll make sure futures::prelude is fixed so that its extension traits are unshadowable, but I still think it's best to have as _ in all examples)).

@yoshuawuyts
Copy link
Member

The discussion here has more or less quieted down. The futures crate is now also shadowing extension traits. It feels like we've reached consensus, and it's okay to close this now (:

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

No branches or pull requests

6 participants