Skip to content

Commit

Permalink
ioctl/framesizes: add v4l2_frmsizeenum accessor
Browse files Browse the repository at this point in the history
Add an accessor for v4l2_frmsizeenum anonymous
union members, avoiding having to write the union
name and safely returning the appropiate member of
the union that is used.

Signed-off-by: Albert Esteve <aesteve@redhat.com>
  • Loading branch information
aesteve-rh authored and Gnurou committed Sep 26, 2023
1 parent 883dba7 commit 532c316
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/src/ioctl/framesizes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,40 @@ impl FrameSize for bindings::v4l2_frmsizeenum {
}
}

/// A wrapper for the 'v4l2_frmsizeenum' union member types
#[derive(Debug)]
pub enum FrmSizeTypes<'a> {
Discrete(&'a bindings::v4l2_frmsize_discrete),
StepWise(&'a bindings::v4l2_frmsize_stepwise),
}

impl bindings::v4l2_frmsizeenum {
/// Safely access the size member of the struct based on the
/// returned index.
pub fn size(&self) -> Option<FrmSizeTypes> {
match self.index {
// SAFETY: the member of the union that gets used by the driver
// is determined by the index
bindings::v4l2_frmsizetypes_V4L2_FRMSIZE_TYPE_DISCRETE => {
Some(FrmSizeTypes::Discrete(unsafe {
&self.__bindgen_anon_1.discrete
}))
}

// SAFETY: the member of the union that gets used by the driver
// is determined by the index
bindings::v4l2_frmsizetypes_V4L2_FRMSIZE_TYPE_CONTINUOUS
| bindings::v4l2_frmsizetypes_V4L2_FRMSIZE_TYPE_STEPWISE => {
Some(FrmSizeTypes::StepWise(unsafe {
&self.__bindgen_anon_1.stepwise
}))
}

_ => None,
}
}
}

#[doc(hidden)]
mod ioctl {
use crate::bindings::v4l2_frmsizeenum;
Expand Down

0 comments on commit 532c316

Please sign in to comment.