Skip to content

Commit 7636de4

Browse files
committed
More detail about goals for sync and async code behave the same
1 parent 5cb7917 commit 7636de4

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/vision/roadmap/polish/sync_and_async.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,56 @@ unexpected should happen.
1717
| Create testing to ensure same behavior | 💤 | [yoshuawuyts] |
1818

1919
[yoshuawuyts]: https://github.com/yoshuawuyts
20+
21+
## Details
22+
23+
Ideally, there should not be a lot of work needed specifically to achieve this goal.
24+
Instead, the primary aim is to define principles that can inform the design in other places.
25+
That said, automated testing to verify we have achieved these principles may take significant effort.
26+
27+
There are several ways we can look at what it means to behave the same.
28+
One way is from a language and semantics standpoint, while another is from a library and ecosystem standpoint.
29+
We will look at each of these in more detail, and then lay out some ideas for automated testing.
30+
31+
### Language and Semantics
32+
33+
Roughly want we want here is that code that differs only in its syncness should do the same thing.
34+
Of course, this is not strictly possible because a sync and async program are fundamentally different program.
35+
We still want something approximating this.
36+
Below are several principles that try to make this more precise.
37+
For each one, we are talking about a synchronous and asynchronous version of a piece of code where the synchronous version is basically the async version with all the `async` and `.await` keywords removed.
38+
Note that this assumes there are no manually implemented futures.
39+
40+
1. **Equality**: if the sync version and the async version both produce a value, then the values are the same.
41+
2. **Termination**: either both the sync and async version terminate (or can be polled to completion in the async case), or both do not terminate.
42+
3. **Panic**: the sync version panics if and only if the async version panics.
43+
4. **Types***: if the sync version has type `T` then the async version has type `Future<Output = T>` and vice-versa.
44+
5. **Compilation***: either both the sync and async version compile successfully, or they both produce equivalent compiler errors on the same line.
45+
46+
The first three principles are probably not terrible hard to achieve.
47+
The last two, marked with an asterisk, may not be completely possible or even desirable in all cases.
48+
49+
For types, there is a fundamental difference in the async code because `.await` points expose types that would be purely internal in the sync version.
50+
One impact of this is that the auto traits may not be the same between the two.
51+
We might be able to get this property in one direction though.
52+
For example, adding a `.await` might make future not `Send`, but removing a `.await` will probably not remove any auto traits.
53+
54+
Compilation is closely related to the types goal because if async causes the types to change then this could introduce or remove compilation errors.
55+
Additionally, we will probably have some async-only diagnostics, such as the [`must_not_suspend` lint][must_not_suspend].
56+
57+
### Library and Ecosystem
58+
59+
At a high level, the library and ecosystem goals are about having comparable capabilities available in libraries for both sync and async code.
60+
For example, mutexes in an async context need integration with the runtime, so the standard synchronous mutex is not generally suitable for async code.
61+
For this reason, most async runtimes provide some form of `AsyncMutex`.
62+
63+
Note that one way to achieve this may be through [Async Overloading].
64+
65+
🛠️ This area is still underdeveloped, so if you would like to help this would be a great place to pitch in!
66+
67+
### Automated Testing
68+
69+
🛠️ This area is still underdeveloped, so if you would like to help this would be a great place to pitch in!
70+
71+
[Async Overloading]: ../async_overloading.md
72+
[must_not_suspend]: ./lint_must_not_suspend.md

0 commit comments

Comments
 (0)