Skip to content

Commit 11947bf

Browse files
Add debug macro in structs and enums
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
1 parent 7e7174b commit 11947bf

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ readme = "README.md"
1111
edition = "2018"
1212

1313
[dependencies]
14+
derivative = "1.0.3"
1415
libc = "0.2"
1516
thiserror = "1.0.9"
1617

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ mod v4l2;
4545

4646
pub type Result<T> = result::Result<T, Error>;
4747

48+
#[macro_use]
49+
extern crate derivative;
50+
4851
#[derive(Debug, thiserror::Error)]
4952
pub enum Error {
5053
#[error("I/O error: {0}")]
@@ -59,6 +62,7 @@ pub enum Error {
5962
BadField,
6063
}
6164

65+
#[derive(Debug)]
6266
pub struct Config<'a> {
6367
/// The mix of numerator and denominator. v4l2 uses frame intervals instead of frame rates.
6468
/// Default is `(1, 10)`.
@@ -201,6 +205,7 @@ impl fmt::Debug for IntervalInfo {
201205
}
202206
}
203207

208+
#[derive(Debug)]
204209
pub struct Frame {
205210
/// Width and height of the frame.
206211
pub resolution: (u32, u32),
@@ -243,6 +248,7 @@ enum State {
243248
Aborted,
244249
}
245250

251+
#[derive(Debug)]
246252
pub struct Camera {
247253
fd: RawFd,
248254
state: State,
@@ -654,6 +660,7 @@ impl Drop for Camera {
654660
}
655661
}
656662

663+
#[derive(Debug)]
657664
pub struct FormatIter<'a> {
658665
camera: &'a Camera,
659666
index: u32,
@@ -681,6 +688,7 @@ impl<'a> Iterator for FormatIter<'a> {
681688
}
682689
}
683690

691+
#[derive(Debug)]
684692
pub struct ControlIter<'a> {
685693
camera: &'a Camera,
686694
id: u32,

src/v4l2.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(clippy::unreadable_literal)]
22

33
use std::ffi::CString;
4+
use std::fmt;
45
use std::os::unix::io::RawFd;
56
use std::ptr::null_mut;
67
use std::{io, mem, usize};
@@ -94,6 +95,7 @@ pub fn xioctl_valid<T>(fd: RawFd, request: usize, arg: &mut T) -> io::Result<boo
9495
}
9596
}
9697

98+
#[derive(Debug)]
9799
pub struct MappedRegion {
98100
pub ptr: *mut u8,
99101
pub len: usize,
@@ -130,12 +132,15 @@ pub fn mmap(length: usize, fd: RawFd, offset: usize) -> io::Result<MappedRegion>
130132
})
131133
}
132134

135+
#[derive(Derivative)]
136+
#[derivative(Debug)]
133137
#[repr(C)]
134138
pub struct Format {
135139
pub ftype: u32,
136140
#[cfg(target_pointer_width = "64")]
137141
padding: u32,
138142
pub fmt: PixFormat,
143+
#[derivative(Debug="ignore")]
139144
space: [u8; 156],
140145
}
141146

@@ -160,6 +165,7 @@ impl Format {
160165
}
161166
}
162167

168+
#[derive(Debug)]
163169
#[repr(C)]
164170
pub struct PixFormat {
165171
pub width: u32,
@@ -186,6 +192,7 @@ impl PixFormat {
186192
}
187193
}
188194

195+
#[derive(Debug)]
189196
#[repr(C)]
190197
pub struct RequestBuffers {
191198
pub count: u32,
@@ -205,13 +212,16 @@ impl RequestBuffers {
205212
}
206213
}
207214

215+
#[derive(Derivative)]
216+
#[derivative(Debug)]
208217
#[repr(C)]
209218
pub struct Buffer {
210219
pub index: u32,
211220
pub btype: u32,
212221
pub bytesused: u32,
213222
pub flags: u32,
214223
pub field: u32,
224+
#[derivative(Debug="ignore")]
215225
pub timestamp: Timeval,
216226
pub timecode: TimeCode,
217227
pub sequence: u32,
@@ -231,6 +241,7 @@ impl Buffer {
231241
}
232242
}
233243

