4 ms·
I'm aware that encapsulated control flow like this exists at a syntactic level, but the implementation deficiencies remain unaddressed: 1. No access to the act
by 10000truths 3y ago
I'm aware that encapsulated control flow like this exists at a syntactic level, but the implementation deficiencies remain unaddressed:
1. No access to the actual concrete type, as you mentioned, which means you can't do Vec<Stream<...>> - you have to resort to indirections like Vec<Box<dyn Stream<...>>>. You might be able to work around this somehow if you were determined enough (DWARF debug info and std::mem::transmute come to mind), but any such approach would result in an ugly and exceedingly fragile abomination.
2. Brittle, if any, optimization. `stream!` is basically async/await with proc macro syntax dressing, and Rust's async/await implementation still suffers the same sort of bloated frame size issues as generators do, under certain circumstances.
To provide some context, suppose I want to write a custom event loop that handles millions of concurrent sessions (could be filesystem transactions, or TCP sessions, or some other I/O). At that level, every kilobyte of per-session data I add equates to additional gigabytes of memory usage. Every byte of space used by the compiler-generated state machine has to be carefully accounted for. I can't afford to have my per-session context blowing up in size because the compiler naively duplicates stack slots for arguments across yield points [1], or because one of my local variable types implements Copy [2], or because the compiler will simply fail to optimize local variables that are never live across a yield point [3]. I suppose if I care so much about memory usage, I could just draw up my own state machine, mentally track the liveness of each state variable and lay out a hand-written size-optimized struct accordingly, but that's going to be a painful exercise and a maintainability nightmare.
[1] https://github.com/rust-lang/rust/issues/62958 https://github.com/rust-lang/rust/issues/62958
[2] https://github.com/rust-lang/rust/issues/62952 https://github.com/rust-lang/rust/issues/62952
[3] https://github.com/rust-lang/rust/issues/59087 https://github.com/rust-lang/rust/issues/59087