-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an API for Consumer to read all slots? #97
Comments
I'm aware that calling We could of course add a new function that returns a
I think I'm not against adding convenience functions, but the names must be good.
I don't think that |
You could also have a look at https://github.com/agerasev/ringbuf, which has a very different API, and it does not require specifying the number of slots, AFAICT. |
I agree with you, |
Thank you. 😄 |
I would suggest a couple of things:
|
Thanks for the suggestions!
Is this a real use case that you have? |
I'm using this library in a small side project of mine as a channel between an IO-thread and a parser thread. |
Thanks, that's an interesting use case! So if I understand correctly, you need something like this (using existing API): let chunk = c.read_chunk(c.slots()).unwrap();
if chunk.len() >= 25 {
// do something
} ... but with less noise: if let Ok(chunk) = c.read_chunk_least(25) {
// do something
} This new function could then also be used for simply getting all slots by using let chunk = c.read_chunk_least(0).unwrap();
// do something I don't love the name, and I think it's not good to have two similar functions that both take the same arguments (a single number). I think this might be confusing. For comparison, if we had a function to get all slots unconditionally (let's call it let chunk = c.max_read_chunk();
if chunk.len() >= 25 {
// do something
} This would still be more than a one-liner, but at least the I think having to type a few characters less is not reason enough to add a new function, but avoiding If we have to decide between the two options shown above, I think I would vote for the second one, which avoids Another way to look at it is that both |
There is also a subtle performance-related question: should Latter cannot be achieved with the current API. |
Mostly, but as the
Yes.
Doesn't seem like a bad idea to me. It would even work for my usecase since I'm wrapping it all anyway
A bool perhaps? It's a simple solution but probably not exactly elegant |
Just to clarify, that's not a data race, at least I hope not! It is natural that once one thread has finished reading an atomic variable, the other thread can change it again. Whether this happens within a single function or whether another function is called (in this case So even if we implement a new function From a thread-communication point of view, there is no difference between In the current implementation, |
Is there still interest in this feature? I'm still not convinced that it is worth implementing, since it can be written as |
Now, I read all slots from
Consumer
by this:But sometimes I don't care how many slots hold by
RingBuffer
, I just want to read all slots.Do you think it's a good idea to add a method for
Consumer
like this:The text was updated successfully, but these errors were encountered: