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
Copy file name to clipboardExpand all lines: text/0000-return-type-notation.md
+6-18Lines changed: 6 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,7 @@
8
8
# Summary
9
9
[summary]: #summary
10
10
11
-
Return type notation (RTN) gives a way to reference or bound the type returned by a trait method.
12
-
The new bounds look like `T: Trait<method(..): Send>` or `T::method(..): Send`.
13
-
The primary use case is to add bounds such as `Send` to the futures returned by `async fn`s in traits and `-> impl Future` functions, but they work for any trait function defined with return-position impl trait (e.g., `where T: Factory<widgets(..): DoubleEndedIterator>` would also be valid).
11
+
Return type notation (RTN) gives a way to reference or bound the type returned by a trait method. The new bounds look like `T: Trait<method(..): Send>` or `T::method(..): Send`. The primary use case is to add bounds such as `Send` to the futures returned by `async fn`s in traits and `-> impl Future` functions, but they work for any trait function defined with return-position impl trait (e.g., `where T: Factory<widgets(..): DoubleEndedIterator>` would also be valid).
14
12
15
13
This RFC proposes a new kind of type written `<T as Trait>::method(..)` (or `T::method(..)` for short). RTN refers to "the type returned by invoking `method` on `T`".
16
14
@@ -123,8 +121,7 @@ But this `SendService` trait is too strong for use outside a work-stealing setup
123
121
124
122
## Comparison to an analogous problem with `IntoIterator`
125
123
126
-
It is useful to compare this situation with analogous scenarios that arise elsewhere in Rust, such as with associated types. Imagine a function that takes an `I: IntoIterator`
127
-
and which wishes to make use of the returned iterator in a separate thread:
124
+
It is useful to compare this situation with analogous scenarios that arise elsewhere in Rust, such as with associated types. Imagine a function that takes an `I: IntoIterator` and which wishes to make use of the returned iterator in a separate thread:
128
125
129
126
```rust
130
127
fninto_iter_example<I:IntoIterator>(i:I) {
@@ -166,20 +163,13 @@ There are two ways the function `into_iter_example` could be made to compile:
166
163
1. Modify the `IntoIterator` trait to require that the target iterator type is *always*`Send`
167
164
2. Modify the function to have a where-clause `I::IntoIter: Send`.
168
165
169
-
The 1st option is less flexible but more convenient;
170
-
it is inappropriate in a highly generic trait like `IntoIterator` which is used in a number of scenarios.
171
-
It would be fine for an application- or library-specific crate that is only used in narrow circumstances.
172
-
Referring back to the compiler's error message, you can see that an additional where-clause is exactly what it suggested.
166
+
The 1st option is less flexible but more convenient; it is inappropriate in a highly generic trait like `IntoIterator` which is used in a number of scenarios. It would be fine for an application- or library-specific crate that is only used in narrow circumstances. Referring back to the compiler's error message, you can see that an additional where-clause is exactly what it suggested.
173
167
174
-
This is the challenge: **Rust does not currently have a way to write the equivalent of `where I::IntoIter: Send` for the futures returned by `async fn` (or the results of `-> impl Trait` methods in traits).**
175
-
This creates a gap between the first `Service` example, which can only be resolved by modifying the trait,
176
-
and `IntoIterator`, which can be resolved either by modifying the trait or by adding a where-clause to the function,
177
-
whichever is more appropriate.
168
+
This is the challenge: **Rust does not currently have a way to write the equivalent of `where I::IntoIter: Send` for the futures returned by `async fn` (or the results of `-> impl Trait` methods in traits).** This creates a gap between the first `Service` example, which can only be resolved by modifying the trait, and `IntoIterator`, which can be resolved either by modifying the trait or by adding a where-clause to the function, whichever is more appropriate.
178
169
179
170
## Return type notation (RTN) permits the return type of AFIT and RPITIT to be bounded, closing the gap
180
171
181
-
The core feature proposed in this RFC is the ability to write a bound that bounds the return type of an AFIT/RPITIT trait method.
182
-
This allows the `spawn_call` definition to be amended to require that `call()` returns a `Send` future:
172
+
The core feature proposed in this RFC is the ability to write a bound that bounds the return type of an AFIT/RPITIT trait method. This allows the `spawn_call` definition to be amended to require that `call()` returns a `Send` future:
183
173
184
174
```rust
185
175
asyncfnspawn_call<S>(service:S) ->S::Response
@@ -201,9 +191,7 @@ A variant of the proposal in this RFC is already implemented, so you can [try th
201
191
202
192
## RTN is useful for more than `Send` bounds
203
193
204
-
RTN is useful for more than `Send` bounds.
205
-
For example, consider the trait `Factory`,
206
-
which contains a method that returns an `impl Iterator`:
194
+
RTN is useful for more than `Send` bounds. For example, consider the trait `Factory`, which contains a method that returns an `impl Iterator`:
0 commit comments