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

impl-only-use #2166

Merged
merged 4 commits into from Feb 14, 2018

Conversation

Projects
None yet
@phaazon
Copy link
Contributor

commented Oct 1, 2017

This RFC gives Rust the syntax:

use foo::FooTrait as _;

Here, _ means that we want to import the FooTrait trait’s impls but
not the trait symbol directly, because we might declare the same symbol
in the module issuing the use.


Rendered

@phaazon

This comment has been minimized.

Copy link
Contributor Author

commented Oct 1, 2017

Just as a reference, Haskell has that:

import Foo.Bar.Zoo () -- imports the instances (impls) but not the typeclasses (traits) directly
@burdges

This comment has been minimized.

Copy link

commented Oct 1, 2017

We'd want this for inherent methods too, not just traits, right?

@sfackler

This comment has been minimized.

Copy link
Member

commented Oct 1, 2017

@burdges inherent methods don't need to be imported to be used.

@phaazon

This comment has been minimized.

Copy link
Contributor Author

commented Oct 1, 2017

No need for inherent methods because you never express them in the use position. :)

# Guide-level explanation
[guide-level-explanation]: #guide-level-explanation

Qualyfing a `use` with `_` on a trait imports the trait’s `impl`s but not the symbol directly. It’s

This comment has been minimized.

Copy link
@sfackler

sfackler Oct 1, 2017

Member

"Qualifying"

This comment has been minimized.

Copy link
@phaazon

phaazon Oct 1, 2017

Author Contributor

Nice catch, thanks!

impl-only-use
This RFC gives Rust the syntax:

```rust
use foo::FooTrait as _;
```

Here, `_` means that we want to import the `FooTrait` trait’s impls but
not the trait symbol directly, because we might declare the same symbol
in the moduling issuing the `use`.

@phaazon phaazon force-pushed the phaazon:impl-only-use branch from 0d69fad to 17a50ea Oct 1, 2017

@Centril

This comment has been minimized.

Copy link
Member

commented Oct 2, 2017

Neat RFC.

Also related to traits, but perhaps deserving of a separate RFC... How about importing trait methods of the following form as free functions:

trait TheTrait {
    fn notUsingInArgPos2(x: T1, y: T2, ...) -> X<TheTrait>;
}

where X can be something like Result<T, ?>

@egilburg

This comment has been minimized.

Copy link

commented Oct 2, 2017

Hm, isn't conventionally _ used to describe a concrete thing you just don't care about, rather than a template wildcard?

@phaazon

This comment has been minimized.

Copy link
Contributor Author

commented Oct 2, 2017

Yes, and here it’s the same: a concrete thing (a trait) you don’t care about. :)

# Reference-level explanation
[reference-level-explanation]: #reference-level-explanation

To be defined.

This comment has been minimized.

Copy link
@petrochenkov

petrochenkov Oct 3, 2017

Contributor

use Trait as _ needs to desugar into use Trait as SomeUniqueNameYouCantReferTo (i.e. SomeUniqueNameYouCantReferTo is a "gensym").
With this scheme glob imports/reexports can work properly with such items, i.e. import/reexport them.

mod m {
    pub use Trait as _;

    // `Trait` is in scope
}
use m::*;

// `Trait` is in scope too 

This comment has been minimized.

Copy link
@petrochenkov

petrochenkov Oct 3, 2017

Contributor

use NonTrait as _ can work the same way, it will just always be reported as unused import.

extern crate my_crate as _ can work in the same way too (this can be useful for linking-only crates).

This comment has been minimized.

Copy link
@phaazon

phaazon Oct 3, 2017

Author Contributor

Good comments, I’ll have them added! :)

@vitiral

This comment has been minimized.

Copy link

commented Oct 3, 2017

Oh please this. I could use this in the prelude crate so I don't have to alias io::Write vs fmt::Write.

@phaazon phaazon force-pushed the phaazon:impl-only-use branch from 117cac3 to 4727667 Oct 4, 2017

@phaazon phaazon force-pushed the phaazon:impl-only-use branch from 4727667 to 459606f Oct 4, 2017

@scottmcm scottmcm added the T-lang label Oct 12, 2017

@ordovicia ordovicia referenced this pull request Oct 24, 2017

Closed

use some::Trait as _; #1311

@arcnmx

This comment has been minimized.

Copy link

commented Oct 24, 2017

🎉 Every couple months I accidentally do this hoping maybe it got implemented.

A small point brought up previously is that this additionally affects rustdoc, and probably requires some special handling/formatting to display without just showing the generated ident.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

commented Jan 25, 2018

Nominating for discussion in the @rust-lang/lang team meeting.

@withoutboats

This comment has been minimized.

Copy link
Contributor

commented Jan 25, 2018

@rfcbot fcp merge

@rfcbot

This comment has been minimized.

Copy link

commented Jan 25, 2018

Team member @withoutboats has proposed to merge this. The next step is review by the rest of the tagged teams:

No concerns currently listed.

Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot

This comment has been minimized.

Copy link

commented Jan 28, 2018

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot

This comment has been minimized.

Copy link

commented Feb 7, 2018

The final comment period is now complete.

@aturon aturon removed the I-nominated label Feb 8, 2018

@aturon aturon referenced this pull request Feb 14, 2018

Closed

Tracking issue for RFC #2166: impl-only-use #48216

3 of 3 tasks complete
@aturon

This comment has been minimized.

Copy link
Member

commented Feb 14, 2018

This RFC has been merged!

Tracking issue

@aturon aturon merged commit 51ee8c1 into rust-lang:master Feb 14, 2018

@phaazon phaazon deleted the phaazon:impl-only-use branch Feb 25, 2018

@dtolnay dtolnay referenced this pull request Sep 2, 2018

Closed

Release 0.15 #476

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.