244+
#[derive(Debug)]
234245
#[repr(C)]
235246
pub struct TimeCode {
236247
pub ttype: u32,
@@ -242,6 +253,7 @@ pub struct TimeCode {
242253
pub userbits: [u8; 4],
243254
}
244255

256+
#[derive(Debug)]
245257
#[repr(C)]
246258
pub struct FmtDesc {
247259
pub index: u32,
@@ -260,10 +272,13 @@ impl FmtDesc {
260272
}
261273
}
262274

275+
#[derive(Derivative)]
276+
#[derivative(Debug)]
263277
#[repr(C)]
264278
pub struct StreamParm {
265279
pub ptype: u32,
266280
pub parm: CaptureParm,
281+
#[derivative(Debug="ignore")]
267282
space: [u8; 160],
268283
}
269284

@@ -277,6 +292,7 @@ impl StreamParm {
277292
}
278293
}
279294

295+
#[derive(Debug)]
280296
#[repr(C)]
281297
pub struct CaptureParm {
282298
pub capability: u32,
@@ -287,12 +303,14 @@ pub struct CaptureParm {
287303
reserved: [u32; 4],
288304
}
289305

306+
#[derive(Debug)]
290307
#[repr(C)]
291308
pub struct Fract {
292309
pub numerator: u32,
293310
pub denominator: u32,
294311
}
295312

313+
#[derive(Debug)]
296314
#[repr(C)]
297315
pub struct Frmsizeenum {
298316
pub index: u32,
@@ -318,12 +336,14 @@ impl Frmsizeenum {
318336
}
319337
}
320338

339+
#[derive(Debug)]
321340
#[repr(C)]
322341
pub struct FrmsizeDiscrete {
323342
pub width: u32,
324343
pub height: u32,
325344
}
326345

346+
#[derive(Debug)]
327347
#[repr(C)]
328348
pub struct FrmsizeStepwise {
329349
pub min_width: u32,
@@ -334,6 +354,7 @@ pub struct FrmsizeStepwise {
334354
pub step_height: u32,
335355
}
336356

357+
#[derive(Debug)]
337358
#[repr(C)]
338359
pub struct Frmivalenum {
339360
pub index: u32,
@@ -363,13 +384,15 @@ impl Frmivalenum {
363384
}
364385
}
365386

387+
#[derive(Debug)]
366388
#[repr(C)]
367389
pub struct FrmivalStepwise {
368390
pub min: Fract,
369391
pub max: Fract,
370392
pub step: Fract,
371393
}
372394

395+
#[derive(Debug)]
373396
#[repr(C)]
374397
pub struct QueryCtrl {
375398
pub id: u32,
@@ -391,6 +414,7 @@ impl QueryCtrl {
391414
}
392415
}
393416

417+
#[derive(Debug)]
394418
#[repr(C)]
395419
pub struct QueryExtCtrl {
396420
pub id: u32,
@@ -418,6 +442,7 @@ impl QueryExtCtrl {
418442
}
419443
}
420444

445+
#[derive(Debug, Copy, Clone)]
421446
#[repr(C, packed)]
422447
pub struct QueryMenu {
423448
pub id: u32,
@@ -426,12 +451,19 @@ pub struct QueryMenu {
426451
reserved: u32,
427452
}
428453

454+
#[derive(Copy, Clone)]
429455
#[repr(C, packed)]
430456
pub union QueryMenuData {
431457
name: [u8; 32],
432458
value: i64,
433459
}
434460

461+
impl fmt::Debug for QueryMenuData {
462+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
463+
write!(f, "{:?}", self.name())
464+
}
465+
}
466+
435467
impl QueryMenu {
436468
pub fn new(id: u32) -> QueryMenu {
437469
let mut menu: QueryMenu = unsafe { mem::zeroed() };
@@ -450,6 +482,7 @@ impl QueryMenuData {
450482
}
451483
}
452484

485+
#[derive(Debug)]
453486
#[repr(C)]
454487
pub struct Control {
455488
pub id: u32,
@@ -462,6 +495,7 @@ impl Control {
462495
}
463496
}
464497

498+
#[derive(Debug, Copy, Clone)]
465499
#[repr(C, packed)]
466500
pub struct ExtControl {
467501
pub id: u32,
@@ -481,6 +515,7 @@ impl ExtControl {
481515
}
482516
}
483517

518+
#[derive(Debug)]
484519
#[repr(C)]
485520
pub struct ExtControls<'a> {
486521
pub ctrl_class: u32,

0 commit comments

Comments
 (0)