Each AsioStream is kept in a Acr<Mutex<Vec<Option<AsioStream>>>> and to get the stream you use the StreamId.0 - 1 because StreamId is created from incrementing stream_count = Arc::new(AtomicUsize::new(0)) .
Note this count is never decremented even if a stream is destroyed. The stream that is destroyed is simply set to None. (Although theres nothing enforcing this
The - 1 is because it starts off at 0 and the first stream is 1 but the index in the array is actually 0 for the first stream etc.
This is pretty error prone.
Ideas
Use a HashMap<StreamId, AsioStream> instead of the Vec. This would be a bit cleaner and be valid as long as the stream_count is never decremented. Downside is having to hash the StreamId inside the audio callback. I'm pretty sure hashing is one of the things to avoid inside audio stream callbacks.
Change the stream_count to Arc<Option<AtomicUsize>> so that the first stream can check for None and actually be 0. Come to think of it the AtomicUsize shouldn't be in an Arc but will have to be if we use an Option.
I feel like there's a better solution that I haven't thought of
Problem
Each
AsioStream
is kept in aAcr<Mutex<Vec<Option<AsioStream>>>>
and to get the stream you use theStreamId.0 - 1
because StreamId is created from incrementingstream_count = Arc::new(AtomicUsize::new(0))
. Note this count is never decremented even if a stream is destroyed. The stream that is destroyed is simply set to None. (Although theres nothing enforcing this The - 1 is because it starts off at 0 and the first stream is 1 but the index in the array is actually 0 for the first stream etc. This is pretty error prone.Ideas
HashMap<StreamId, AsioStream>
instead of the Vec. This would be a bit cleaner and be valid as long as thestream_count
is never decremented. Downside is having to hash theStreamId
inside the audio callback. I'm pretty sure hashing is one of the things to avoid inside audio stream callbacks.stream_count
toArc<Option<AtomicUsize>>
so that the first stream can check for None and actually be 0. Come to think of it the AtomicUsize shouldn't be in an Arc but will have to be if we use an Option.I feel like there's a better solution that I haven't thought of