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
@@ -191,6 +199,40 @@ that has very complex diagnostics that are hard to improve, even if it's nice on
191
199
Users can use crates like [`genawaiter`](https://crates.io/crates/genawaiter) instead, which work on stable and give you `gen!` blocks that behave pretty mostly
192
200
like `gen` blocks, but don't have compiler support for nice diagnostics or language support for the `?` operator.
193
201
202
+
## `return` statements `yield` one last element
203
+
204
+
Similarly to `try` blocks, trailing expresisons could yield their element.
205
+
206
+
But then have no way to terminate iteration, as `return` statements would similarly have to have a
207
+
value that needs to get `yield`ed before terminating iteration.
208
+
209
+
We could do something magical where returning `()` terminates the iteration, so
210
+
211
+
```rust
212
+
genfnfoo() ->i32 {
213
+
42
214
+
}
215
+
```
216
+
217
+
could be a way to specify `std::iter::once(42)`. The issue I see with this is that
218
+
219
+
```rust
220
+
genfnfoo() ->i32 {
221
+
42; // note the semicolon
222
+
}
223
+
```
224
+
225
+
would then not return a value.
226
+
227
+
Furthermore this would make it unclear what the behaviour of
228
+
229
+
```rust
230
+
genfnfoo() {}
231
+
```
232
+
233
+
is supposed to be, as it could be either `std::iter::once(())` or `std::iter::empty::<()>()`
234
+
235
+
194
236
# Prior art
195
237
[prior-art]: #prior-art
196
238
@@ -277,3 +319,22 @@ while `gen` blocks are the simpler concept that has no arguments and just captur
277
319
278
320
Either way, support for full `Generator`s should (in my opinion) be discussed and implemented separately,
279
321
as there are many more open questions around them than around just a simpler way to write `Iterator`s.
322
+
323
+
## `async` interactions
324
+
325
+
We could support using `await` in `gen` blocks, similar to how we support `?` being used within them.
326
+
This is not trivially possible due to the fact that `Iterator::next` takes `&mut self` and not `Pin<&mut self>`.
327
+
328
+
There are a few options forward for this:
329
+
330
+
* Add a separate trait for pinned iteration that is also usable with `gen` and `for`
331
+
* downside: very similar traits for the same thing
332
+
* backwards compatibly add a way to change the argument type of `Iterator::next`
333
+
* downside: unclear if possible
334
+
* implement `Iterator` for `Pin<&mut G>` instead of for `G` directly (whatever `G` is here, but it could be a `gen` block)
335
+
* downside: the thing being iterated over must now be pinned for the entire iteration, instead of for each iteration
336
+
337
+
## `try` interactions
338
+
339
+
We could allow `try gen fn foo() -> i32` to actually mean something akin to `gen fn foo() -> Result<i32, E>`.
340
+
Whatever we do here, it should mirror whatever `try fn` will mean in the future.
0 commit comments