Splitting a futures::Stream into multiple streams based on a property of the stream item

Viewed 1245

I have a Stream of items (u32, Bytes) where the integer is an index in the range 0..n I would like to split this stream into n streams, basically filtering by the integer.

I considered several possibilities, including

  • creating n streams each of which peeks at the underlying stream to determine if the next item is for it
  • pushing the items to one of n sinks when they arrive, and then use the other side of the sink as a stream again. (This seems to be related to Forwarding from a futures::Stream to a futures::Sink.).

I feel that neither of these possibilities is convincing. The first one seems to create unnecessary overhead and the second one is just not elegant (if it even works, I am not sure).

What's a good way of splitting the stream?

2 Answers
Related