Skip to content

Commit

Permalink
respect bytesused, add getter for the raw buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
SludgePhD committed Dec 16, 2023
1 parent 168036d commit c6925e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "linuxvideo"
version = "0.3.2"
version = "0.3.3"
edition = "2021"
license = "0BSD"
description = "V4L2 video capture and output library"
Expand Down
20 changes: 18 additions & 2 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ impl ReadStream {
let view = ReadBufferView {
flags: buf.flags,
data,
bytesused: buf.bytesused as usize,
};

let res = cb(view);
Expand Down Expand Up @@ -284,24 +285,39 @@ impl AsRawFd for ReadStream {
pub struct ReadBufferView<'a> {
flags: BufFlag,
data: &'a [u8],
bytesused: usize,
}

impl ReadBufferView<'_> {
impl<'a> ReadBufferView<'a> {
/// Returns whether the error flag for this buffer is set.
///
/// If this returns `true`, the application should expect data corruption in the buffer data.
#[inline]
pub fn is_error(&self) -> bool {
self.flags.contains(BufFlag::ERROR)
}

/// Returns a reference to the *entire* backing buffer.
///
/// [`ReadBufferView`] dereferences to the *used* portion of the buffer. For fixed-size
/// (uncompressed) image formats, the *used* portion is typically equal to the entire buffer,
/// but for compressed formats like MJPEG, the backing buffer, which can be access with this
/// method, is usually a lot larger than the actual data of interest.
///
/// Normally, this method does not need to be used, as only the used portion of the buffer is
/// needed.
#[inline]
pub fn raw_buffer(&self) -> &'a [u8] {
self.data
}
}

impl Deref for ReadBufferView<'_> {
type Target = [u8];

#[inline]
fn deref(&self) -> &Self::Target {
self.data
&self.data[..self.bytesused]
}
}

Expand Down

0 comments on commit c6925e5

Please sign in to comment.