You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow importing methods from traits and then using them like regular functions.
9
+
Allow importing associated functions from traits and then using them like regular functions.
10
10
11
11
# Motivation
12
12
[motivation]: #motivation
13
13
14
-
There has for a long time been a desire to shorten the duplication needed to access certain methods, such as `Default::default`. Codebases like [Bevy](https://github.com/bevyengine/bevy/blob/7c7d1e8a6442a4258896b6c605beb1bf50399396/crates/bevy_utils/src/default.rs#L27) provide wrapper methods to shorten this call, and a previous, now-rejected, [RFC](https://github.com/rust-lang/rust/pull/73001) aimed to provide this method as part of the standard library. This RFC was rejected with a note that there is a desire for a more general capability to import any trait method.
14
+
There has for a long time been a desire to shorten the duplication needed to access certain associated functions, such as `Default::default`. Codebases like [Bevy](https://github.com/bevyengine/bevy/blob/7c7d1e8a6442a4258896b6c605beb1bf50399396/crates/bevy_utils/src/default.rs#L27) provide wrapper functions to shorten this call, and a previous, now-rejected, [RFC](https://github.com/rust-lang/rust/pull/73001) aimed to provide this function as part of the standard library. This RFC was rejected with a note that there is a desire for a more general capability to import any trait associated functions.
15
15
16
-
Additionally, if you pull in a crate like [num_traits](https://docs.rs/num-traits/latest/num_traits/), then this feature will allow access to numeric methods such as `sin` using the `sin(x)` syntax that is more common in mathematics. More generally, it will make calls to trait methods shorter without having to write a wrapper function.
16
+
Additionally, if you pull in a crate like [num_traits](https://docs.rs/num-traits/latest/num_traits/), then this feature will allow access to numeric functions such as `sin` using the `sin(x)` syntax that is more common in mathematics. More generally, it will make calls to trait associated functions shorter without having to write a wrapper function.
Importinga method from a trait does not import the trait.If you want to call `self` methods on a trait or `impl` it, then you can import the traitas well as methodsinthe trait:
52
+
Importingan associated function from a trait does not import the trait.If you want to call `self` methods on a trait or `impl` it, then you can import the traitand its associated functionsina single import statement:
53
53
54
54
```rust
55
55
mod a {
@@ -80,7 +80,7 @@ mod b {
80
80
}
81
81
```
82
82
83
-
Trait methods can also be renamed when they are imported using the usual `as` syntax:
83
+
Associated functions can also be renamed when they are imported using the usual `as` syntax:
84
84
```rust
85
85
useDefault::default as gimme
86
86
@@ -97,7 +97,7 @@ impl S {
97
97
}
98
98
```
99
99
100
-
You cannot import a parent traitmethod from a sub-trait:
100
+
You cannot import a parent traitassociated function from a sub-trait:
occurs, a new item `m` is made available in the function namespace of the current module. Any attempts to call this item are treated calling the trait method explicitly qualified. As always, the `as` qualifier is optional, in which case the name of the new item is identical with the name of the method in the trait. In other words, the example:
119
+
occurs, a new item `m` is made available in the function namespace of the current module. Any attempts to call this item are treated calling the associated function explicitly qualified. As always, the `as` qualifier is optional, in which case the name of the new item is identical with the name of the associated function in the trait. In other words, the example:
120
120
121
121
```rust
122
122
useDefault::default;
@@ -149,12 +149,12 @@ impl S {
149
149
```
150
150
And a call
151
151
```rust
152
-
useTrait::methodas m;
152
+
useTrait::funcas m;
153
153
m(x, y, z);
154
154
```
155
155
desugars to
156
156
```rust
157
-
Trait::method(x, y, z);
157
+
Trait::func(x, y, z);
158
158
```
159
159
160
160
Additionally, the syntax
@@ -166,14 +166,14 @@ is sugar for
166
166
usesome_module::Trait;
167
167
```
168
168
169
-
The restriction on importing parent trait methods is a consequence of this desugaring, see https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=51bef9ba69ce1fc20248e987bf106bd4 for examples of the errors you get when you try to call parent trait methods through a child trait. We will likely want better error messages than this if a user tries to import a parent method.
169
+
The restriction on importing parent trait associated functions is a consequence of this desugaring, see https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=51bef9ba69ce1fc20248e987bf106bd4 for examples of the errors you get when you try to call parent trait associated functions through a child trait. We will likely want better error messages than this if a user tries to import a parent function.
170
170
171
171
# Drawbacks
172
172
[drawbacks]: #drawbacks
173
173
174
174
Calls to `default` are less explicit than calls to `Default::default` or to `T::default`, likewise for any other trait. Some users may see this lack of explicitness as bad style.
175
175
176
-
To expand on this, [the book](https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html#creating-idiomatic-use-paths) currently recommends that methods should be called using their parent module's name:
176
+
To expand on this, [the book](https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html#creating-idiomatic-use-paths) currently recommends that functions should be called using their parent module's name:
177
177
> Although both Listing 7-11 and 7-13 accomplish the same task, Listing 7-11 is the idiomatic way to bring a function into scope with use. Bringing the function’s parent module into scope with use means we have to specify the parent module when calling the function. Specifying the parent module when calling the function makes it clear that the function isn’t locally defined while still minimizing repetition of the full path.
178
178
179
179
This recommendation makes the most sense when there is a possibility of ambiguity in the mind of the reader. For example, a function like `sin` is unlikely to be ambiguous, because there is only one mathematical function of that name. If a codebase is likely to be making use of multiple different implementations of `sin`, then it makes more sense to require specifically naming the one you are going to use. Similar considerations apply to traits like `Default::default`, or more generally in cases like `Frobnicator::frobnicate`.
@@ -189,32 +189,32 @@ This design is minimalist, it adds no extra syntax, instead providing a natural
189
189
190
190
## What other designs have been considered and what is the rationale for not choosing them?
191
191
192
-
In [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Writing.20an.20RFC.20for.20.60use.20Default.3A.3Adefault.60/near/427795694), there was some discussion of whether `use Trait::method` should bring `Trait` into scope. There are three possibilities:
192
+
In [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Writing.20an.20RFC.20for.20.60use.20Default.3A.3Adefault.60/near/427795694), there was some discussion of whether `use Trait::func` should bring `Trait` into scope. There are three possibilities:
193
193
194
194
1. It does not - this may be unexpected, but maybe not
195
-
2. It does - then `value.other_method_from_the_same_trait()` will work as well, this may be unexpected too
196
-
3. It does, but only for method, that's something new for the language (need new more fine-grained tracking of traits in scope)
195
+
2. It does - then `value.other_func_from_the_same_trait()` will work as well, this may be unexpected too
196
+
3. It does, but only for `func`, that's something new for the language (need new more fine-grained tracking of traits in scope)
197
197
198
-
Option 1 is what is proposed here. It has the simplest semantics, and I believe it best matches the user intent when they import a trait method; the desire is to make that method available as-if it were a regular function. Furthermore, it is more minimalist than the other two options in the sense that you can get to option 2 simply by importing the trait also. Option 3 seems like extra complexity for almost no added value.
198
+
Option 1 is what is proposed here. It has the simplest semantics, and I believe it best matches the user intent when they import an associated function; the desire is to make that function available as-if it were a regular function. Furthermore, it is more minimalist than the other two options in the sense that you can get to option 2 simply by importing the trait also. Option 3 seems like extra complexity for almost no added value.
199
199
200
200
## What is the impact of not doing this?
201
201
202
-
Users of the language continue to create helper methods to access trait methods with regular function syntax. More specifically, each such instance requires a minimum of three lines when using normal rust formatting, corresponding to the following example:
202
+
Users of the language continue to create helper functions to access associated with regular function syntax. More specifically, each such instance requires a minimum of three lines when using normal rust formatting, corresponding to the following example:
203
203
```rust
204
-
fnmy_trait_method<T:MyTrait>(args) ->ret {
205
-
MyTrait::my_trait_method(args)
204
+
fnmy_trait_func<T:MyTrait>(args) ->ret {
205
+
MyTrait::my_trait_func(args)
206
206
}
207
207
```
208
208
Such code is boilerplate that serves nobody's time to have to write repeatedly.
209
209
210
210
## If this is a language proposal, could this be done in a library or macro instead? Does the proposed change make Rust code easier or harder to read, understand, and maintain?
211
211
212
-
A library solution has already been rejected for this. This solves the same problem as a library solution in a much more general way, that doesn't require adding new library methods every time we want shorthand access to trait method names.
212
+
A library solution has already been rejected for this. This solves the same problem as a library solution in a much more general way, that doesn't require adding new library functions every time we want shorthand access to trait function names.
213
213
214
214
# Prior art
215
215
[prior-art]: #prior-art
216
216
217
-
As mentioned in [motivation], there was a rejected [RFC](https://github.com/rust-lang/rust/pull/73001) for adding a method`std::default::default` to make calling `Default::default` less repetitive. This RFC was rejected, with a desire to see something like what this RFC proposes replace it.
217
+
As mentioned in [motivation], there was a rejected [RFC](https://github.com/rust-lang/rust/pull/73001) for adding a function`std::default::default` to make calling `Default::default` less repetitive. This RFC was rejected, with a desire to see something like what this RFC proposes replace it.
218
218
219
219
[This issue](https://github.com/rust-lang/rfcs/issues/1995) also lists some further motivation for this feature.
0 commit comments