Skip to content

Commit bc3832c

Browse files
Add return type notation call for testing post
1 parent 5df30da commit bc3832c

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---
2+
layout: post
3+
title: "Return Type Notation MVP: Call for Testing!"
4+
author: Michael Goulet
5+
team: The Async Working Group <https://www.rust-lang.org/governance/wgs/wg-async>
6+
---
7+
8+
The async working group is excited to announce that [RFC 3654] return type notation (RTN) is ready for testing. In this post, we want to briefly describe the feature and announce a call for testing on nightly Rust.
9+
10+
## The backstory
11+
12+
Rust 1.75 [stabilized](https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html) async fn in traits (AFIT) and return-position impl Trait in traits (RPITIT). These desugar to anonymous generic associated types (GATs). However, unlike GATs, users of these types cannot use `where` clauses to further restrict these return types. This is known as the ["send bound"](https://smallcultfollowing.com/babysteps/blog/2023/02/01/async-trait-send-bounds-part-1-intro/) problem, since it often affects `Send` bounds on futures in the async ecosystem.
13+
14+
### An example
15+
16+
Consider a trait `Foo` with a `method` that returns a type of `impl Future<Output = ()>`. We want to write a function that calls `method` and spawns the future on another thread:
17+
18+
```rust
19+
fn spawn<T>(f: impl Future<Output = T> + Send + 'static) {}
20+
21+
trait Foo {
22+
fn method() -> impl Future<Output = ()>; // <-- RPITIT.
23+
}
24+
25+
fn needs_sendable_future<T: Foo>()
26+
where
27+
// How do we further restrict `T::method()` to be `Send + 'static`?
28+
{
29+
spawn(T::method());
30+
//~^ ERROR: `impl Future<Output = ()>` is not `Send`!
31+
}
32+
```
33+
34+
Specifically, we may not want to restrict the *declaration* of `Foo`, since changing it in the declaration would restrict *all* implementations of `Foo`.
35+
36+
```rust
37+
trait Foo {
38+
fn method() -> impl Future<Output = ()> + Send + 'static;
39+
// ~~~~~~~~~~~~~~~~
40+
// Not what we want.
41+
}
42+
```
43+
44+
So, on stable Rust, we have no way of expressing this restriction when using AFIT or RPITIT. In contrast, we can express this today if we were to use a GAT directly:
45+
46+
```rust
47+
trait Foo {
48+
type MethodFuture: Future<Output = ()>;
49+
fn method() -> Self::MethodFuture;
50+
}
51+
52+
fn needs_sendable_future<T: Foo>()
53+
where
54+
// We can restrict this to only implementors of `Foo`
55+
// whose `MethodFuture` is `Send + 'static`, so we can
56+
// call `spawn` below:
57+
T::MethodFuture: Send + 'static
58+
{
59+
spawn(T::method());
60+
}
61+
```
62+
63+
However, using GATs means that implementors of `Foo` have to write out the return type explicitly, `type MethodFuture = ...`, which doesn't ([yet](https://github.com/rust-lang/rust/pull/120700)) work if we have an anonymous, unnameable `Future` type!
64+
65+
## The solution
66+
67+
In [RFC 3654] we introduced return type notation (RTN). This will allow us to write `where` clause bounds that restrict the return types of functions and methods that use async fn in traits (AFIT) and return-position impl Trait in traits (RPITIT). Extending the example above, RTN lets us write:
68+
69+
```rust
70+
fn needs_sendable_future<T: Foo>()
71+
where
72+
T::method(..): Send + 'static //
73+
{
74+
spawn(T::method());
75+
//~^ Works!
76+
}
77+
```
78+
79+
## Restrictions
80+
81+
Currently, RTN is only allowed for trait associated functions and methods with lifetime generics (not const or type generics) that use:
82+
83+
* async fn in traits (AFIT)
84+
* return-position impl Trait in traits (RPITIT) where the impl Trait is the outermost return type, i.e. `-> impl Trait`, but not `-> Box<impl Trait>`
85+
86+
These restrictions are described in further detail in [RFC 3654].
87+
88+
## How do I help?
89+
90+
We'd love for you to test out this new feature!
91+
92+
Specifically, we'd like for you to identify traits where you're unnecessarily restricting your trait definitions with `+ Send` or similar bounds:
93+
94+
```rust
95+
// Instead of writing a trait like:
96+
97+
trait Foo {
98+
fn method() -> impl Future<Output = ()> + Send + 'static;
99+
}
100+
101+
// Write this:
102+
103+
trait Foo {
104+
async fn method();
105+
}
106+
107+
// And then at the call site, add:
108+
109+
fn use_foo<T: Foo>()
110+
where
111+
T::method(..): Send + 'static,
112+
{}
113+
```
114+
115+
Similarly, we'd like for you to identify traits that currently are returning GATs for the same reason:
116+
117+
```rust
118+
// Instead of writing this in the trait and call site:
119+
120+
trait Foo {
121+
type MethodFuture: Future<Output = ()>;
122+
fn method() -> Self::MethodFuture;
123+
}
124+
125+
fn use_foo<T: Foo>()
126+
where
127+
T::MethodFuture: Send + 'static,
128+
{}
129+
130+
// Write this:
131+
132+
trait Foo {
133+
async fn method();
134+
}
135+
136+
fn use_foo<T: Foo>()
137+
where
138+
T::method(..): Send + 'static,
139+
{}
140+
```
141+
142+
Note, however, that we don't yet support RTN in type position. So while, with the first version, you can write:
143+
144+
```rust
145+
struct Bar<T: Foo> {
146+
field: T::MethodFuture,
147+
}
148+
```
149+
150+
You can't yet, with the second version, write:
151+
152+
```rust
153+
struct Bar<T: Foo> {
154+
field: T::method(..),
155+
}
156+
```
157+
158+
We'd be interested in hearing about any places where you would run into this limitation.
159+
160+
We're excited for RTN to make it easier to use async fn in traits (AFIT) in `Send`-bound-heavy async Rust ecosystems.
161+
162+
As always, take a look at the [RFC][RFC 3654] itself for a detailed explanation for why we settled on this design, in particular the [frequently-asked questions and rationale](https://rust-lang.github.io/rfcs/3654-return-type-notation.html#rationale-and-alternatives).
163+
164+
[RFC 3654]: https://rust-lang.github.io/rfcs/3654-return-type-notation.html
165+
[RFC 3425]: https://rust-lang.github.io/rfcs/3425-return-position-impl-trait-in-traits.html

0 commit comments

Comments
 (0)