Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions book/src/async.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,25 @@ void shim_doThing(
});
}
```

### Streams

The async example above can be extended to support streaming data as well using a channel such as [`futures::channel::mpsc::unbounded`] instead of oneshot and passing `DoThingContext` to the closure as a reference:

[`futures::channel::mpsc::unbounded`]: https://docs.rs/futures/0.3.8/futures/channel/mpsc/fn.unbounded.html

```cpp
// bridge_shim.cc

#include "path/to/bridge.rs.h"
#include "rust/cxx.h"

void shim_doThing(
Arg arg,
rust::Fn<void(const DoThingContext &ctx, Ret ret)> done,
rust::Box<DoThingContext> ctx) noexcept {
done(*ctx, std::move(res));
done(*ctx, std::move(res));
done(*ctx, std::move(res));
Comment on lines +104 to +106
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you able to share some code structure that is more illustrative of how these res values would be obtained asynchronously in C++? As is, this example looks like it is just synchronously sending 3 values, which would not make sense to treat as futures::Stream in Rust. And something like co_await arg->next() does not work because this function is not a coroutine.

I am not familiar enough with streaming APIs in C++ to tell from this example how it would translate to usable real-world code.

}
```