Hi there,
I'm working on a project on stable Rust, and I'm in a situation where I need something like generators / coroutines. But, being on stable, I'd like to do this with just vanilla futures. I think it will involve implementing my own Executor, and I'd love some help on how to get started.
Here's some Python that shows the kind of thing I'd like to do:
from itertools import zip_longest
class Thing:
def __init__(self, name):
self.name = name
def go(self, ticker): # needs &mut Ticker
# will call various mut method of ticker
ticker.tick(self.name)
yield
ticker.tick(self.name)
yield
ticker.tick(self.name)
yield
class Ticker:
def __init__(self):
self.num = 0
def tick(self, name):
print("{}: {}".format(name, self.num))
def cycle(self, things):
iters = [thing.go(self) for thing in things]
for _ in zip_longest(*iters):
self.num += 1
Ticker().cycle([
Thing("a"),
Thing("b"),
Thing("c"),
])
This will print the following:
a: 0
b: 0
c: 0
a: 1
b: 1
c: 1
a: 2
b: 2
c: 2
Ticker
is sort of serving as the Executor
, and it's cycling through the Thing
s it has, doing one unit of work each time. The go
method of Thing
requires exclusive access to the "Context" (something like &mut Ticker
), but only while it's running. While yielded, other Thing
s can do part of their go
work.
Any help on how to go about this would be appreciated!