Async stream producer consumer stack

I'm new to rust (2 days in) and have recently been playing with the async functionality, I've been reading as much documentation as I can find. I set myself the task of creating a async stream backed by a thread-safe stack (LIFO) to recycle values.

While this solution works, it seems complicated for what i'm trying to achieve, I feel like I'm missing something simpler. if anyone has any feedback and links to documentation or other examples that I can use to improve this pattern. It would be greatly appreciated.

https://gist.github.com/zeroflaw/9908644d7d6951cc0ac1f2eaa6d450d6

Thanks in advance.

Well the complexity comes from the Treiber stack? Also, it's best to avoid mixing Tokio with async-std.

I could not find a Semaphore in async-std, the only other one I found was here . Thats been deprecated, has it been moved? So I used the one in Tokio

As for the Treiber stack, it was the only data structure that was thread-safe? and LIFO, I found that code from the crossbeam package that again was deprecated after version 6.0.0.

I typically recommend people use Tokio anyway. In principle it's okay to use Tokio's Semaphore outside of Tokio, but there are many of Tokio's types for which this is not fine, so it can be risky.

As for the Treiber stack, well I don't think a structure like the one you built exists in any of the crates I know of. That's probably related to the fact that it's pretty rare for people to need an async Treiber stack. I'm guessing that Tokio would accept a PR with such a collection.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.