1use std::{
4 cmp,
5 collections::HashSet,
6 fmt, ops, slice,
7 sync::{LazyLock, Mutex},
8};
9
10use crate::ffi;
11use glib::{prelude::*, translate::*};
12use num_rational::Rational32;
13
14#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
15pub struct Fraction(pub Rational32);
16
17impl Fraction {
18 #[inline]
25 pub fn new(numer: i32, denom: i32) -> Self {
26 skip_assert_initialized!();
27 (numer, denom).into()
28 }
29
30 #[inline]
36 pub const fn new_raw(numer: i32, denom: i32) -> Self {
37 skip_assert_initialized!();
38 Self(Rational32::new_raw(numer, denom))
39 }
40
41 #[inline]
44 pub const fn from_integer(t: i32) -> Self {
45 skip_assert_initialized!();
46 Self::new_raw(t, 1)
47 }
48
49 pub fn approximate_f32(x: f32) -> Option<Self> {
50 skip_assert_initialized!();
51 Rational32::approximate_float(x).map(|r| r.into())
52 }
53
54 pub fn approximate_f64(x: f64) -> Option<Self> {
55 skip_assert_initialized!();
56 Rational32::approximate_float(x).map(|r| r.into())
57 }
58
59 #[inline]
60 pub fn numer(&self) -> i32 {
61 *self.0.numer()
62 }
63
64 #[inline]
65 pub fn denom(&self) -> i32 {
66 *self.0.denom()
67 }
68
69 #[cfg(feature = "v1_24")]
70 #[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
71 #[doc(alias = "gst_util_simplify_fraction")]
72 pub fn simplify(&mut self, n_terms: u32, threshold: u32) {
73 skip_assert_initialized!();
74 unsafe {
75 let mut numer = self.numer();
76 let mut denom = self.denom();
77 ffi::gst_util_simplify_fraction(&mut numer, &mut denom, n_terms, threshold);
78 *self = Self::new(numer, denom);
79 }
80 }
81}
82
83impl fmt::Display for Fraction {
84 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85 self.0.fmt(f)
86 }
87}
88
89impl ops::Deref for Fraction {
90 type Target = Rational32;
91
92 #[inline]
93 fn deref(&self) -> &Self::Target {
94 &self.0
95 }
96}
97
98impl ops::DerefMut for Fraction {
99 #[inline]
100 fn deref_mut(&mut self) -> &mut Rational32 {
101 &mut self.0
102 }
103}
104
105impl AsRef<Rational32> for Fraction {
106 #[inline]
107 fn as_ref(&self) -> &Rational32 {
108 &self.0
109 }
110}
111
112macro_rules! impl_fraction_binop {
113 ($name:ident, $f:ident, $name_assign:ident, $f_assign:ident) => {
114 impl ops::$name<Fraction> for Fraction {
115 type Output = Fraction;
116
117 #[inline]
118 fn $f(self, other: Fraction) -> Self::Output {
119 Fraction((self.0).$f(other.0))
120 }
121 }
122
123 impl ops::$name<Fraction> for &Fraction {
124 type Output = Fraction;
125
126 #[inline]
127 fn $f(self, other: Fraction) -> Self::Output {
128 Fraction((self.0).$f(other.0))
129 }
130 }
131
132 impl ops::$name<&Fraction> for Fraction {
133 type Output = Fraction;
134
135 #[inline]
136 fn $f(self, other: &Fraction) -> Self::Output {
137 Fraction((self.0).$f(other.0))
138 }
139 }
140
141 impl ops::$name<&Fraction> for &Fraction {
142 type Output = Fraction;
143
144 #[inline]
145 fn $f(self, other: &Fraction) -> Self::Output {
146 Fraction((self.0).$f(other.0))
147 }
148 }
149
150 impl ops::$name<i32> for Fraction {
151 type Output = Fraction;
152
153 #[inline]
154 fn $f(self, other: i32) -> Self::Output {
155 self.$f(Fraction::from(other))
156 }
157 }
158
159 impl ops::$name<i32> for &Fraction {
160 type Output = Fraction;
161
162 #[inline]
163 fn $f(self, other: i32) -> Self::Output {
164 self.$f(Fraction::from(other))
165 }
166 }
167
168 impl ops::$name<&i32> for Fraction {
169 type Output = Fraction;
170
171 #[inline]
172 fn $f(self, other: &i32) -> Self::Output {
173 self.$f(Fraction::from(*other))
174 }
175 }
176
177 impl ops::$name<&i32> for &Fraction {
178 type Output = Fraction;
179
180 #[inline]
181 fn $f(self, other: &i32) -> Self::Output {
182 self.$f(Fraction::from(*other))
183 }
184 }
185
186 impl ops::$name<Fraction> for i32 {
187 type Output = Fraction;
188
189 #[inline]
190 fn $f(self, other: Fraction) -> Self::Output {
191 Fraction::from(self).$f(other)
192 }
193 }
194
195 impl ops::$name<&Fraction> for i32 {
196 type Output = Fraction;
197
198 #[inline]
199 fn $f(self, other: &Fraction) -> Self::Output {
200 Fraction::from(self).$f(other)
201 }
202 }
203
204 impl ops::$name<Fraction> for &i32 {
205 type Output = Fraction;
206
207 #[inline]
208 fn $f(self, other: Fraction) -> Self::Output {
209 Fraction::from(*self).$f(other)
210 }
211 }
212
213 impl ops::$name<&Fraction> for &i32 {
214 type Output = Fraction;
215
216 #[inline]
217 fn $f(self, other: &Fraction) -> Self::Output {
218 Fraction::from(*self).$f(other)
219 }
220 }
221
222 impl ops::$name_assign<Fraction> for Fraction {
223 #[inline]
224 fn $f_assign(&mut self, other: Fraction) {
225 (self.0).$f_assign(other.0)
226 }
227 }
228
229 impl ops::$name_assign<&Fraction> for Fraction {
230 #[inline]
231 fn $f_assign(&mut self, other: &Fraction) {
232 (self.0).$f_assign(other.0)
233 }
234 }
235
236 impl ops::$name_assign<i32> for Fraction {
237 #[inline]
238 fn $f_assign(&mut self, other: i32) {
239 (self.0).$f_assign(other)
240 }
241 }
242
243 impl ops::$name_assign<&i32> for Fraction {
244 #[inline]
245 fn $f_assign(&mut self, other: &i32) {
246 (self.0).$f_assign(other)
247 }
248 }
249 };
250}
251
252impl_fraction_binop!(Add, add, AddAssign, add_assign);
253impl_fraction_binop!(Sub, sub, SubAssign, sub_assign);
254impl_fraction_binop!(Div, div, DivAssign, div_assign);
255impl_fraction_binop!(Mul, mul, MulAssign, mul_assign);
256impl_fraction_binop!(Rem, rem, RemAssign, rem_assign);
257
258impl ops::Neg for Fraction {
259 type Output = Fraction;
260
261 #[inline]
262 fn neg(self) -> Self::Output {
263 Fraction(self.0.neg())
264 }
265}
266
267impl ops::Neg for &Fraction {
268 type Output = Fraction;
269
270 #[inline]
271 fn neg(self) -> Self::Output {
272 Fraction(self.0.neg())
273 }
274}
275
276impl From<i32> for Fraction {
277 #[inline]
278 fn from(x: i32) -> Self {
279 skip_assert_initialized!();
280 Fraction(x.into())
281 }
282}
283
284impl From<(i32, i32)> for Fraction {
285 #[inline]
286 fn from(x: (i32, i32)) -> Self {
287 skip_assert_initialized!();
288 Fraction(x.into())
289 }
290}
291
292impl From<Fraction> for (i32, i32) {
293 #[inline]
294 fn from(f: Fraction) -> Self {
295 skip_assert_initialized!();
296 f.0.into()
297 }
298}
299
300impl From<Rational32> for Fraction {
301 #[inline]
302 fn from(x: Rational32) -> Self {
303 skip_assert_initialized!();
304 Fraction(x)
305 }
306}
307
308impl From<Fraction> for Rational32 {
309 #[inline]
310 fn from(x: Fraction) -> Self {
311 skip_assert_initialized!();
312 x.0
313 }
314}
315
316impl glib::types::StaticType for Fraction {
317 #[inline]
318 fn static_type() -> glib::types::Type {
319 unsafe { from_glib(ffi::gst_fraction_get_type()) }
320 }
321}
322
323impl glib::value::ValueType for Fraction {
324 type Type = Self;
325}
326
327unsafe impl<'a> glib::value::FromValue<'a> for Fraction {
328 type Checker = glib::value::GenericValueTypeChecker<Self>;
329
330 #[inline]
331 unsafe fn from_value(value: &'a glib::Value) -> Self {
332 unsafe {
333 skip_assert_initialized!();
334 let n = ffi::gst_value_get_fraction_numerator(value.to_glib_none().0);
335 let d = ffi::gst_value_get_fraction_denominator(value.to_glib_none().0);
336
337 Fraction::new(n, d)
338 }
339 }
340}
341
342impl glib::value::ToValue for Fraction {
343 #[inline]
344 fn to_value(&self) -> glib::Value {
345 let mut value = glib::Value::for_value_type::<Self>();
346 unsafe {
347 ffi::gst_value_set_fraction(value.to_glib_none_mut().0, self.numer(), self.denom());
348 }
349 value
350 }
351
352 #[inline]
353 fn value_type(&self) -> glib::Type {
354 Self::static_type()
355 }
356}
357
358impl From<Fraction> for glib::Value {
359 #[inline]
360 fn from(v: Fraction) -> glib::Value {
361 skip_assert_initialized!();
362 glib::value::ToValue::to_value(&v)
363 }
364}
365
366#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
367#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
368pub struct IntRange<T> {
369 min: T,
370 max: T,
371 step: T,
372}
373
374impl<T: Copy> IntRange<T> {
375 #[inline]
376 pub fn min(&self) -> T {
377 self.min
378 }
379
380 #[inline]
381 pub fn max(&self) -> T {
382 self.max
383 }
384
385 #[inline]
386 pub fn step(&self) -> T {
387 self.step
388 }
389}
390
391#[doc(hidden)]
392pub trait IntRangeType: Sized + Clone + Copy + 'static {
393 fn with_min_max(min: Self, max: Self) -> IntRange<Self>;
394 fn with_step(min: Self, max: Self, step: Self) -> IntRange<Self>;
395}
396
397impl IntRangeType for i32 {
398 #[inline]
399 fn with_min_max(min: i32, max: i32) -> IntRange<Self> {
400 skip_assert_initialized!();
401 IntRange { min, max, step: 1 }
402 }
403
404 #[inline]
405 fn with_step(min: i32, max: i32, step: i32) -> IntRange<Self> {
406 skip_assert_initialized!();
407
408 assert!(
409 min < max,
410 "maximum value must be greater than minimum value"
411 );
412 assert!(step > 0, "step size must be greater than zero");
413 assert!(
414 min % step == 0,
415 "minimum value must be evenly dividable by step size"
416 );
417 assert!(
418 max % step == 0,
419 "maximum value must be evenly dividable by step size"
420 );
421
422 IntRange { min, max, step }
423 }
424}
425
426impl IntRangeType for i64 {
427 #[inline]
428 fn with_min_max(min: i64, max: i64) -> IntRange<Self> {
429 skip_assert_initialized!();
430 IntRange { min, max, step: 1 }
431 }
432
433 #[inline]
434 fn with_step(min: i64, max: i64, step: i64) -> IntRange<Self> {
435 skip_assert_initialized!();
436
437 assert!(
438 min < max,
439 "maximum value must be greater than minimum value"
440 );
441 assert!(step > 0, "step size must be greater than zero");
442 assert!(
443 min % step == 0,
444 "minimum value must be evenly dividable by step size"
445 );
446 assert!(
447 max % step == 0,
448 "maximum value must be evenly dividable by step size"
449 );
450
451 IntRange { min, max, step }
452 }
453}
454
455impl<T: IntRangeType> IntRange<T> {
456 #[inline]
457 pub fn new(min: T, max: T) -> IntRange<T> {
458 skip_assert_initialized!();
459 T::with_min_max(min, max)
460 }
461
462 #[inline]
463 pub fn with_step(min: T, max: T, step: T) -> IntRange<T> {
464 skip_assert_initialized!();
465 T::with_step(min, max, step)
466 }
467}
468
469impl From<(i32, i32)> for IntRange<i32> {
470 #[inline]
471 fn from((min, max): (i32, i32)) -> Self {
472 skip_assert_initialized!();
473 Self::new(min, max)
474 }
475}
476
477impl From<(i32, i32, i32)> for IntRange<i32> {
478 #[inline]
479 fn from((min, max, step): (i32, i32, i32)) -> Self {
480 skip_assert_initialized!();
481 Self::with_step(min, max, step)
482 }
483}
484
485impl From<(i64, i64)> for IntRange<i64> {
486 #[inline]
487 fn from((min, max): (i64, i64)) -> Self {
488 skip_assert_initialized!();
489 Self::new(min, max)
490 }
491}
492
493impl From<(i64, i64, i64)> for IntRange<i64> {
494 #[inline]
495 fn from((min, max, step): (i64, i64, i64)) -> Self {
496 skip_assert_initialized!();
497 Self::with_step(min, max, step)
498 }
499}
500
501impl glib::types::StaticType for IntRange<i32> {
502 #[inline]
503 fn static_type() -> glib::types::Type {
504 unsafe { from_glib(ffi::gst_int_range_get_type()) }
505 }
506}
507
508impl glib::value::ValueType for IntRange<i32> {
509 type Type = Self;
510}
511
512unsafe impl<'a> glib::value::FromValue<'a> for IntRange<i32> {
513 type Checker = glib::value::GenericValueTypeChecker<Self>;
514
515 #[inline]
516 unsafe fn from_value(value: &'a glib::Value) -> Self {
517 unsafe {
518 skip_assert_initialized!();
519 let min = ffi::gst_value_get_int_range_min(value.to_glib_none().0);
520 let max = ffi::gst_value_get_int_range_max(value.to_glib_none().0);
521 let step = ffi::gst_value_get_int_range_step(value.to_glib_none().0);
522
523 Self::with_step(min, max, step)
524 }
525 }
526}
527
528impl glib::value::ToValue for IntRange<i32> {
529 #[inline]
530 fn to_value(&self) -> glib::Value {
531 let mut value = glib::Value::for_value_type::<Self>();
532 unsafe {
533 ffi::gst_value_set_int_range_step(
534 value.to_glib_none_mut().0,
535 self.min(),
536 self.max(),
537 self.step(),
538 );
539 }
540 value
541 }
542
543 #[inline]
544 fn value_type(&self) -> glib::Type {
545 Self::static_type()
546 }
547}
548
549impl From<IntRange<i32>> for glib::Value {
550 #[inline]
551 fn from(v: IntRange<i32>) -> glib::Value {
552 skip_assert_initialized!();
553 glib::value::ToValue::to_value(&v)
554 }
555}
556
557impl glib::types::StaticType for IntRange<i64> {
558 #[inline]
559 fn static_type() -> glib::types::Type {
560 unsafe { from_glib(ffi::gst_int64_range_get_type()) }
561 }
562}
563
564impl glib::value::ValueType for IntRange<i64> {
565 type Type = Self;
566}
567
568unsafe impl<'a> glib::value::FromValue<'a> for IntRange<i64> {
569 type Checker = glib::value::GenericValueTypeChecker<Self>;
570
571 #[inline]
572 unsafe fn from_value(value: &'a glib::Value) -> Self {
573 unsafe {
574 skip_assert_initialized!();
575 let min = ffi::gst_value_get_int64_range_min(value.to_glib_none().0);
576 let max = ffi::gst_value_get_int64_range_max(value.to_glib_none().0);
577 let step = ffi::gst_value_get_int64_range_step(value.to_glib_none().0);
578
579 Self::with_step(min, max, step)
580 }
581 }
582}
583
584impl glib::value::ToValue for IntRange<i64> {
585 #[inline]
586 fn to_value(&self) -> glib::Value {
587 let mut value = glib::Value::for_value_type::<Self>();
588 unsafe {
589 ffi::gst_value_set_int64_range_step(
590 value.to_glib_none_mut().0,
591 self.min(),
592 self.max(),
593 self.step(),
594 );
595 }
596 value
597 }
598
599 #[inline]
600 fn value_type(&self) -> glib::Type {
601 Self::static_type()
602 }
603}
604
605impl From<IntRange<i64>> for glib::Value {
606 #[inline]
607 fn from(v: IntRange<i64>) -> glib::Value {
608 skip_assert_initialized!();
609 glib::value::ToValue::to_value(&v)
610 }
611}
612
613#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
614#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
615pub struct FractionRange {
616 min: Fraction,
617 max: Fraction,
618}
619
620impl FractionRange {
621 #[inline]
622 pub fn new<T: Into<Fraction>, U: Into<Fraction>>(min: T, max: U) -> Self {
623 skip_assert_initialized!();
624
625 let min = min.into();
626 let max = max.into();
627
628 assert!(min <= max);
629
630 FractionRange { min, max }
631 }
632
633 #[inline]
634 pub fn min(&self) -> Fraction {
635 self.min
636 }
637
638 #[inline]
639 pub fn max(&self) -> Fraction {
640 self.max
641 }
642}
643
644impl From<(Fraction, Fraction)> for FractionRange {
645 #[inline]
646 fn from((min, max): (Fraction, Fraction)) -> Self {
647 skip_assert_initialized!();
648
649 Self::new(min, max)
650 }
651}
652
653impl glib::types::StaticType for FractionRange {
654 #[inline]
655 fn static_type() -> glib::types::Type {
656 unsafe { from_glib(ffi::gst_fraction_range_get_type()) }
657 }
658}
659
660impl glib::value::ValueType for FractionRange {
661 type Type = Self;
662}
663
664unsafe impl<'a> glib::value::FromValue<'a> for FractionRange {
665 type Checker = glib::value::GenericValueTypeChecker<Self>;
666
667 #[inline]
668 unsafe fn from_value(value: &'a glib::Value) -> Self {
669 unsafe {
670 skip_assert_initialized!();
671 let min = ffi::gst_value_get_fraction_range_min(value.to_glib_none().0);
672 let max = ffi::gst_value_get_fraction_range_max(value.to_glib_none().0);
673
674 let min_n = ffi::gst_value_get_fraction_numerator(min);
675 let min_d = ffi::gst_value_get_fraction_denominator(min);
676 let max_n = ffi::gst_value_get_fraction_numerator(max);
677 let max_d = ffi::gst_value_get_fraction_denominator(max);
678
679 Self::new((min_n, min_d), (max_n, max_d))
680 }
681 }
682}
683
684impl glib::value::ToValue for FractionRange {
685 #[inline]
686 fn to_value(&self) -> glib::Value {
687 let mut value = glib::Value::for_value_type::<Self>();
688 unsafe {
689 ffi::gst_value_set_fraction_range_full(
690 value.to_glib_none_mut().0,
691 self.min().numer(),
692 self.min().denom(),
693 self.max().numer(),
694 self.max().denom(),
695 );
696 }
697 value
698 }
699
700 #[inline]
701 fn value_type(&self) -> glib::Type {
702 Self::static_type()
703 }
704}
705
706impl From<FractionRange> for glib::Value {
707 #[inline]
708 fn from(v: FractionRange) -> glib::Value {
709 skip_assert_initialized!();
710 glib::value::ToValue::to_value(&v)
711 }
712}
713
714#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
715#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
716pub struct Bitmask(pub u64);
717
718impl Bitmask {
719 #[inline]
720 pub fn new(v: u64) -> Self {
721 skip_assert_initialized!();
722 Bitmask(v)
723 }
724}
725
726impl ops::Deref for Bitmask {
727 type Target = u64;
728
729 #[inline]
730 fn deref(&self) -> &u64 {
731 &self.0
732 }
733}
734
735impl ops::DerefMut for Bitmask {
736 #[inline]
737 fn deref_mut(&mut self) -> &mut u64 {
738 &mut self.0
739 }
740}
741
742impl ops::BitAnd for Bitmask {
743 type Output = Self;
744
745 #[inline]
746 fn bitand(self, rhs: Self) -> Self {
747 Bitmask(self.0.bitand(rhs.0))
748 }
749}
750
751impl ops::BitOr for Bitmask {
752 type Output = Self;
753
754 #[inline]
755 fn bitor(self, rhs: Self) -> Self {
756 Bitmask(self.0.bitor(rhs.0))
757 }
758}
759
760impl ops::BitXor for Bitmask {
761 type Output = Self;
762
763 #[inline]
764 fn bitxor(self, rhs: Self) -> Self {
765 Bitmask(self.0.bitxor(rhs.0))
766 }
767}
768
769impl ops::Not for Bitmask {
770 type Output = Self;
771
772 #[inline]
773 fn not(self) -> Self {
774 Bitmask(self.0.not())
775 }
776}
777
778impl From<u64> for Bitmask {
779 #[inline]
780 fn from(v: u64) -> Self {
781 skip_assert_initialized!();
782 Self::new(v)
783 }
784}
785
786impl glib::types::StaticType for Bitmask {
787 #[inline]
788 fn static_type() -> glib::types::Type {
789 unsafe { from_glib(ffi::gst_bitmask_get_type()) }
790 }
791}
792
793impl glib::value::ValueType for Bitmask {
794 type Type = Self;
795}
796
797unsafe impl<'a> glib::value::FromValue<'a> for Bitmask {
798 type Checker = glib::value::GenericValueTypeChecker<Self>;
799
800 #[inline]
801 unsafe fn from_value(value: &'a glib::Value) -> Self {
802 unsafe {
803 skip_assert_initialized!();
804 let v = ffi::gst_value_get_bitmask(value.to_glib_none().0);
805 Self::new(v)
806 }
807 }
808}
809
810impl glib::value::ToValue for Bitmask {
811 #[inline]
812 fn to_value(&self) -> glib::Value {
813 let mut value = glib::Value::for_value_type::<Self>();
814 unsafe {
815 ffi::gst_value_set_bitmask(value.to_glib_none_mut().0, self.0);
816 }
817 value
818 }
819
820 #[inline]
821 fn value_type(&self) -> glib::Type {
822 Self::static_type()
823 }
824}
825
826impl From<Bitmask> for glib::Value {
827 #[inline]
828 fn from(v: Bitmask) -> glib::Value {
829 skip_assert_initialized!();
830 glib::value::ToValue::to_value(&v)
831 }
832}
833
834#[derive(Clone)]
835pub struct Array(glib::SendValue);
836
837unsafe impl Send for Array {}
838unsafe impl Sync for Array {}
839
840impl fmt::Debug for Array {
841 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
842 f.debug_tuple("Array").field(&self.as_slice()).finish()
843 }
844}
845
846impl Array {
847 pub fn new<T: Into<glib::Value> + Send>(values: impl IntoIterator<Item = T>) -> Self {
848 assert_initialized_main_thread!();
849
850 unsafe {
851 let mut value = glib::Value::for_value_type::<Array>();
852 for v in values.into_iter() {
853 let mut v = v.into().into_raw();
854 ffi::gst_value_array_append_and_take_value(value.to_glib_none_mut().0, &mut v);
855 }
856
857 Self(glib::SendValue::unsafe_from(value.into_raw()))
858 }
859 }
860
861 pub fn from_values(values: impl IntoIterator<Item = glib::SendValue>) -> Self {
862 skip_assert_initialized!();
863
864 Self::new(values)
865 }
866
867 #[inline]
868 pub fn as_slice(&self) -> &[glib::SendValue] {
869 unsafe {
870 let arr = (*self.0.as_ptr()).data[0].v_pointer as *const glib::ffi::GArray;
871 if arr.is_null() || (*arr).len == 0 {
872 &[]
873 } else {
874 #[allow(clippy::cast_ptr_alignment)]
875 slice::from_raw_parts((*arr).data as *const glib::SendValue, (*arr).len as usize)
876 }
877 }
878 }
879
880 pub fn append_value(&mut self, value: glib::SendValue) {
881 unsafe {
882 ffi::gst_value_array_append_and_take_value(
883 self.0.to_glib_none_mut().0,
884 &mut value.into_raw(),
885 );
886 }
887 }
888
889 pub fn append(&mut self, value: impl Into<glib::Value> + Send) {
890 self.append_value(glib::SendValue::from_owned(value));
891 }
892}
893
894impl Default for Array {
895 fn default() -> Self {
896 skip_assert_initialized!();
897
898 unsafe {
899 let value = glib::Value::for_value_type::<Array>();
900
901 Self(glib::SendValue::unsafe_from(value.into_raw()))
902 }
903 }
904}
905
906impl ops::Deref for Array {
907 type Target = [glib::SendValue];
908
909 #[inline]
910 fn deref(&self) -> &[glib::SendValue] {
911 self.as_slice()
912 }
913}
914
915impl AsRef<[glib::SendValue]> for Array {
916 #[inline]
917 fn as_ref(&self) -> &[glib::SendValue] {
918 self.as_slice()
919 }
920}
921
922impl std::iter::FromIterator<glib::SendValue> for Array {
923 fn from_iter<T: IntoIterator<Item = glib::SendValue>>(iter: T) -> Self {
924 skip_assert_initialized!();
925 Self::from_values(iter)
926 }
927}
928
929impl std::iter::Extend<glib::SendValue> for Array {
930 fn extend<T: IntoIterator<Item = glib::SendValue>>(&mut self, iter: T) {
931 for v in iter.into_iter() {
932 self.append_value(v);
933 }
934 }
935}
936
937impl glib::value::ValueType for Array {
938 type Type = Self;
939}
940
941unsafe impl<'a> glib::value::FromValue<'a> for Array {
942 type Checker = glib::value::GenericValueTypeChecker<Self>;
943
944 unsafe fn from_value(value: &'a glib::Value) -> Self {
945 unsafe {
946 skip_assert_initialized!();
947 Self(glib::SendValue::unsafe_from(value.clone().into_raw()))
948 }
949 }
950}
951
952impl glib::value::ToValue for Array {
953 fn to_value(&self) -> glib::Value {
954 self.0.clone().into()
955 }
956
957 fn value_type(&self) -> glib::Type {
958 Self::static_type()
959 }
960}
961
962impl From<Array> for glib::Value {
963 fn from(v: Array) -> glib::Value {
964 skip_assert_initialized!();
965 v.0.into()
966 }
967}
968
969impl glib::types::StaticType for Array {
970 #[inline]
971 fn static_type() -> glib::types::Type {
972 unsafe { from_glib(ffi::gst_value_array_get_type()) }
973 }
974}
975
976#[derive(Debug, Clone)]
977pub struct ArrayRef<'a>(&'a [glib::SendValue]);
978
979unsafe impl Send for ArrayRef<'_> {}
980unsafe impl Sync for ArrayRef<'_> {}
981
982impl<'a> ArrayRef<'a> {
983 pub fn new(values: &'a [glib::SendValue]) -> Self {
984 skip_assert_initialized!();
985
986 Self(values)
987 }
988
989 #[inline]
990 pub fn as_slice(&self) -> &'a [glib::SendValue] {
991 self.0
992 }
993}
994
995impl ops::Deref for ArrayRef<'_> {
996 type Target = [glib::SendValue];
997
998 #[inline]
999 fn deref(&self) -> &[glib::SendValue] {
1000 self.as_slice()
1001 }
1002}
1003
1004impl AsRef<[glib::SendValue]> for ArrayRef<'_> {
1005 #[inline]
1006 fn as_ref(&self) -> &[glib::SendValue] {
1007 self.as_slice()
1008 }
1009}
1010
1011unsafe impl<'a> glib::value::FromValue<'a> for ArrayRef<'a> {
1012 type Checker = glib::value::GenericValueTypeChecker<Self>;
1013
1014 #[inline]
1015 unsafe fn from_value(value: &'a glib::Value) -> Self {
1016 unsafe {
1017 skip_assert_initialized!();
1018 let arr = (*value.as_ptr()).data[0].v_pointer as *const glib::ffi::GArray;
1019 if arr.is_null() || (*arr).len == 0 {
1020 Self(&[])
1021 } else {
1022 #[allow(clippy::cast_ptr_alignment)]
1023 Self(slice::from_raw_parts(
1024 (*arr).data as *const glib::SendValue,
1025 (*arr).len as usize,
1026 ))
1027 }
1028 }
1029 }
1030}
1031
1032impl glib::value::ToValue for ArrayRef<'_> {
1033 #[inline]
1034 fn to_value(&self) -> glib::Value {
1035 let mut value = glib::Value::for_value_type::<Array>();
1036 unsafe {
1037 for v in self.0 {
1038 ffi::gst_value_array_append_value(value.to_glib_none_mut().0, v.to_glib_none().0);
1039 }
1040 }
1041 value
1042 }
1043
1044 #[inline]
1045 fn value_type(&self) -> glib::Type {
1046 Self::static_type()
1047 }
1048}
1049
1050impl<'a> From<ArrayRef<'a>> for glib::Value {
1051 #[inline]
1052 fn from(v: ArrayRef<'a>) -> glib::Value {
1053 skip_assert_initialized!();
1054 glib::value::ToValue::to_value(&v)
1055 }
1056}
1057
1058impl glib::types::StaticType for ArrayRef<'_> {
1059 #[inline]
1060 fn static_type() -> glib::types::Type {
1061 unsafe { from_glib(ffi::gst_value_array_get_type()) }
1062 }
1063}
1064
1065#[derive(Clone)]
1066pub struct List(glib::SendValue);
1067
1068unsafe impl Send for List {}
1069unsafe impl Sync for List {}
1070
1071impl fmt::Debug for List {
1072 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1073 f.debug_tuple("List").field(&self.as_slice()).finish()
1074 }
1075}
1076
1077impl List {
1078 pub fn new<T: Into<glib::Value> + Send>(values: impl IntoIterator<Item = T>) -> Self {
1079 assert_initialized_main_thread!();
1080
1081 unsafe {
1082 let mut value = glib::Value::for_value_type::<List>();
1083 for v in values.into_iter() {
1084 let mut v = v.into().into_raw();
1085 ffi::gst_value_list_append_and_take_value(value.to_glib_none_mut().0, &mut v);
1086 }
1087
1088 Self(glib::SendValue::unsafe_from(value.into_raw()))
1089 }
1090 }
1091
1092 pub fn from_values(values: impl IntoIterator<Item = glib::SendValue>) -> Self {
1093 skip_assert_initialized!();
1094
1095 Self::new(values)
1096 }
1097
1098 #[inline]
1099 pub fn as_slice(&self) -> &[glib::SendValue] {
1100 unsafe {
1101 let arr = (*self.0.as_ptr()).data[0].v_pointer as *const glib::ffi::GArray;
1102 if arr.is_null() || (*arr).len == 0 {
1103 &[]
1104 } else {
1105 #[allow(clippy::cast_ptr_alignment)]
1106 slice::from_raw_parts((*arr).data as *const glib::SendValue, (*arr).len as usize)
1107 }
1108 }
1109 }
1110
1111 pub fn append_value(&mut self, value: glib::SendValue) {
1112 unsafe {
1113 ffi::gst_value_list_append_and_take_value(
1114 self.0.to_glib_none_mut().0,
1115 &mut value.into_raw(),
1116 );
1117 }
1118 }
1119
1120 pub fn append(&mut self, value: impl Into<glib::Value> + Send) {
1121 self.append_value(glib::SendValue::from_owned(value));
1122 }
1123}
1124
1125impl Default for List {
1126 fn default() -> Self {
1127 skip_assert_initialized!();
1128
1129 unsafe {
1130 let value = glib::Value::for_value_type::<List>();
1131
1132 Self(glib::SendValue::unsafe_from(value.into_raw()))
1133 }
1134 }
1135}
1136
1137impl ops::Deref for List {
1138 type Target = [glib::SendValue];
1139
1140 #[inline]
1141 fn deref(&self) -> &[glib::SendValue] {
1142 self.as_slice()
1143 }
1144}
1145
1146impl AsRef<[glib::SendValue]> for List {
1147 #[inline]
1148 fn as_ref(&self) -> &[glib::SendValue] {
1149 self.as_slice()
1150 }
1151}
1152
1153impl std::iter::FromIterator<glib::SendValue> for List {
1154 fn from_iter<T: IntoIterator<Item = glib::SendValue>>(iter: T) -> Self {
1155 skip_assert_initialized!();
1156 Self::from_values(iter)
1157 }
1158}
1159
1160impl std::iter::Extend<glib::SendValue> for List {
1161 fn extend<T: IntoIterator<Item = glib::SendValue>>(&mut self, iter: T) {
1162 for v in iter.into_iter() {
1163 self.append_value(v);
1164 }
1165 }
1166}
1167
1168impl glib::value::ValueType for List {
1169 type Type = Self;
1170}
1171
1172unsafe impl<'a> glib::value::FromValue<'a> for List {
1173 type Checker = glib::value::GenericValueTypeChecker<Self>;
1174
1175 unsafe fn from_value(value: &'a glib::Value) -> Self {
1176 unsafe {
1177 skip_assert_initialized!();
1178 Self(glib::SendValue::unsafe_from(value.clone().into_raw()))
1179 }
1180 }
1181}
1182
1183impl glib::value::ToValue for List {
1184 fn to_value(&self) -> glib::Value {
1185 self.0.clone().into()
1186 }
1187
1188 fn value_type(&self) -> glib::Type {
1189 Self::static_type()
1190 }
1191}
1192
1193impl From<List> for glib::Value {
1194 fn from(v: List) -> glib::Value {
1195 skip_assert_initialized!();
1196 v.0.into()
1197 }
1198}
1199
1200impl glib::types::StaticType for List {
1201 #[inline]
1202 fn static_type() -> glib::types::Type {
1203 unsafe { from_glib(ffi::gst_value_list_get_type()) }
1204 }
1205}
1206
1207#[derive(Debug, Clone)]
1208pub struct ListRef<'a>(&'a [glib::SendValue]);
1209
1210unsafe impl Send for ListRef<'_> {}
1211unsafe impl Sync for ListRef<'_> {}
1212
1213impl<'a> ListRef<'a> {
1214 pub fn new(values: &'a [glib::SendValue]) -> Self {
1215 skip_assert_initialized!();
1216
1217 Self(values)
1218 }
1219
1220 #[inline]
1221 pub fn as_slice(&self) -> &'a [glib::SendValue] {
1222 self.0
1223 }
1224}
1225
1226impl ops::Deref for ListRef<'_> {
1227 type Target = [glib::SendValue];
1228
1229 #[inline]
1230 fn deref(&self) -> &[glib::SendValue] {
1231 self.as_slice()
1232 }
1233}
1234
1235impl AsRef<[glib::SendValue]> for ListRef<'_> {
1236 #[inline]
1237 fn as_ref(&self) -> &[glib::SendValue] {
1238 self.as_slice()
1239 }
1240}
1241
1242unsafe impl<'a> glib::value::FromValue<'a> for ListRef<'a> {
1243 type Checker = glib::value::GenericValueTypeChecker<Self>;
1244
1245 #[inline]
1246 unsafe fn from_value(value: &'a glib::Value) -> Self {
1247 unsafe {
1248 skip_assert_initialized!();
1249 let arr = (*value.as_ptr()).data[0].v_pointer as *const glib::ffi::GArray;
1250 if arr.is_null() || (*arr).len == 0 {
1251 Self(&[])
1252 } else {
1253 #[allow(clippy::cast_ptr_alignment)]
1254 Self(slice::from_raw_parts(
1255 (*arr).data as *const glib::SendValue,
1256 (*arr).len as usize,
1257 ))
1258 }
1259 }
1260 }
1261}
1262
1263impl glib::value::ToValue for ListRef<'_> {
1264 #[inline]
1265 fn to_value(&self) -> glib::Value {
1266 let mut value = glib::Value::for_value_type::<List>();
1267 unsafe {
1268 for v in self.0 {
1269 ffi::gst_value_list_append_value(value.to_glib_none_mut().0, v.to_glib_none().0);
1270 }
1271 }
1272 value
1273 }
1274
1275 #[inline]
1276 fn value_type(&self) -> glib::Type {
1277 Self::static_type()
1278 }
1279}
1280
1281impl<'a> From<ListRef<'a>> for glib::Value {
1282 #[inline]
1283 fn from(v: ListRef<'a>) -> glib::Value {
1284 skip_assert_initialized!();
1285 glib::value::ToValue::to_value(&v)
1286 }
1287}
1288
1289impl glib::types::StaticType for ListRef<'_> {
1290 #[inline]
1291 fn static_type() -> glib::types::Type {
1292 unsafe { from_glib(ffi::gst_value_list_get_type()) }
1293 }
1294}
1295
1296#[cfg(feature = "v1_28")]
1297#[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
1298mod unique_list {
1299 use std::mem;
1300
1301 use super::*;
1302
1303 #[derive(Clone)]
1304 pub struct UniqueList(glib::SendValue);
1305
1306 unsafe impl Send for UniqueList {}
1307 unsafe impl Sync for UniqueList {}
1308
1309 impl fmt::Debug for UniqueList {
1310 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1311 f.debug_tuple("UniqueList").field(&self.as_slice()).finish()
1312 }
1313 }
1314
1315 impl UniqueList {
1316 pub fn new<T: Into<glib::Value> + Send>(values: impl IntoIterator<Item = T>) -> Self {
1317 assert_initialized_main_thread!();
1318
1319 unsafe {
1320 let mut value = glib::Value::for_value_type::<UniqueList>();
1321 for v in values.into_iter() {
1322 let mut v = v.into().into_raw();
1323 ffi::gst_value_unique_list_append_and_take_value(
1324 value.to_glib_none_mut().0,
1325 &mut v,
1326 );
1327 }
1328
1329 Self(glib::SendValue::unsafe_from(value.into_raw()))
1330 }
1331 }
1332
1333 pub fn from_values(values: impl IntoIterator<Item = glib::SendValue>) -> Self {
1334 skip_assert_initialized!();
1335
1336 Self::new(values)
1337 }
1338
1339 #[inline]
1340 pub fn as_slice(&self) -> &[glib::SendValue] {
1341 unsafe {
1342 let arr = (*self.0.as_ptr()).data[0].v_pointer as *const glib::ffi::GArray;
1343 if arr.is_null() || (*arr).len == 0 {
1344 &[]
1345 } else {
1346 #[allow(clippy::cast_ptr_alignment)]
1347 slice::from_raw_parts(
1348 (*arr).data as *const glib::SendValue,
1349 (*arr).len as usize,
1350 )
1351 }
1352 }
1353 }
1354
1355 pub fn append_value(&mut self, value: glib::SendValue) {
1356 unsafe {
1357 ffi::gst_value_unique_list_append_and_take_value(
1358 self.0.to_glib_none_mut().0,
1359 &mut value.into_raw(),
1360 );
1361 }
1362 }
1363
1364 pub fn append(&mut self, value: impl Into<glib::Value> + Send) {
1365 self.append_value(glib::SendValue::from_owned(value));
1366 }
1367
1368 #[doc(alias = "gst_value_list_concat")]
1369 pub fn concat(a: &UniqueList, b: &UniqueList) -> UniqueList {
1370 skip_assert_initialized!();
1371
1372 unsafe {
1373 let mut res = mem::MaybeUninit::zeroed();
1374 ffi::gst_value_list_concat(
1375 res.as_mut_ptr(),
1376 a.0.to_glib_none().0,
1377 b.0.to_glib_none().0,
1378 );
1379 UniqueList(glib::SendValue::unsafe_from(res.assume_init()))
1380 }
1381 }
1382 }
1383
1384 impl Default for UniqueList {
1385 fn default() -> Self {
1386 skip_assert_initialized!();
1387
1388 unsafe {
1389 let value = glib::Value::for_value_type::<UniqueList>();
1390
1391 Self(glib::SendValue::unsafe_from(value.into_raw()))
1392 }
1393 }
1394 }
1395
1396 impl ops::Deref for UniqueList {
1397 type Target = [glib::SendValue];
1398
1399 #[inline]
1400 fn deref(&self) -> &[glib::SendValue] {
1401 self.as_slice()
1402 }
1403 }
1404
1405 impl AsRef<[glib::SendValue]> for UniqueList {
1406 #[inline]
1407 fn as_ref(&self) -> &[glib::SendValue] {
1408 self.as_slice()
1409 }
1410 }
1411
1412 impl std::iter::FromIterator<glib::SendValue> for UniqueList {
1413 fn from_iter<T: IntoIterator<Item = glib::SendValue>>(iter: T) -> Self {
1414 skip_assert_initialized!();
1415 Self::from_values(iter)
1416 }
1417 }
1418
1419 impl std::iter::Extend<glib::SendValue> for UniqueList {
1420 fn extend<T: IntoIterator<Item = glib::SendValue>>(&mut self, iter: T) {
1421 for v in iter.into_iter() {
1422 self.append_value(v);
1423 }
1424 }
1425 }
1426
1427 impl glib::value::ValueType for UniqueList {
1428 type Type = Self;
1429 }
1430
1431 unsafe impl<'a> glib::value::FromValue<'a> for UniqueList {
1432 type Checker = glib::value::GenericValueTypeChecker<Self>;
1433
1434 unsafe fn from_value(value: &'a glib::Value) -> Self {
1435 unsafe {
1436 skip_assert_initialized!();
1437 Self(glib::SendValue::unsafe_from(value.clone().into_raw()))
1438 }
1439 }
1440 }
1441
1442 impl glib::value::ToValue for UniqueList {
1443 fn to_value(&self) -> glib::Value {
1444 self.0.clone().into()
1445 }
1446
1447 fn value_type(&self) -> glib::Type {
1448 Self::static_type()
1449 }
1450 }
1451
1452 impl From<UniqueList> for glib::Value {
1453 fn from(v: UniqueList) -> glib::Value {
1454 skip_assert_initialized!();
1455 v.0.into()
1456 }
1457 }
1458
1459 impl glib::types::StaticType for UniqueList {
1460 #[inline]
1461 fn static_type() -> glib::types::Type {
1462 unsafe { from_glib(ffi::gst_value_unique_list_get_type()) }
1463 }
1464 }
1465
1466 #[derive(Debug, Clone)]
1467 pub struct UniqueListRef<'a>(&'a [glib::SendValue]);
1468
1469 unsafe impl Send for UniqueListRef<'_> {}
1470 unsafe impl Sync for UniqueListRef<'_> {}
1471
1472 impl<'a> UniqueListRef<'a> {
1473 pub fn new(values: &'a [glib::SendValue]) -> Self {
1474 skip_assert_initialized!();
1475
1476 Self(values)
1477 }
1478
1479 #[inline]
1480 pub fn as_slice(&self) -> &'a [glib::SendValue] {
1481 self.0
1482 }
1483 }
1484
1485 impl ops::Deref for UniqueListRef<'_> {
1486 type Target = [glib::SendValue];
1487
1488 #[inline]
1489 fn deref(&self) -> &[glib::SendValue] {
1490 self.as_slice()
1491 }
1492 }
1493
1494 impl AsRef<[glib::SendValue]> for UniqueListRef<'_> {
1495 #[inline]
1496 fn as_ref(&self) -> &[glib::SendValue] {
1497 self.as_slice()
1498 }
1499 }
1500
1501 unsafe impl<'a> glib::value::FromValue<'a> for UniqueListRef<'a> {
1502 type Checker = glib::value::GenericValueTypeChecker<Self>;
1503
1504 #[inline]
1505 unsafe fn from_value(value: &'a glib::Value) -> Self {
1506 unsafe {
1507 skip_assert_initialized!();
1508 let arr = (*value.as_ptr()).data[0].v_pointer as *const glib::ffi::GArray;
1509 if arr.is_null() || (*arr).len == 0 {
1510 Self(&[])
1511 } else {
1512 #[allow(clippy::cast_ptr_alignment)]
1513 Self(slice::from_raw_parts(
1514 (*arr).data as *const glib::SendValue,
1515 (*arr).len as usize,
1516 ))
1517 }
1518 }
1519 }
1520 }
1521
1522 impl glib::value::ToValue for UniqueListRef<'_> {
1523 #[inline]
1524 fn to_value(&self) -> glib::Value {
1525 let mut value = glib::Value::for_value_type::<UniqueList>();
1526 unsafe {
1527 for v in self.0 {
1528 ffi::gst_value_unique_list_append_value(
1529 value.to_glib_none_mut().0,
1530 v.to_glib_none().0,
1531 );
1532 }
1533 }
1534 value
1535 }
1536
1537 #[inline]
1538 fn value_type(&self) -> glib::Type {
1539 Self::static_type()
1540 }
1541 }
1542
1543 impl<'a> From<UniqueListRef<'a>> for glib::Value {
1544 #[inline]
1545 fn from(v: UniqueListRef<'a>) -> glib::Value {
1546 skip_assert_initialized!();
1547 glib::value::ToValue::to_value(&v)
1548 }
1549 }
1550
1551 impl glib::types::StaticType for UniqueListRef<'_> {
1552 #[inline]
1553 fn static_type() -> glib::types::Type {
1554 unsafe { from_glib(ffi::gst_value_unique_list_get_type()) }
1555 }
1556 }
1557}
1558
1559#[cfg(feature = "v1_28")]
1560#[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
1561pub use unique_list::*;
1562
1563pub trait GstValueExt: Sized {
1564 #[doc(alias = "gst_value_can_compare")]
1565 fn can_compare(&self, other: &Self) -> bool;
1566 #[doc(alias = "gst_value_compare")]
1567 fn compare(&self, other: &Self) -> Option<cmp::Ordering>;
1568 fn eq(&self, other: &Self) -> bool;
1569 #[doc(alias = "gst_value_can_intersect")]
1570 fn can_intersect(&self, other: &Self) -> bool;
1571 #[doc(alias = "gst_value_intersect")]
1572 fn intersect(&self, other: &Self) -> Option<Self>;
1573 #[doc(alias = "gst_value_can_subtract")]
1574 fn can_subtract(&self, other: &Self) -> bool;
1575 #[doc(alias = "gst_value_subtract")]
1576 fn subtract(&self, other: &Self) -> Option<Self>;
1577 #[doc(alias = "gst_value_can_union")]
1578 fn can_union(&self, other: &Self) -> bool;
1579 #[doc(alias = "gst_value_union")]
1580 fn union(&self, other: &Self) -> Option<Self>;
1581 #[doc(alias = "gst_value_fixate")]
1582 fn fixate(&self) -> Option<Self>;
1583 #[doc(alias = "gst_value_is_fixed")]
1584 fn is_fixed(&self) -> bool;
1585 #[doc(alias = "gst_value_is_subset")]
1586 fn is_subset(&self, superset: &Self) -> bool;
1587 #[doc(alias = "gst_value_serialize")]
1588 fn serialize(&self) -> Result<glib::GString, glib::BoolError>;
1589 #[doc(alias = "gst_value_deserialize")]
1590 fn deserialize(s: &str, type_: glib::Type) -> Result<glib::Value, glib::BoolError>;
1591 #[cfg(feature = "v1_20")]
1592 #[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
1593 #[doc(alias = "gst_value_deserialize_with_pspec")]
1594 fn deserialize_with_pspec(
1595 s: &str,
1596 pspec: &glib::ParamSpec,
1597 ) -> Result<glib::Value, glib::BoolError>;
1598 #[cfg(feature = "v1_28")]
1599 #[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
1600 #[doc(alias = "gst_value_hash")]
1601 fn hash(&self) -> Result<u32, glib::BoolError>;
1602}
1603
1604impl GstValueExt for glib::Value {
1605 fn can_compare(&self, other: &Self) -> bool {
1606 unsafe {
1607 from_glib(ffi::gst_value_can_compare(
1608 self.to_glib_none().0,
1609 other.to_glib_none().0,
1610 ))
1611 }
1612 }
1613
1614 fn compare(&self, other: &Self) -> Option<cmp::Ordering> {
1615 unsafe {
1616 let val = ffi::gst_value_compare(self.to_glib_none().0, other.to_glib_none().0);
1617
1618 match val {
1619 ffi::GST_VALUE_LESS_THAN => Some(cmp::Ordering::Less),
1620 ffi::GST_VALUE_EQUAL => Some(cmp::Ordering::Equal),
1621 ffi::GST_VALUE_GREATER_THAN => Some(cmp::Ordering::Greater),
1622 _ => None,
1623 }
1624 }
1625 }
1626
1627 fn eq(&self, other: &Self) -> bool {
1628 self.compare(other) == Some(cmp::Ordering::Equal)
1629 }
1630
1631 fn can_intersect(&self, other: &Self) -> bool {
1632 unsafe {
1633 from_glib(ffi::gst_value_can_intersect(
1634 self.to_glib_none().0,
1635 other.to_glib_none().0,
1636 ))
1637 }
1638 }
1639
1640 fn intersect(&self, other: &Self) -> Option<Self> {
1641 unsafe {
1642 let mut value = glib::Value::uninitialized();
1643 let ret: bool = from_glib(ffi::gst_value_intersect(
1644 value.to_glib_none_mut().0,
1645 self.to_glib_none().0,
1646 other.to_glib_none().0,
1647 ));
1648 if ret { Some(value) } else { None }
1649 }
1650 }
1651
1652 fn can_subtract(&self, other: &Self) -> bool {
1653 unsafe {
1654 from_glib(ffi::gst_value_can_subtract(
1655 self.to_glib_none().0,
1656 other.to_glib_none().0,
1657 ))
1658 }
1659 }
1660
1661 fn subtract(&self, other: &Self) -> Option<Self> {
1662 unsafe {
1663 let mut value = glib::Value::uninitialized();
1664 let ret: bool = from_glib(ffi::gst_value_subtract(
1665 value.to_glib_none_mut().0,
1666 self.to_glib_none().0,
1667 other.to_glib_none().0,
1668 ));
1669 if ret { Some(value) } else { None }
1670 }
1671 }
1672
1673 fn can_union(&self, other: &Self) -> bool {
1674 unsafe {
1675 from_glib(ffi::gst_value_can_union(
1676 self.to_glib_none().0,
1677 other.to_glib_none().0,
1678 ))
1679 }
1680 }
1681
1682 fn union(&self, other: &Self) -> Option<Self> {
1683 unsafe {
1684 let mut value = glib::Value::uninitialized();
1685 let ret: bool = from_glib(ffi::gst_value_union(
1686 value.to_glib_none_mut().0,
1687 self.to_glib_none().0,
1688 other.to_glib_none().0,
1689 ));
1690 if ret { Some(value) } else { None }
1691 }
1692 }
1693
1694 fn fixate(&self) -> Option<Self> {
1695 unsafe {
1696 let mut value = glib::Value::uninitialized();
1697 let ret: bool = from_glib(ffi::gst_value_fixate(
1698 value.to_glib_none_mut().0,
1699 self.to_glib_none().0,
1700 ));
1701 if ret { Some(value) } else { None }
1702 }
1703 }
1704
1705 fn is_fixed(&self) -> bool {
1706 unsafe { from_glib(ffi::gst_value_is_fixed(self.to_glib_none().0)) }
1707 }
1708
1709 fn is_subset(&self, superset: &Self) -> bool {
1710 unsafe {
1711 from_glib(ffi::gst_value_is_subset(
1712 self.to_glib_none().0,
1713 superset.to_glib_none().0,
1714 ))
1715 }
1716 }
1717
1718 fn serialize(&self) -> Result<glib::GString, glib::BoolError> {
1719 unsafe {
1720 Option::<_>::from_glib_full(ffi::gst_value_serialize(self.to_glib_none().0))
1721 .ok_or_else(|| glib::bool_error!("Failed to serialize value"))
1722 }
1723 }
1724
1725 fn deserialize(s: &str, type_: glib::Type) -> Result<glib::Value, glib::BoolError> {
1726 skip_assert_initialized!();
1727
1728 unsafe {
1729 let mut value = glib::Value::from_type(type_);
1730 let ret: bool = from_glib(ffi::gst_value_deserialize(
1731 value.to_glib_none_mut().0,
1732 s.to_glib_none().0,
1733 ));
1734 if ret {
1735 Ok(value)
1736 } else {
1737 Err(glib::bool_error!("Failed to deserialize value"))
1738 }
1739 }
1740 }
1741
1742 #[cfg(feature = "v1_20")]
1743 #[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
1744 fn deserialize_with_pspec(
1745 s: &str,
1746 pspec: &glib::ParamSpec,
1747 ) -> Result<glib::Value, glib::BoolError> {
1748 skip_assert_initialized!();
1749
1750 unsafe {
1751 let mut value = glib::Value::from_type_unchecked(pspec.value_type());
1752 let ret: bool = from_glib(ffi::gst_value_deserialize_with_pspec(
1753 value.to_glib_none_mut().0,
1754 s.to_glib_none().0,
1755 pspec.to_glib_none().0,
1756 ));
1757 if ret {
1758 Ok(value)
1759 } else {
1760 Err(glib::bool_error!("Failed to deserialize value"))
1761 }
1762 }
1763 }
1764
1765 #[cfg(feature = "v1_28")]
1766 fn hash(&self) -> Result<u32, glib::BoolError> {
1767 {
1768 unsafe {
1769 let mut hash = 0;
1770 glib::result_from_gboolean!(
1771 gstreamer_sys::gst_value_hash(self.to_glib_none().0, &mut hash),
1772 "Failed to hash {self:?}"
1773 )?;
1774 Ok(hash)
1775 }
1776 }
1777 }
1778}
1779
1780pub trait ValueTableImpl: StaticType + Into<glib::Value> + Send + Sync + 'static {
1787 fn serialize(&self) -> Result<glib::GString, glib::BoolError>;
1790
1791 fn deserialize(s: &str, pspec: Option<&glib::ParamSpec>) -> Result<Self, glib::BoolError>;
1796
1797 fn compare(&self, _other: &Self) -> Option<cmp::Ordering> {
1802 None
1803 }
1804
1805 fn hash(&self) -> Option<u32> {
1809 None
1810 }
1811}
1812
1813#[doc(alias = "gst_value_register")]
1818pub fn value_register<T: ValueTableImpl>()
1819where
1820 for<'a> &'a T: glib::value::FromValue<'a>,
1821{
1822 assert_initialized_main_thread!();
1823
1824 static REGISTERED: LazyLock<Mutex<HashSet<glib::Type>>> =
1825 LazyLock::new(|| Mutex::new(HashSet::new()));
1826
1827 unsafe extern "C" fn serialize<T: ValueTableImpl>(
1828 value: *const glib::gobject_ffi::GValue,
1829 ) -> *mut std::ffi::c_char
1830 where
1831 for<'a> &'a T: glib::value::FromValue<'a>,
1832 {
1833 unsafe {
1834 let value = glib::Value::from_glib_ptr_borrow(value);
1835 match value
1836 .get::<&T>()
1837 .map_err(|err| glib::bool_error!("{err}"))
1838 .and_then(|v| v.serialize())
1839 {
1840 Ok(s) => s.into_glib_ptr(),
1841 Err(err) => {
1842 crate::debug!(crate::CAT_RUST, "Failed to serialize {value:?}: {err}");
1843 std::ptr::null_mut()
1844 }
1845 }
1846 }
1847 }
1848
1849 unsafe extern "C" fn compare<T: ValueTableImpl>(
1850 value1: *const glib::gobject_ffi::GValue,
1851 value2: *const glib::gobject_ffi::GValue,
1852 ) -> std::ffi::c_int
1853 where
1854 for<'a> &'a T: glib::value::FromValue<'a>,
1855 {
1856 unsafe {
1857 let value1 = glib::Value::from_glib_ptr_borrow(value1);
1858 let value2 = glib::Value::from_glib_ptr_borrow(value2);
1859 let (Ok(v1), Ok(v2)) = (value1.get::<&T>(), value2.get::<&T>()) else {
1860 return ffi::GST_VALUE_UNORDERED;
1861 };
1862
1863 match v1.compare(v2) {
1864 Some(cmp::Ordering::Less) => ffi::GST_VALUE_LESS_THAN,
1865 Some(cmp::Ordering::Equal) => ffi::GST_VALUE_EQUAL,
1866 Some(cmp::Ordering::Greater) => ffi::GST_VALUE_GREATER_THAN,
1867 None => ffi::GST_VALUE_UNORDERED,
1868 }
1869 }
1870 }
1871
1872 unsafe extern "C" fn hash<T: ValueTableImpl>(
1873 value: *const glib::gobject_ffi::GValue,
1874 res: *mut std::ffi::c_uint,
1875 ) -> glib::ffi::gboolean
1876 where
1877 for<'a> &'a T: glib::value::FromValue<'a>,
1878 {
1879 unsafe {
1880 let value = glib::Value::from_glib_ptr_borrow(value);
1881 match value.get::<&T>().ok().and_then(|v| v.hash()) {
1882 Some(h) => {
1883 *res = h;
1884 glib::ffi::GTRUE
1885 }
1886 None => glib::ffi::GFALSE,
1887 }
1888 }
1889 }
1890
1891 unsafe extern "C" fn deserialize_with_pspec<T: ValueTableImpl>(
1892 value: *mut glib::gobject_ffi::GValue,
1893 s: *const std::ffi::c_char,
1894 pspec: *mut glib::gobject_ffi::GParamSpec,
1895 ) -> glib::ffi::gboolean {
1896 unsafe {
1897 let value = glib::Value::from_glib_ptr_borrow_mut(value);
1898 let Some(s) = glib::GStr::from_ptr_checked(s) else {
1899 return glib::ffi::GFALSE;
1900 };
1901 let s = s.as_str();
1902 let pspec = (!pspec.is_null()).then(|| glib::ParamSpec::from_glib_borrow(pspec));
1903
1904 match T::deserialize(s, pspec.as_deref()) {
1905 Ok(v) => {
1906 let v = v.into();
1907 if !v.type_().is_a(value.type_()) {
1908 crate::debug!(
1909 crate::CAT_RUST,
1910 "Can't store a {} in a {} value",
1911 v.type_(),
1912 value.type_()
1913 );
1914 return glib::ffi::GFALSE;
1915 }
1916 if v.type_() == value.type_() {
1917 *value = v;
1918 } else {
1919 glib::gobject_ffi::g_value_copy(
1922 v.to_glib_none().0,
1923 value.to_glib_none_mut().0,
1924 );
1925 }
1926 glib::ffi::GTRUE
1927 }
1928 Err(err) => {
1929 crate::debug!(
1930 crate::CAT_RUST,
1931 "Failed to deserialize {s:?} as {}: {err}",
1932 T::static_type()
1933 );
1934 glib::ffi::GFALSE
1935 }
1936 }
1937 }
1938 }
1939
1940 unsafe extern "C" fn deserialize<T: ValueTableImpl>(
1941 value: *mut glib::gobject_ffi::GValue,
1942 s: *const std::ffi::c_char,
1943 ) -> glib::ffi::gboolean {
1944 unsafe { deserialize_with_pspec::<T>(value, s, std::ptr::null_mut()) }
1945 }
1946
1947 let type_ = T::static_type();
1948 let mut registered = REGISTERED.lock().unwrap();
1949 if !registered.insert(type_) {
1950 return;
1951 }
1952
1953 let table = Box::leak(Box::new(ffi::GstValueTable {
1956 type_: type_.into_glib(),
1957 compare: Some(compare::<T>),
1958 serialize: Some(serialize::<T>),
1959 deserialize: Some(deserialize::<T>),
1960 deserialize_with_pspec: Some(deserialize_with_pspec::<T>),
1961 hash: Some(hash::<T>),
1962 _gst_reserved: [std::ptr::null_mut(); 2],
1963 }));
1964 unsafe {
1965 ffi::gst_value_register(table);
1966 }
1967}
1968
1969#[doc(hidden)]
1970#[macro_export]
1971macro_rules! impl_builder_gvalue_extra_setters (
1972 (field) => {
1973 #[inline]
1979 pub fn field_if(self, name: impl $crate::glib::IntoGStr, value: impl Into<$crate::glib::Value> + Send, predicate: bool) -> Self {
1980 if predicate {
1981 self.field(name, value)
1982 } else {
1983 self
1984 }
1985 }
1986
1987 #[inline]
1993 pub fn field_with_static_if(self, name: impl AsRef<$crate::glib::GStr> + 'static, value: impl Into<$crate::glib::Value> + Send, predicate: bool) -> Self {
1994 if predicate {
1995 self.field_with_static(name, value)
1996 } else {
1997 self
1998 }
1999 }
2000
2001 #[inline]
2007 pub fn field_with_id_if(self, name: impl AsRef<$crate::IdStr>, value: impl Into<$crate::glib::Value> + Send, predicate: bool) -> Self {
2008 if predicate {
2009 self.field_with_id(name, value)
2010 } else {
2011 self
2012 }
2013 }
2014
2015 #[inline]
2020 pub fn field_if_some(self, name: impl $crate::glib::IntoGStr, value: Option<impl Into<$crate::glib::Value> + Send>) -> Self {
2021 if let Some(value) = value {
2022 self.field(name, value)
2023 } else {
2024 self
2025 }
2026 }
2027
2028 #[inline]
2033 pub fn field_with_static_if_some(self, name: impl AsRef<$crate::glib::GStr> + 'static, value: Option<impl Into<$crate::glib::Value> + Send>) -> Self {
2034 if let Some(value) = value {
2035 self.field_with_static(name, value)
2036 } else {
2037 self
2038 }
2039 }
2040
2041 #[inline]
2046 pub fn field_with_id_if_some(self, name: impl AsRef<$crate::IdStr>, value: Option<impl Into<$crate::glib::Value> + Send>) -> Self {
2047 if let Some(value) = value {
2048 self.field_with_id(name, value)
2049 } else {
2050 self
2051 }
2052 }
2053
2054 #[inline]
2059 pub fn field_from_iter<
2060 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send,
2061 I: $crate::glib::value::ToSendValue,
2062 >(
2063 self,
2064 name: impl $crate::glib::IntoGStr,
2065 iter: impl IntoIterator<Item = I>,
2066 ) -> Self {
2067 let iter = iter.into_iter().map(|item| item.to_send_value());
2068 self.field(name, V::from_iter(iter))
2069 }
2070
2071 #[inline]
2076 pub fn field_with_static_from_iter<
2077 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send,
2078 I: $crate::glib::value::ToSendValue,
2079 >(
2080 self,
2081 name: impl AsRef<$crate::glib::GStr> + 'static,
2082 iter: impl IntoIterator<Item = I>,
2083 ) -> Self {
2084 let iter = iter.into_iter().map(|item| item.to_send_value());
2085 self.field_with_static(name, V::from_iter(iter))
2086 }
2087
2088 #[inline]
2093 pub fn field_with_id_from_iter<
2094 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send,
2095 I: $crate::glib::value::ToSendValue,
2096 >(
2097 self,
2098 name: impl AsRef<$crate::IdStr>,
2099 iter: impl IntoIterator<Item = I>,
2100 ) -> Self {
2101 let iter = iter.into_iter().map(|item| item.to_send_value());
2102 self.field_with_id(name, V::from_iter(iter))
2103 }
2104
2105 #[inline]
2111 pub fn field_if_not_empty<
2112 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send,
2113 I: $crate::glib::value::ToSendValue,
2114 >(
2115 self,
2116 name: impl $crate::glib::IntoGStr,
2117 iter: impl IntoIterator<Item = I>,
2118 ) -> Self {
2119 let mut iter = iter.into_iter().peekable();
2120 if iter.peek().is_some() {
2121 let iter = iter.map(|item| item.to_send_value());
2122 self.field(name, V::from_iter(iter))
2123 } else {
2124 self
2125 }
2126 }
2127
2128 #[inline]
2134 pub fn field_with_static_if_not_empty<
2135 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send,
2136 I: $crate::glib::value::ToSendValue,
2137 >(
2138 self,
2139 name: impl AsRef<$crate::glib::GStr> + 'static,
2140 iter: impl IntoIterator<Item = I>,
2141 ) -> Self {
2142 let mut iter = iter.into_iter().peekable();
2143 if iter.peek().is_some() {
2144 let iter = iter.map(|item| item.to_send_value());
2145 self.field_with_static(name, V::from_iter(iter))
2146 } else {
2147 self
2148 }
2149 }
2150
2151 #[inline]
2157 pub fn field_with_id_if_not_empty
2158 <V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send,
2159 I: $crate::glib::value::ToSendValue,
2160 >(
2161 self,
2162 name: impl AsRef<IdStr>,
2163 iter: impl IntoIterator<Item = I>,
2164 ) -> Self {
2165 let mut iter = iter.into_iter().peekable();
2166 if iter.peek().is_some() {
2167 let iter = iter.map(|item| item.to_send_value());
2168 self.field_with_id(name, V::from_iter(iter))
2169 } else {
2170 self
2171 }
2172 }
2173 };
2174
2175 (other_field) => {
2176 #[inline]
2182 pub fn other_field_if(self, name: &'a str, value: impl $crate::glib::value::ToSendValue, predicate: bool) -> Self {
2183 if predicate {
2184 self.other_field(name, value)
2185 } else {
2186 self
2187 }
2188 }
2189
2190 #[inline]
2195 pub fn other_field_if_some(self, name: &'a str, value: Option<impl $crate::glib::value::ToSendValue>) -> Self {
2196 if let Some(value) = value {
2197 self.other_field(name, value)
2198 } else {
2199 self
2200 }
2201 }
2202
2203 #[inline]
2208 pub fn other_field_from_iter<
2209 V: $crate::glib::value::ValueType + $crate::glib::value::ToSendValue + FromIterator<$crate::glib::SendValue>,
2210 I: $crate::glib::value::ToSendValue,
2211 >(
2212 self,
2213 name: &'a str,
2214 iter: impl IntoIterator<Item = I>,
2215 ) -> Self {
2216 let iter = iter.into_iter().map(|item| item.to_send_value());
2217 self.other_field(name, V::from_iter(iter))
2218 }
2219
2220 #[inline]
2226 pub fn other_field_if_not_empty<
2227 V: $crate::glib::value::ValueType + $crate::glib::value::ToSendValue + FromIterator<$crate::glib::SendValue>,
2228 I: $crate::glib::value::ToSendValue,
2229 >(
2230 self,
2231 name: &'a str,
2232 iter: impl IntoIterator<Item = I>,
2233 ) -> Self {
2234 let mut iter = iter.into_iter().peekable();
2235 if iter.peek().is_some() {
2236 let iter = iter.map(|item| item.to_send_value());
2237 self.other_field(name, V::from_iter(iter))
2238 } else {
2239 self
2240 }
2241 }
2242 };
2243
2244 (property_and_name) => {
2245 #[inline]
2251 pub fn property_if(self, name: &'a str, value: impl Into<$crate::glib::Value> + 'a, predicate: bool) -> Self {
2252 if predicate {
2253 self.property(name, value)
2254 } else {
2255 self
2256 }
2257 }
2258
2259 #[inline]
2264 pub fn property_if_some(self, name: &'a str, value: Option<impl Into<$crate::glib::Value> + 'a>) -> Self {
2265 if let Some(value) = value {
2266 self.property(name, value)
2267 } else {
2268 self
2269 }
2270 }
2271
2272 #[inline]
2277 pub fn property_from_iter<
2278 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue>,
2279 I: $crate::glib::value::ToSendValue,
2280 >(
2281 self,
2282 name: &'a str,
2283 iter: impl IntoIterator<Item = I>,
2284 ) -> Self {
2285 let iter = iter.into_iter().map(|item| item.to_send_value());
2286 self.property(name, V::from_iter(iter))
2287 }
2288
2289 #[inline]
2295 pub fn property_if_not_empty<
2296 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue>,
2297 I: $crate::glib::value::ToSendValue,
2298 >(
2299 self,
2300 name: &'a str,
2301 iter: impl IntoIterator<Item = I>,
2302 ) -> Self {
2303 let mut iter = iter.into_iter().peekable();
2304 if iter.peek().is_some() {
2305 let iter = iter.map(|item| item.to_send_value());
2306 self.property(name, V::from_iter(iter))
2307 } else {
2308 self
2309 }
2310 }
2311
2312 #[inline]
2318 pub fn property_from_str_if(self, name: &'a str, value: &'a str, predicate: bool) -> Self {
2319 if predicate {
2320 self.property_from_str(name, value)
2321 } else {
2322 self
2323 }
2324 }
2325
2326 #[inline]
2331 pub fn property_from_str_if_some(self, name: &'a str, value: Option<&'a str>) -> Self {
2332 if let Some(value) = value {
2333 self.property_from_str(name, value)
2334 } else {
2335 self
2336 }
2337 }
2338
2339 #[inline]
2342 pub fn name(self, name: impl Into<$crate::glib::GString>) -> Self {
2343 self.property("name", name.into())
2344 }
2345
2346 #[inline]
2352 pub fn name_if(self, name: impl Into<$crate::glib::GString>, predicate: bool) -> Self {
2353 if predicate {
2354 self.name(name)
2355 } else {
2356 self
2357 }
2358 }
2359
2360 #[inline]
2365 pub fn name_if_some(self, name: Option<impl Into<$crate::glib::GString>>) -> Self {
2366 if let Some(name) = name {
2367 self.name(name)
2368 } else {
2369 self
2370 }
2371 }
2372 };
2373);
2374
2375#[cfg(test)]
2376mod tests {
2377 use super::*;
2378
2379 #[test]
2380 fn test_fraction() {
2381 crate::init().unwrap();
2382
2383 let f1 = crate::Fraction::new(1, 2);
2384 let f2 = crate::Fraction::new(2, 3);
2385 let mut f3 = f1 * f2;
2386 let f4 = f1 * f2;
2387 f3 *= f2;
2388 f3 *= f4;
2389
2390 assert_eq!(f3, crate::Fraction::new(2, 27));
2391 }
2392
2393 #[test]
2394 fn test_int_range_constructor() {
2395 crate::init().unwrap();
2396
2397 let _r1 = crate::IntRange::new(1i32, 2i32);
2399 let _r2 = crate::IntRange::with_step(2i64, 10i64, 2i64);
2400 let _r3 = crate::IntRange::with_step(0i64, 6i64, 3i64);
2401 }
2402
2403 #[test]
2404 #[should_panic(expected = "step size must be greater than zero")]
2405 fn test_int_range_constructor_step0() {
2406 crate::init().unwrap();
2407 let _r = crate::IntRange::with_step(0i32, 2i32, 0i32);
2408 }
2409
2410 #[test]
2411 #[should_panic(expected = "maximum value must be greater than minimum value")]
2412 fn test_int_range_constructor_max_min() {
2413 crate::init().unwrap();
2414 let _r = crate::IntRange::with_step(4i32, 2i32, 2i32);
2415 }
2416
2417 #[test]
2418 #[should_panic(expected = "maximum value must be greater than minimum value")]
2419 fn test_int_range_constructor_max_eq_min() {
2420 crate::init().unwrap();
2421 let _r = crate::IntRange::with_step(4i32, 4i32, 2i32);
2422 }
2423
2424 #[test]
2425 #[should_panic(expected = "minimum value must be evenly dividable by step size")]
2426 fn test_int_range_constructor_bad_step() {
2427 crate::init().unwrap();
2428 let _r = crate::IntRange::with_step(1i32, 10i32, 2i32);
2429 }
2430
2431 #[test]
2432 #[should_panic(expected = "maximum value must be evenly dividable by step size")]
2433 fn test_int_range_constructor_bad_step_max() {
2434 crate::init().unwrap();
2435 let _r = crate::IntRange::with_step(0i32, 10i32, 3i32);
2436 }
2437
2438 #[test]
2439 #[should_panic(expected = "step size must be greater than zero")]
2440 fn test_int_range_constructor_step0_i64() {
2441 crate::init().unwrap();
2442 let _r = crate::IntRange::with_step(0i64, 2i64, 0i64);
2443 }
2444
2445 #[test]
2446 #[should_panic(expected = "maximum value must be greater than minimum value")]
2447 fn test_int_range_constructor_max_min_i64() {
2448 crate::init().unwrap();
2449 let _r = crate::IntRange::with_step(4i64, 2i64, 2i64);
2450 }
2451
2452 #[test]
2453 #[should_panic(expected = "maximum value must be greater than minimum value")]
2454 fn test_int_range_constructor_max_eq_min_i64() {
2455 crate::init().unwrap();
2456 let _r = crate::IntRange::with_step(4i64, 4i64, 2i64);
2457 }
2458
2459 #[test]
2460 #[should_panic(expected = "minimum value must be evenly dividable by step size")]
2461 fn test_int_range_constructor_bad_step_i64() {
2462 crate::init().unwrap();
2463 let _r = crate::IntRange::with_step(1i64, 10i64, 2i64);
2464 }
2465 #[test]
2466 #[should_panic(expected = "maximum value must be evenly dividable by step size")]
2467 fn test_int_range_constructor_bad_step_max_i64() {
2468 crate::init().unwrap();
2469 let _r = crate::IntRange::with_step(0i64, 10i64, 3i64);
2470 }
2471
2472 #[test]
2473 fn test_deserialize() {
2474 crate::init().unwrap();
2475
2476 let v = glib::Value::deserialize("123", i32::static_type()).unwrap();
2477 assert_eq!(v.get::<i32>(), Ok(123));
2478 }
2479
2480 mod value_table {
2481 use std::sync::{
2482 Arc,
2483 atomic::{AtomicUsize, Ordering},
2484 };
2485
2486 use glib::subclass::prelude::*;
2487
2488 #[derive(Debug, glib::Boxed)]
2489 #[boxed_type(name = "GstRsValueTableUnordered")]
2490 pub struct Unordered {
2491 pub copies: Arc<AtomicUsize>,
2492 }
2493
2494 impl Clone for Unordered {
2495 fn clone(&self) -> Self {
2496 self.copies.fetch_add(1, Ordering::SeqCst);
2497 Self {
2498 copies: self.copies.clone(),
2499 }
2500 }
2501 }
2502
2503 impl crate::ValueTableImpl for Unordered {
2504 fn serialize(&self) -> Result<glib::GString, glib::BoolError> {
2505 Ok("unordered".into())
2506 }
2507
2508 fn deserialize(
2509 _s: &str,
2510 _pspec: Option<&glib::ParamSpec>,
2511 ) -> Result<Self, glib::BoolError> {
2512 skip_assert_initialized!();
2513 Ok(Self {
2514 copies: Arc::new(AtomicUsize::new(0)),
2515 })
2516 }
2517 }
2518
2519 mod imp {
2520 use super::*;
2521
2522 #[derive(Default)]
2523 pub struct Named(pub std::sync::Mutex<String>);
2524
2525 #[glib::object_subclass]
2526 impl ObjectSubclass for Named {
2527 const NAME: &'static str = "GstRsValueTableNamed";
2528 type Type = super::Named;
2529 }
2530
2531 impl ObjectImpl for Named {}
2532 }
2533
2534 glib::wrapper! {
2535 pub struct Named(ObjectSubclass<imp::Named>);
2536 }
2537
2538 impl Named {
2539 pub fn new(name: &str) -> Self {
2540 skip_assert_initialized!();
2541 let obj: Self = glib::Object::new();
2542 *obj.imp().0.lock().unwrap() = name.to_string();
2543 obj
2544 }
2545
2546 pub fn name(&self) -> String {
2547 self.imp().0.lock().unwrap().clone()
2548 }
2549 }
2550
2551 impl crate::ValueTableImpl for Named {
2552 fn serialize(&self) -> Result<glib::GString, glib::BoolError> {
2553 Ok(self.name().into())
2554 }
2555
2556 fn deserialize(
2557 s: &str,
2558 _pspec: Option<&glib::ParamSpec>,
2559 ) -> Result<Self, glib::BoolError> {
2560 skip_assert_initialized!();
2561 if s == "invalid" {
2562 return Err(glib::bool_error!("invalid name"));
2563 }
2564 Ok(Self::new(s))
2565 }
2566
2567 fn compare(&self, other: &Self) -> Option<std::cmp::Ordering> {
2568 Some(self.name().cmp(&other.name()))
2569 }
2570
2571 fn hash(&self) -> Option<u32> {
2572 Some(self.name().len() as u32)
2573 }
2574 }
2575 }
2576
2577 #[test]
2578 fn test_value_table() {
2579 use value_table::Named;
2580
2581 crate::init().unwrap();
2582
2583 crate::value_register::<Named>();
2584 crate::value_register::<Named>();
2586
2587 let v = glib::Value::deserialize("foo", Named::static_type()).unwrap();
2588 assert_eq!(v.get::<Named>().unwrap().name(), "foo");
2589 assert_eq!(v.serialize().unwrap(), "foo");
2590
2591 assert!(glib::Value::deserialize("invalid", Named::static_type()).is_err());
2592
2593 let s = "test, named=(GstRsValueTableNamed)bar"
2594 .parse::<crate::Structure>()
2595 .unwrap();
2596 assert_eq!(s.get::<Named>("named").unwrap().name(), "bar");
2597 assert_eq!(s.to_string(), "test, named=(GstRsValueTableNamed)bar;");
2598
2599 let a = glib::Value::deserialize("a", Named::static_type()).unwrap();
2601 let a2 = glib::Value::deserialize("a", Named::static_type()).unwrap();
2602 let b = glib::Value::deserialize("b", Named::static_type()).unwrap();
2603 assert!(a.eq(&a2));
2604 assert_eq!(a.compare(&b), Some(std::cmp::Ordering::Less));
2605 assert_eq!(b.compare(&a), Some(std::cmp::Ordering::Greater));
2606 assert_eq!(a.compare(&a), Some(std::cmp::Ordering::Equal));
2607 assert_eq!(a.compare(&42i32.to_value()), None);
2608
2609 let null = glib::Value::from_type(Named::static_type());
2610 assert!(null.serialize().is_err());
2611 assert_eq!(null.compare(&a), None);
2612 assert_eq!(a.compare(&null), None);
2613 assert_eq!(null.compare(&null), None);
2614
2615 assert_eq!(
2616 s,
2617 "test, named=(GstRsValueTableNamed)bar"
2618 .parse::<crate::Structure>()
2619 .unwrap()
2620 );
2621
2622 #[cfg(feature = "v1_28")]
2623 assert_eq!(
2624 GstValueExt::hash(&a).unwrap(),
2625 GstValueExt::hash(&a2).unwrap()
2626 );
2627 }
2628
2629 #[test]
2630 fn test_value_table_unordered() {
2631 use value_table::Unordered;
2632
2633 crate::init().unwrap();
2634 crate::value_register::<Unordered>();
2635
2636 let a = glib::Value::deserialize("unordered", Unordered::static_type()).unwrap();
2637 let b = glib::Value::deserialize("unordered", Unordered::static_type()).unwrap();
2638 assert_eq!(a.compare(&a), None);
2639 assert_eq!(a.compare(&b), None);
2640
2641 #[cfg(feature = "v1_28")]
2642 assert!(GstValueExt::hash(&a).is_err());
2643 }
2644
2645 #[test]
2646 fn test_value_table_no_copies() {
2647 use std::sync::atomic::Ordering;
2648
2649 use value_table::Unordered;
2650
2651 crate::init().unwrap();
2652 crate::value_register::<Unordered>();
2653
2654 let a = glib::Value::deserialize("unordered", Unordered::static_type()).unwrap();
2655 let b = glib::Value::deserialize("unordered", Unordered::static_type()).unwrap();
2656 assert_eq!(a.serialize().unwrap(), "unordered");
2657 assert_eq!(a.compare(&b), None);
2658
2659 assert_eq!(
2660 a.get::<&Unordered>().unwrap().copies.load(Ordering::SeqCst),
2661 0
2662 );
2663 assert_eq!(
2664 b.get::<&Unordered>().unwrap().copies.load(Ordering::SeqCst),
2665 0
2666 );
2667 }
2668}