Skip to content

Commit

Permalink
Improve serde feature
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
  • Loading branch information
patrickelectric committed Jan 17, 2020
1 parent 83c644c commit 939e11f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ readme = "README.md"
[dependencies]
derivative = "1.0.3"
libc = "0.2"
serde_derive = { version = "1.0.104", optional = true }
serde = { version = "1.0.104", features = ["derive"], optional = true }

[features]
no_wrapper = []
static = []
default = ["serde_derive"]
default = ["serde"]
26 changes: 13 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub use v4l2::pubconsts as consts;
pub type Result<T> = result::Result<T, Error>;

#[cfg(feature = "serde")]
use serde::Serialize;
extern crate serde;

#[macro_use]
extern crate derivative;
Expand Down Expand Up @@ -94,7 +94,7 @@ impl error::Error for Error {
}
}

fn cause(&self) -> Option<&error::Error> {
fn cause(&self) -> Option<&dyn error::Error> {
if let Error::Io(ref err) = *self {
Some(err)
} else {
Expand All @@ -109,8 +109,8 @@ impl From<io::Error> for Error {
}
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Config<'a> {
/// The mix of numerator and denominator. v4l2 uses frame intervals instead of frame rates.
/// Default is `(1, 10)`.
Expand Down Expand Up @@ -142,7 +142,7 @@ impl<'a> Default for Config<'a> {
}
}

#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct FormatInfo {
/// FourCC of format (e.g. `b"H264"`).
pub format: [u8; 4],
Expand Down Expand Up @@ -255,7 +255,6 @@ impl fmt::Debug for IntervalInfo {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Frame {
/// Width and height of the frame.
pub resolution: (u32, u32),
Expand Down Expand Up @@ -292,19 +291,21 @@ impl Drop for Frame {
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
enum State {
Idle,
Streaming,
Aborted,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Camera {
fd: RawFd,
state: State,
resolution: (u32, u32),
format: [u8; 4],
#[cfg_attr(feature = "serde", serde(skip_serializing))]
buffers: Vec<Arc<MappedRegion>>,
}

Expand Down Expand Up @@ -712,7 +713,7 @@ impl Drop for Camera {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct FormatIter<'a> {
camera: &'a Camera,
index: u32,
Expand Down Expand Up @@ -741,7 +742,6 @@ impl<'a> Iterator for FormatIter<'a> {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct ControlIter<'a> {
camera: &'a Camera,
id: u32,
Expand Down Expand Up @@ -805,7 +805,7 @@ impl Settable for String {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Control {
pub id: u32,
pub name: String,
Expand All @@ -815,7 +815,7 @@ pub struct Control {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum CtrlData {
Integer {
value: i32,
Expand Down Expand Up @@ -862,14 +862,14 @@ pub enum CtrlData {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CtrlMenuItem {
pub index: u32,
pub name: String,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CtrlIntMenuItem {
pub index: u32,
pub value: i64,
Expand Down
41 changes: 19 additions & 22 deletions src/v4l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ pub fn mmap(length: usize, fd: RawFd, offset: usize) -> io::Result<MappedRegion>

#[derive(Derivative)]
#[derivative(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[repr(C)]
pub struct Format {
pub ftype: u32,
Expand Down Expand Up @@ -168,7 +167,7 @@ impl Format {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct PixFormat {
pub width: u32,
Expand Down Expand Up @@ -196,7 +195,7 @@ impl PixFormat {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct RequestBuffers {
pub count: u32,
Expand All @@ -218,7 +217,6 @@ impl RequestBuffers {

#[derive(Derivative)]
#[derivative(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[repr(C)]
pub struct Buffer {
pub index: u32,
Expand Down Expand Up @@ -247,7 +245,7 @@ impl Buffer {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct TimeCode {
pub ttype: u32,
Expand All @@ -260,7 +258,7 @@ pub struct TimeCode {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct FmtDesc {
pub index: u32,
Expand All @@ -281,7 +279,6 @@ impl FmtDesc {

#[derive(Derivative)]
#[derivative(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[repr(C)]
pub struct StreamParm {
pub ptype: u32,
Expand All @@ -301,7 +298,7 @@ impl StreamParm {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct CaptureParm {
pub capability: u32,
Expand All @@ -313,15 +310,15 @@ pub struct CaptureParm {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct Fract {
pub numerator: u32,
pub denominator: u32,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct Frmsizeenum {
pub index: u32,
Expand All @@ -348,15 +345,15 @@ impl Frmsizeenum {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct FrmsizeDiscrete {
pub width: u32,
pub height: u32,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct FrmsizeStepwise {
pub min_width: u32,
Expand All @@ -368,7 +365,7 @@ pub struct FrmsizeStepwise {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct Frmivalenum {
pub index: u32,
Expand Down Expand Up @@ -399,7 +396,7 @@ impl Frmivalenum {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct FrmivalStepwise {
pub min: Fract,
Expand All @@ -408,7 +405,7 @@ pub struct FrmivalStepwise {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct QueryCtrl {
pub id: u32,
Expand All @@ -431,7 +428,7 @@ impl QueryCtrl {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct QueryExtCtrl {
pub id: u32,
Expand Down Expand Up @@ -459,8 +456,7 @@ impl QueryExtCtrl {
}
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[repr(C, packed)]
pub struct QueryMenu {
pub id: u32,
Expand All @@ -469,6 +465,7 @@ pub struct QueryMenu {
reserved: u32,
}

#[derive(Copy, Clone)]
#[repr(C, packed)]
pub union QueryMenuData {
name: [u8; 32],
Expand Down Expand Up @@ -500,7 +497,7 @@ impl QueryMenuData {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct Control {
pub id: u32,
Expand All @@ -513,8 +510,8 @@ impl Control {
}
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C, packed)]
pub struct ExtControl {
pub id: u32,
Expand All @@ -535,7 +532,7 @@ impl ExtControl {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct ExtControls<'a> {
pub ctrl_class: u32,
Expand Down

0 comments on commit 939e11f

Please sign in to comment.