Skip to content
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

BufRead for ring buffers #3800

Open
darkwater opened this issue Jan 23, 2025 · 1 comment
Open

BufRead for ring buffers #3800

darkwater opened this issue Jan 23, 2025 · 1 comment

Comments

@darkwater
Copy link
Contributor

I'm writing a crate that parses data directly out of an embassy_stm32 BufferedUartRx. This goes wrong when the internal ring buffer wraps around, causing only the "front" slice to be returned by BufRead::fill_buf() until it is consumed, meaning I can't avoid allocating a second buffer for parsing. I wonder if we can do better than that.

Initially I thought that BufRead shouldn't be implemented for ring buffers, and this implementation would have to move around its contents to keep them contiguous, but that's probably not a good idea. Besides, std implements BufRead for VecDeque.

One idea would be an additional RingBufRead trait, since ring buffers are common enough, especially on embedded:

pub trait RingBufRead: BufRead {
  // mimicking VecDeque::as_slices()
  async fn fill_buf_slices(&mut self) -> Result<(&[u8], &[u8]), Self::Error>;
}

Or, this method could be added to BufRead with a default implementation that calls fill_buf and returns (fill_buf_result, &[]).

Thoughts?

@darkwater
Copy link
Contributor Author

I implemented a proof of concept here (embassy) and here (embedded-hal). I'm also working on a utility for nom to deal with split buffers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant