gstreamer_sys/
manual.rs
1use glib_sys::gpointer;
2use std::{fmt, mem};
3
4cfg_if::cfg_if! {
5 if #[cfg(target_pointer_width = "64")] {
6 const GST_ID_STR_PADDING_LEN: usize = 8;
7 } else if #[cfg(target_pointer_width = "32")] {
8 const GST_ID_STR_PADDING_LEN: usize = 12;
9 } else {
10 panic!("Only 32 bit and 64 bit pointers supported currently");
11 }
12}
13
14#[derive(Copy, Clone)]
15#[repr(C)]
16pub struct GstIdStr {
17 pointer: gpointer,
19 padding: [u8; GST_ID_STR_PADDING_LEN],
20}
21
22impl fmt::Debug for GstIdStr {
23 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24 use fmt::Write;
25
26 unsafe {
27 let pointer =
28 &*(&self.pointer as *const *mut _ as *const [u8; mem::size_of::<gpointer>()]);
29
30 let is_pretty = f.alternate();
31
32 f.write_str("GstIdStr(")?;
33 if is_pretty {
34 f.write_str("\n ")?;
35 }
36
37 f.write_str("ascii: \"")?;
38 for &b in pointer.iter().chain(self.padding.iter()) {
39 match b {
40 0 => break,
41 c if c.is_ascii() => f.write_char(char::from(b))?,
42 _ => f.write_char('�')?,
43 }
44 }
45
46 if is_pretty {
47 f.write_str("\",\n ")?;
48 } else {
49 f.write_str("\", ")?;
50 }
51
52 f.write_str("hex: ")?;
53 for (i, b) in pointer.iter().chain(self.padding.iter()).enumerate() {
54 if i > 0 {
55 f.write_char(' ')?;
56 }
57 f.write_fmt(format_args!("{b:02x}"))?;
58 }
59
60 if is_pretty {
61 f.write_str(",\n")?;
62 }
63
64 f.write_char(')')
65 }
66 }
67}
68
69#[cfg(test)]
70mod tests {
71 #[cfg(feature = "v1_26")]
72 #[test]
73 fn gstidstr_debug() {
74 unsafe {
75 use std::{ffi::c_char, mem};
76
77 let mut s = mem::MaybeUninit::uninit();
78 crate::gst_id_str_init(s.as_mut_ptr());
79 let mut s = s.assume_init();
80
81 crate::gst_id_str_set(&mut s as *mut _, b"short\0" as *const u8 as *const c_char);
82 assert_eq!(
83 format!("{s:?}"),
84 r#"GstIdStr(ascii: "short", hex: 73 68 6f 72 74 00 00 00 00 00 00 00 00 00 00 00)"#,
85 );
86 assert_eq!(
87 format!("{s:#?}"),
88 r#"GstIdStr(
89 ascii: "short",
90 hex: 73 68 6f 72 74 00 00 00 00 00 00 00 00 00 00 00,
91)"#
92 );
93
94 crate::gst_id_str_set(
95 &mut s as *mut _,
96 b"utf8\xc3\xa7\0" as *const u8 as *const c_char,
97 );
98 assert_eq!(
99 format!("{s:?}"),
100 r#"GstIdStr(ascii: "utf8��", hex: 75 74 66 38 c3 a7 00 00 00 00 00 00 00 00 00 00)"#,
101 );
102 assert_eq!(
103 format!("{s:#?}"),
104 r#"GstIdStr(
105 ascii: "utf8��",
106 hex: 75 74 66 38 c3 a7 00 00 00 00 00 00 00 00 00 00,
107)"#
108 );
109 }
110 }
111}