1use std::{cmp, fmt, ops, slice};
4
5use crate::ffi;
6use glib::{prelude::*, translate::*};
7use num_rational::Rational32;
8
9#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
10pub struct Fraction(pub Rational32);
11
12impl Fraction {
13 #[inline]
20 pub fn new(numer: i32, denom: i32) -> Self {
21 skip_assert_initialized!();
22 (numer, denom).into()
23 }
24
25 #[inline]
31 pub const fn new_raw(numer: i32, denom: i32) -> Self {
32 skip_assert_initialized!();
33 Self(Rational32::new_raw(numer, denom))
34 }
35
36 #[inline]
39 pub const fn from_integer(t: i32) -> Self {
40 skip_assert_initialized!();
41 Self::new_raw(t, 1)
42 }
43
44 pub fn approximate_f32(x: f32) -> Option<Self> {
45 skip_assert_initialized!();
46 Rational32::approximate_float(x).map(|r| r.into())
47 }
48
49 pub fn approximate_f64(x: f64) -> Option<Self> {
50 skip_assert_initialized!();
51 Rational32::approximate_float(x).map(|r| r.into())
52 }
53
54 #[inline]
55 pub fn numer(&self) -> i32 {
56 *self.0.numer()
57 }
58
59 #[inline]
60 pub fn denom(&self) -> i32 {
61 *self.0.denom()
62 }
63
64 #[cfg(feature = "v1_24")]
65 #[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
66 #[doc(alias = "gst_util_simplify_fraction")]
67 pub fn simplify(&mut self, n_terms: u32, threshold: u32) {
68 skip_assert_initialized!();
69 unsafe {
70 let mut numer = self.numer();
71 let mut denom = self.denom();
72 ffi::gst_util_simplify_fraction(&mut numer, &mut denom, n_terms, threshold);
73 *self = Self::new(numer, denom);
74 }
75 }
76}
77
78impl fmt::Display for Fraction {
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80 self.0.fmt(f)
81 }
82}
83
84impl ops::Deref for Fraction {
85 type Target = Rational32;
86
87 #[inline]
88 fn deref(&self) -> &Self::Target {
89 &self.0
90 }
91}
92
93impl ops::DerefMut for Fraction {
94 #[inline]
95 fn deref_mut(&mut self) -> &mut Rational32 {
96 &mut self.0
97 }
98}
99
100impl AsRef<Rational32> for Fraction {
101 #[inline]
102 fn as_ref(&self) -> &Rational32 {
103 &self.0
104 }
105}
106
107macro_rules! impl_fraction_binop {
108 ($name:ident, $f:ident, $name_assign:ident, $f_assign:ident) => {
109 impl ops::$name<Fraction> for Fraction {
110 type Output = Fraction;
111
112 #[inline]
113 fn $f(self, other: Fraction) -> Self::Output {
114 Fraction((self.0).$f(other.0))
115 }
116 }
117
118 impl ops::$name<Fraction> for &Fraction {
119 type Output = Fraction;
120
121 #[inline]
122 fn $f(self, other: Fraction) -> Self::Output {
123 Fraction((self.0).$f(other.0))
124 }
125 }
126
127 impl ops::$name<&Fraction> for Fraction {
128 type Output = Fraction;
129
130 #[inline]
131 fn $f(self, other: &Fraction) -> Self::Output {
132 Fraction((self.0).$f(other.0))
133 }
134 }
135
136 impl ops::$name<&Fraction> for &Fraction {
137 type Output = Fraction;
138
139 #[inline]
140 fn $f(self, other: &Fraction) -> Self::Output {
141 Fraction((self.0).$f(other.0))
142 }
143 }
144
145 impl ops::$name<i32> for Fraction {
146 type Output = Fraction;
147
148 #[inline]
149 fn $f(self, other: i32) -> Self::Output {
150 self.$f(Fraction::from(other))
151 }
152 }
153
154 impl ops::$name<i32> for &Fraction {
155 type Output = Fraction;
156
157 #[inline]
158 fn $f(self, other: i32) -> Self::Output {
159 self.$f(Fraction::from(other))
160 }
161 }
162
163 impl ops::$name<&i32> for Fraction {
164 type Output = Fraction;
165
166 #[inline]
167 fn $f(self, other: &i32) -> Self::Output {
168 self.$f(Fraction::from(*other))
169 }
170 }
171
172 impl ops::$name<&i32> for &Fraction {
173 type Output = Fraction;
174
175 #[inline]
176 fn $f(self, other: &i32) -> Self::Output {
177 self.$f(Fraction::from(*other))
178 }
179 }
180
181 impl ops::$name<Fraction> for i32 {
182 type Output = Fraction;
183
184 #[inline]
185 fn $f(self, other: Fraction) -> Self::Output {
186 Fraction::from(self).$f(other)
187 }
188 }
189
190 impl ops::$name<&Fraction> for i32 {
191 type Output = Fraction;
192
193 #[inline]
194 fn $f(self, other: &Fraction) -> Self::Output {
195 Fraction::from(self).$f(other)
196 }
197 }
198
199 impl ops::$name<Fraction> for &i32 {
200 type Output = Fraction;
201
202 #[inline]
203 fn $f(self, other: Fraction) -> Self::Output {
204 Fraction::from(*self).$f(other)
205 }
206 }
207
208 impl ops::$name<&Fraction> for &i32 {
209 type Output = Fraction;
210
211 #[inline]
212 fn $f(self, other: &Fraction) -> Self::Output {
213 Fraction::from(*self).$f(other)
214 }
215 }
216
217 impl ops::$name_assign<Fraction> for Fraction {
218 #[inline]
219 fn $f_assign(&mut self, other: Fraction) {
220 (self.0).$f_assign(other.0)
221 }
222 }
223
224 impl ops::$name_assign<&Fraction> for Fraction {
225 #[inline]
226 fn $f_assign(&mut self, other: &Fraction) {
227 (self.0).$f_assign(other.0)
228 }
229 }
230
231 impl ops::$name_assign<i32> for Fraction {
232 #[inline]
233 fn $f_assign(&mut self, other: i32) {
234 (self.0).$f_assign(other)
235 }
236 }
237
238 impl ops::$name_assign<&i32> for Fraction {
239 #[inline]
240 fn $f_assign(&mut self, other: &i32) {
241 (self.0).$f_assign(other)
242 }
243 }
244 };
245}
246
247impl_fraction_binop!(Add, add, AddAssign, add_assign);
248impl_fraction_binop!(Sub, sub, SubAssign, sub_assign);
249impl_fraction_binop!(Div, div, DivAssign, div_assign);
250impl_fraction_binop!(Mul, mul, MulAssign, mul_assign);
251impl_fraction_binop!(Rem, rem, RemAssign, rem_assign);
252
253impl ops::Neg for Fraction {
254 type Output = Fraction;
255
256 #[inline]
257 fn neg(self) -> Self::Output {
258 Fraction(self.0.neg())
259 }
260}
261
262impl ops::Neg for &Fraction {
263 type Output = Fraction;
264
265 #[inline]
266 fn neg(self) -> Self::Output {
267 Fraction(self.0.neg())
268 }
269}
270
271impl From<i32> for Fraction {
272 #[inline]
273 fn from(x: i32) -> Self {
274 skip_assert_initialized!();
275 Fraction(x.into())
276 }
277}
278
279impl From<(i32, i32)> for Fraction {
280 #[inline]
281 fn from(x: (i32, i32)) -> Self {
282 skip_assert_initialized!();
283 Fraction(x.into())
284 }
285}
286
287impl From<Fraction> for (i32, i32) {
288 #[inline]
289 fn from(f: Fraction) -> Self {
290 skip_assert_initialized!();
291 f.0.into()
292 }
293}
294
295impl From<Rational32> for Fraction {
296 #[inline]
297 fn from(x: Rational32) -> Self {
298 skip_assert_initialized!();
299 Fraction(x)
300 }
301}
302
303impl From<Fraction> for Rational32 {
304 #[inline]
305 fn from(x: Fraction) -> Self {
306 skip_assert_initialized!();
307 x.0
308 }
309}
310
311impl glib::types::StaticType for Fraction {
312 #[inline]
313 fn static_type() -> glib::types::Type {
314 unsafe { from_glib(ffi::gst_fraction_get_type()) }
315 }
316}
317
318impl glib::value::ValueType for Fraction {
319 type Type = Self;
320}
321
322unsafe impl<'a> glib::value::FromValue<'a> for Fraction {
323 type Checker = glib::value::GenericValueTypeChecker<Self>;
324
325 #[inline]
326 unsafe fn from_value(value: &'a glib::Value) -> Self {
327 unsafe {
328 skip_assert_initialized!();
329 let n = ffi::gst_value_get_fraction_numerator(value.to_glib_none().0);
330 let d = ffi::gst_value_get_fraction_denominator(value.to_glib_none().0);
331
332 Fraction::new(n, d)
333 }
334 }
335}
336
337impl glib::value::ToValue for Fraction {
338 #[inline]
339 fn to_value(&self) -> glib::Value {
340 let mut value = glib::Value::for_value_type::<Self>();
341 unsafe {
342 ffi::gst_value_set_fraction(value.to_glib_none_mut().0, self.numer(), self.denom());
343 }
344 value
345 }
346
347 #[inline]
348 fn value_type(&self) -> glib::Type {
349 Self::static_type()
350 }
351}
352
353impl From<Fraction> for glib::Value {
354 #[inline]
355 fn from(v: Fraction) -> glib::Value {
356 skip_assert_initialized!();
357 glib::value::ToValue::to_value(&v)
358 }
359}
360
361#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
362#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
363pub struct IntRange<T> {
364 min: T,
365 max: T,
366 step: T,
367}
368
369impl<T: Copy> IntRange<T> {
370 #[inline]
371 pub fn min(&self) -> T {
372 self.min
373 }
374
375 #[inline]
376 pub fn max(&self) -> T {
377 self.max
378 }
379
380 #[inline]
381 pub fn step(&self) -> T {
382 self.step
383 }
384}
385
386#[doc(hidden)]
387pub trait IntRangeType: Sized + Clone + Copy + 'static {
388 fn with_min_max(min: Self, max: Self) -> IntRange<Self>;
389 fn with_step(min: Self, max: Self, step: Self) -> IntRange<Self>;
390}
391
392impl IntRangeType for i32 {
393 #[inline]
394 fn with_min_max(min: i32, max: i32) -> IntRange<Self> {
395 skip_assert_initialized!();
396 IntRange { min, max, step: 1 }
397 }
398
399 #[inline]
400 fn with_step(min: i32, max: i32, step: i32) -> IntRange<Self> {
401 skip_assert_initialized!();
402
403 assert!(
404 min < max,
405 "maximum value must be greater than minimum value"
406 );
407 assert!(step > 0, "step size must be greater than zero");
408 assert!(
409 min % step == 0,
410 "minimum value must be evenly dividable by step size"
411 );
412 assert!(
413 max % step == 0,
414 "maximum value must be evenly dividable by step size"
415 );
416
417 IntRange { min, max, step }
418 }
419}
420
421impl IntRangeType for i64 {
422 #[inline]
423 fn with_min_max(min: i64, max: i64) -> IntRange<Self> {
424 skip_assert_initialized!();
425 IntRange { min, max, step: 1 }
426 }
427
428 #[inline]
429 fn with_step(min: i64, max: i64, step: i64) -> IntRange<Self> {
430 skip_assert_initialized!();
431
432 assert!(
433 min < max,
434 "maximum value must be greater than minimum value"
435 );
436 assert!(step > 0, "step size must be greater than zero");
437 assert!(
438 min % step == 0,
439 "minimum value must be evenly dividable by step size"
440 );
441 assert!(
442 max % step == 0,
443 "maximum value must be evenly dividable by step size"
444 );
445
446 IntRange { min, max, step }
447 }
448}
449
450impl<T: IntRangeType> IntRange<T> {
451 #[inline]
452 pub fn new(min: T, max: T) -> IntRange<T> {
453 skip_assert_initialized!();
454 T::with_min_max(min, max)
455 }
456
457 #[inline]
458 pub fn with_step(min: T, max: T, step: T) -> IntRange<T> {
459 skip_assert_initialized!();
460 T::with_step(min, max, step)
461 }
462}
463
464impl From<(i32, i32)> for IntRange<i32> {
465 #[inline]
466 fn from((min, max): (i32, i32)) -> Self {
467 skip_assert_initialized!();
468 Self::new(min, max)
469 }
470}
471
472impl From<(i32, i32, i32)> for IntRange<i32> {
473 #[inline]
474 fn from((min, max, step): (i32, i32, i32)) -> Self {
475 skip_assert_initialized!();
476 Self::with_step(min, max, step)
477 }
478}
479
480impl From<(i64, i64)> for IntRange<i64> {
481 #[inline]
482 fn from((min, max): (i64, i64)) -> Self {
483 skip_assert_initialized!();
484 Self::new(min, max)
485 }
486}
487
488impl From<(i64, i64, i64)> for IntRange<i64> {
489 #[inline]
490 fn from((min, max, step): (i64, i64, i64)) -> Self {
491 skip_assert_initialized!();
492 Self::with_step(min, max, step)
493 }
494}
495
496impl glib::types::StaticType for IntRange<i32> {
497 #[inline]
498 fn static_type() -> glib::types::Type {
499 unsafe { from_glib(ffi::gst_int_range_get_type()) }
500 }
501}
502
503impl glib::value::ValueType for IntRange<i32> {
504 type Type = Self;
505}
506
507unsafe impl<'a> glib::value::FromValue<'a> for IntRange<i32> {
508 type Checker = glib::value::GenericValueTypeChecker<Self>;
509
510 #[inline]
511 unsafe fn from_value(value: &'a glib::Value) -> Self {
512 unsafe {
513 skip_assert_initialized!();
514 let min = ffi::gst_value_get_int_range_min(value.to_glib_none().0);
515 let max = ffi::gst_value_get_int_range_max(value.to_glib_none().0);
516 let step = ffi::gst_value_get_int_range_step(value.to_glib_none().0);
517
518 Self::with_step(min, max, step)
519 }
520 }
521}
522
523impl glib::value::ToValue for IntRange<i32> {
524 #[inline]
525 fn to_value(&self) -> glib::Value {
526 let mut value = glib::Value::for_value_type::<Self>();
527 unsafe {
528 ffi::gst_value_set_int_range_step(
529 value.to_glib_none_mut().0,
530 self.min(),
531 self.max(),
532 self.step(),
533 );
534 }
535 value
536 }
537
538 #[inline]
539 fn value_type(&self) -> glib::Type {
540 Self::static_type()
541 }
542}
543
544impl From<IntRange<i32>> for glib::Value {
545 #[inline]
546 fn from(v: IntRange<i32>) -> glib::Value {
547 skip_assert_initialized!();
548 glib::value::ToValue::to_value(&v)
549 }
550}
551
552impl glib::types::StaticType for IntRange<i64> {
553 #[inline]
554 fn static_type() -> glib::types::Type {
555 unsafe { from_glib(ffi::gst_int64_range_get_type()) }
556 }
557}
558
559impl glib::value::ValueType for IntRange<i64> {
560 type Type = Self;
561}
562
563unsafe impl<'a> glib::value::FromValue<'a> for IntRange<i64> {
564 type Checker = glib::value::GenericValueTypeChecker<Self>;
565
566 #[inline]
567 unsafe fn from_value(value: &'a glib::Value) -> Self {
568 unsafe {
569 skip_assert_initialized!();
570 let min = ffi::gst_value_get_int64_range_min(value.to_glib_none().0);
571 let max = ffi::gst_value_get_int64_range_max(value.to_glib_none().0);
572 let step = ffi::gst_value_get_int64_range_step(value.to_glib_none().0);
573
574 Self::with_step(min, max, step)
575 }
576 }
577}
578
579impl glib::value::ToValue for IntRange<i64> {
580 #[inline]
581 fn to_value(&self) -> glib::Value {
582 let mut value = glib::Value::for_value_type::<Self>();
583 unsafe {
584 ffi::gst_value_set_int64_range_step(
585 value.to_glib_none_mut().0,
586 self.min(),
587 self.max(),
588 self.step(),
589 );
590 }
591 value
592 }
593
594 #[inline]
595 fn value_type(&self) -> glib::Type {
596 Self::static_type()
597 }
598}
599
600impl From<IntRange<i64>> for glib::Value {
601 #[inline]
602 fn from(v: IntRange<i64>) -> glib::Value {
603 skip_assert_initialized!();
604 glib::value::ToValue::to_value(&v)
605 }
606}
607
608#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
609#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
610pub struct FractionRange {
611 min: Fraction,
612 max: Fraction,
613}
614
615impl FractionRange {
616 #[inline]
617 pub fn new<T: Into<Fraction>, U: Into<Fraction>>(min: T, max: U) -> Self {
618 skip_assert_initialized!();
619
620 let min = min.into();
621 let max = max.into();
622
623 assert!(min <= max);
624
625 FractionRange { min, max }
626 }
627
628 #[inline]
629 pub fn min(&self) -> Fraction {
630 self.min
631 }
632
633 #[inline]
634 pub fn max(&self) -> Fraction {
635 self.max
636 }
637}
638
639impl From<(Fraction, Fraction)> for FractionRange {
640 #[inline]
641 fn from((min, max): (Fraction, Fraction)) -> Self {
642 skip_assert_initialized!();
643
644 Self::new(min, max)
645 }
646}
647
648impl glib::types::StaticType for FractionRange {
649 #[inline]
650 fn static_type() -> glib::types::Type {
651 unsafe { from_glib(ffi::gst_fraction_range_get_type()) }
652 }
653}
654
655impl glib::value::ValueType for FractionRange {
656 type Type = Self;
657}
658
659unsafe impl<'a> glib::value::FromValue<'a> for FractionRange {
660 type Checker = glib::value::GenericValueTypeChecker<Self>;
661
662 #[inline]
663 unsafe fn from_value(value: &'a glib::Value) -> Self {
664 unsafe {
665 skip_assert_initialized!();
666 let min = ffi::gst_value_get_fraction_range_min(value.to_glib_none().0);
667 let max = ffi::gst_value_get_fraction_range_max(value.to_glib_none().0);
668
669 let min_n = ffi::gst_value_get_fraction_numerator(min);
670 let min_d = ffi::gst_value_get_fraction_denominator(min);
671 let max_n = ffi::gst_value_get_fraction_numerator(max);
672 let max_d = ffi::gst_value_get_fraction_denominator(max);
673
674 Self::new((min_n, min_d), (max_n, max_d))
675 }
676 }
677}
678
679impl glib::value::ToValue for FractionRange {
680 #[inline]
681 fn to_value(&self) -> glib::Value {
682 let mut value = glib::Value::for_value_type::<Self>();
683 unsafe {
684 ffi::gst_value_set_fraction_range_full(
685 value.to_glib_none_mut().0,
686 self.min().numer(),
687 self.min().denom(),
688 self.max().numer(),
689 self.max().denom(),
690 );
691 }
692 value
693 }
694
695 #[inline]
696 fn value_type(&self) -> glib::Type {
697 Self::static_type()
698 }
699}
700
701impl From<FractionRange> for glib::Value {
702 #[inline]
703 fn from(v: FractionRange) -> glib::Value {
704 skip_assert_initialized!();
705 glib::value::ToValue::to_value(&v)
706 }
707}
708
709#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
710#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
711pub struct Bitmask(pub u64);
712
713impl Bitmask {
714 #[inline]
715 pub fn new(v: u64) -> Self {
716 skip_assert_initialized!();
717 Bitmask(v)
718 }
719}
720
721impl ops::Deref for Bitmask {
722 type Target = u64;
723
724 #[inline]
725 fn deref(&self) -> &u64 {
726 &self.0
727 }
728}
729
730impl ops::DerefMut for Bitmask {
731 #[inline]
732 fn deref_mut(&mut self) -> &mut u64 {
733 &mut self.0
734 }
735}
736
737impl ops::BitAnd for Bitmask {
738 type Output = Self;
739
740 #[inline]
741 fn bitand(self, rhs: Self) -> Self {
742 Bitmask(self.0.bitand(rhs.0))
743 }
744}
745
746impl ops::BitOr for Bitmask {
747 type Output = Self;
748
749 #[inline]
750 fn bitor(self, rhs: Self) -> Self {
751 Bitmask(self.0.bitor(rhs.0))
752 }
753}
754
755impl ops::BitXor for Bitmask {
756 type Output = Self;
757
758 #[inline]
759 fn bitxor(self, rhs: Self) -> Self {
760 Bitmask(self.0.bitxor(rhs.0))
761 }
762}
763
764impl ops::Not for Bitmask {
765 type Output = Self;
766
767 #[inline]
768 fn not(self) -> Self {
769 Bitmask(self.0.not())
770 }
771}
772
773impl From<u64> for Bitmask {
774 #[inline]
775 fn from(v: u64) -> Self {
776 skip_assert_initialized!();
777 Self::new(v)
778 }
779}
780
781impl glib::types::StaticType for Bitmask {
782 #[inline]
783 fn static_type() -> glib::types::Type {
784 unsafe { from_glib(ffi::gst_bitmask_get_type()) }
785 }
786}
787
788impl glib::value::ValueType for Bitmask {
789 type Type = Self;
790}
791
792unsafe impl<'a> glib::value::FromValue<'a> for Bitmask {
793 type Checker = glib::value::GenericValueTypeChecker<Self>;
794
795 #[inline]
796 unsafe fn from_value(value: &'a glib::Value) -> Self {
797 unsafe {
798 skip_assert_initialized!();
799 let v = ffi::gst_value_get_bitmask(value.to_glib_none().0);
800 Self::new(v)
801 }
802 }
803}
804
805impl glib::value::ToValue for Bitmask {
806 #[inline]
807 fn to_value(&self) -> glib::Value {
808 let mut value = glib::Value::for_value_type::<Self>();
809 unsafe {
810 ffi::gst_value_set_bitmask(value.to_glib_none_mut().0, self.0);
811 }
812 value
813 }
814
815 #[inline]
816 fn value_type(&self) -> glib::Type {
817 Self::static_type()
818 }
819}
820
821impl From<Bitmask> for glib::Value {
822 #[inline]
823 fn from(v: Bitmask) -> glib::Value {
824 skip_assert_initialized!();
825 glib::value::ToValue::to_value(&v)
826 }
827}
828
829#[derive(Clone)]
830pub struct Array(glib::SendValue);
831
832unsafe impl Send for Array {}
833unsafe impl Sync for Array {}
834
835impl fmt::Debug for Array {
836 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
837 f.debug_tuple("Array").field(&self.as_slice()).finish()
838 }
839}
840
841impl Array {
842 pub fn new<T: Into<glib::Value> + Send>(values: impl IntoIterator<Item = T>) -> Self {
843 assert_initialized_main_thread!();
844
845 unsafe {
846 let mut value = glib::Value::for_value_type::<Array>();
847 for v in values.into_iter() {
848 let mut v = v.into().into_raw();
849 ffi::gst_value_array_append_and_take_value(value.to_glib_none_mut().0, &mut v);
850 }
851
852 Self(glib::SendValue::unsafe_from(value.into_raw()))
853 }
854 }
855
856 pub fn from_values(values: impl IntoIterator<Item = glib::SendValue>) -> Self {
857 skip_assert_initialized!();
858
859 Self::new(values)
860 }
861
862 #[inline]
863 pub fn as_slice(&self) -> &[glib::SendValue] {
864 unsafe {
865 let arr = (*self.0.as_ptr()).data[0].v_pointer as *const glib::ffi::GArray;
866 if arr.is_null() || (*arr).len == 0 {
867 &[]
868 } else {
869 #[allow(clippy::cast_ptr_alignment)]
870 slice::from_raw_parts((*arr).data as *const glib::SendValue, (*arr).len as usize)
871 }
872 }
873 }
874
875 pub fn append_value(&mut self, value: glib::SendValue) {
876 unsafe {
877 ffi::gst_value_array_append_and_take_value(
878 self.0.to_glib_none_mut().0,
879 &mut value.into_raw(),
880 );
881 }
882 }
883
884 pub fn append(&mut self, value: impl Into<glib::Value> + Send) {
885 self.append_value(glib::SendValue::from_owned(value));
886 }
887}
888
889impl Default for Array {
890 fn default() -> Self {
891 skip_assert_initialized!();
892
893 unsafe {
894 let value = glib::Value::for_value_type::<Array>();
895
896 Self(glib::SendValue::unsafe_from(value.into_raw()))
897 }
898 }
899}
900
901impl ops::Deref for Array {
902 type Target = [glib::SendValue];
903
904 #[inline]
905 fn deref(&self) -> &[glib::SendValue] {
906 self.as_slice()
907 }
908}
909
910impl AsRef<[glib::SendValue]> for Array {
911 #[inline]
912 fn as_ref(&self) -> &[glib::SendValue] {
913 self.as_slice()
914 }
915}
916
917impl std::iter::FromIterator<glib::SendValue> for Array {
918 fn from_iter<T: IntoIterator<Item = glib::SendValue>>(iter: T) -> Self {
919 skip_assert_initialized!();
920 Self::from_values(iter)
921 }
922}
923
924impl std::iter::Extend<glib::SendValue> for Array {
925 fn extend<T: IntoIterator<Item = glib::SendValue>>(&mut self, iter: T) {
926 for v in iter.into_iter() {
927 self.append_value(v);
928 }
929 }
930}
931
932impl glib::value::ValueType for Array {
933 type Type = Self;
934}
935
936unsafe impl<'a> glib::value::FromValue<'a> for Array {
937 type Checker = glib::value::GenericValueTypeChecker<Self>;
938
939 unsafe fn from_value(value: &'a glib::Value) -> Self {
940 unsafe {
941 skip_assert_initialized!();
942 Self(glib::SendValue::unsafe_from(value.clone().into_raw()))
943 }
944 }
945}
946
947impl glib::value::ToValue for Array {
948 fn to_value(&self) -> glib::Value {
949 self.0.clone().into()
950 }
951
952 fn value_type(&self) -> glib::Type {
953 Self::static_type()
954 }
955}
956
957impl From<Array> for glib::Value {
958 fn from(v: Array) -> glib::Value {
959 skip_assert_initialized!();
960 v.0.into()
961 }
962}
963
964impl glib::types::StaticType for Array {
965 #[inline]
966 fn static_type() -> glib::types::Type {
967 unsafe { from_glib(ffi::gst_value_array_get_type()) }
968 }
969}
970
971#[derive(Debug, Clone)]
972pub struct ArrayRef<'a>(&'a [glib::SendValue]);
973
974unsafe impl Send for ArrayRef<'_> {}
975unsafe impl Sync for ArrayRef<'_> {}
976
977impl<'a> ArrayRef<'a> {
978 pub fn new(values: &'a [glib::SendValue]) -> Self {
979 skip_assert_initialized!();
980
981 Self(values)
982 }
983
984 #[inline]
985 pub fn as_slice(&self) -> &'a [glib::SendValue] {
986 self.0
987 }
988}
989
990impl ops::Deref for ArrayRef<'_> {
991 type Target = [glib::SendValue];
992
993 #[inline]
994 fn deref(&self) -> &[glib::SendValue] {
995 self.as_slice()
996 }
997}
998
999impl AsRef<[glib::SendValue]> for ArrayRef<'_> {
1000 #[inline]
1001 fn as_ref(&self) -> &[glib::SendValue] {
1002 self.as_slice()
1003 }
1004}
1005
1006unsafe impl<'a> glib::value::FromValue<'a> for ArrayRef<'a> {
1007 type Checker = glib::value::GenericValueTypeChecker<Self>;
1008
1009 #[inline]
1010 unsafe fn from_value(value: &'a glib::Value) -> Self {
1011 unsafe {
1012 skip_assert_initialized!();
1013 let arr = (*value.as_ptr()).data[0].v_pointer as *const glib::ffi::GArray;
1014 if arr.is_null() || (*arr).len == 0 {
1015 Self(&[])
1016 } else {
1017 #[allow(clippy::cast_ptr_alignment)]
1018 Self(slice::from_raw_parts(
1019 (*arr).data as *const glib::SendValue,
1020 (*arr).len as usize,
1021 ))
1022 }
1023 }
1024 }
1025}
1026
1027impl glib::value::ToValue for ArrayRef<'_> {
1028 #[inline]
1029 fn to_value(&self) -> glib::Value {
1030 let mut value = glib::Value::for_value_type::<Array>();
1031 unsafe {
1032 for v in self.0 {
1033 ffi::gst_value_array_append_value(value.to_glib_none_mut().0, v.to_glib_none().0);
1034 }
1035 }
1036 value
1037 }
1038
1039 #[inline]
1040 fn value_type(&self) -> glib::Type {
1041 Self::static_type()
1042 }
1043}
1044
1045impl<'a> From<ArrayRef<'a>> for glib::Value {
1046 #[inline]
1047 fn from(v: ArrayRef<'a>) -> glib::Value {
1048 skip_assert_initialized!();
1049 glib::value::ToValue::to_value(&v)
1050 }
1051}
1052
1053impl glib::types::StaticType for ArrayRef<'_> {
1054 #[inline]
1055 fn static_type() -> glib::types::Type {
1056 unsafe { from_glib(ffi::gst_value_array_get_type()) }
1057 }
1058}
1059
1060#[derive(Clone)]
1061pub struct List(glib::SendValue);
1062
1063unsafe impl Send for List {}
1064unsafe impl Sync for List {}
1065
1066impl fmt::Debug for List {
1067 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1068 f.debug_tuple("List").field(&self.as_slice()).finish()
1069 }
1070}
1071
1072impl List {
1073 pub fn new<T: Into<glib::Value> + Send>(values: impl IntoIterator<Item = T>) -> Self {
1074 assert_initialized_main_thread!();
1075
1076 unsafe {
1077 let mut value = glib::Value::for_value_type::<List>();
1078 for v in values.into_iter() {
1079 let mut v = v.into().into_raw();
1080 ffi::gst_value_list_append_and_take_value(value.to_glib_none_mut().0, &mut v);
1081 }
1082
1083 Self(glib::SendValue::unsafe_from(value.into_raw()))
1084 }
1085 }
1086
1087 pub fn from_values(values: impl IntoIterator<Item = glib::SendValue>) -> Self {
1088 skip_assert_initialized!();
1089
1090 Self::new(values)
1091 }
1092
1093 #[inline]
1094 pub fn as_slice(&self) -> &[glib::SendValue] {
1095 unsafe {
1096 let arr = (*self.0.as_ptr()).data[0].v_pointer as *const glib::ffi::GArray;
1097 if arr.is_null() || (*arr).len == 0 {
1098 &[]
1099 } else {
1100 #[allow(clippy::cast_ptr_alignment)]
1101 slice::from_raw_parts((*arr).data as *const glib::SendValue, (*arr).len as usize)
1102 }
1103 }
1104 }
1105
1106 pub fn append_value(&mut self, value: glib::SendValue) {
1107 unsafe {
1108 ffi::gst_value_list_append_and_take_value(
1109 self.0.to_glib_none_mut().0,
1110 &mut value.into_raw(),
1111 );
1112 }
1113 }
1114
1115 pub fn append(&mut self, value: impl Into<glib::Value> + Send) {
1116 self.append_value(glib::SendValue::from_owned(value));
1117 }
1118}
1119
1120impl Default for List {
1121 fn default() -> Self {
1122 skip_assert_initialized!();
1123
1124 unsafe {
1125 let value = glib::Value::for_value_type::<List>();
1126
1127 Self(glib::SendValue::unsafe_from(value.into_raw()))
1128 }
1129 }
1130}
1131
1132impl ops::Deref for List {
1133 type Target = [glib::SendValue];
1134
1135 #[inline]
1136 fn deref(&self) -> &[glib::SendValue] {
1137 self.as_slice()
1138 }
1139}
1140
1141impl AsRef<[glib::SendValue]> for List {
1142 #[inline]
1143 fn as_ref(&self) -> &[glib::SendValue] {
1144 self.as_slice()
1145 }
1146}
1147
1148impl std::iter::FromIterator<glib::SendValue> for List {
1149 fn from_iter<T: IntoIterator<Item = glib::SendValue>>(iter: T) -> Self {
1150 skip_assert_initialized!();
1151 Self::from_values(iter)
1152 }
1153}
1154
1155impl std::iter::Extend<glib::SendValue> for List {
1156 fn extend<T: IntoIterator<Item = glib::SendValue>>(&mut self, iter: T) {
1157 for v in iter.into_iter() {
1158 self.append_value(v);
1159 }
1160 }
1161}
1162
1163impl glib::value::ValueType for List {
1164 type Type = Self;
1165}
1166
1167unsafe impl<'a> glib::value::FromValue<'a> for List {
1168 type Checker = glib::value::GenericValueTypeChecker<Self>;
1169
1170 unsafe fn from_value(value: &'a glib::Value) -> Self {
1171 unsafe {
1172 skip_assert_initialized!();
1173 Self(glib::SendValue::unsafe_from(value.clone().into_raw()))
1174 }
1175 }
1176}
1177
1178impl glib::value::ToValue for List {
1179 fn to_value(&self) -> glib::Value {
1180 self.0.clone().into()
1181 }
1182
1183 fn value_type(&self) -> glib::Type {
1184 Self::static_type()
1185 }
1186}
1187
1188impl From<List> for glib::Value {
1189 fn from(v: List) -> glib::Value {
1190 skip_assert_initialized!();
1191 v.0.into()
1192 }
1193}
1194
1195impl glib::types::StaticType for List {
1196 #[inline]
1197 fn static_type() -> glib::types::Type {
1198 unsafe { from_glib(ffi::gst_value_list_get_type()) }
1199 }
1200}
1201
1202#[derive(Debug, Clone)]
1203pub struct ListRef<'a>(&'a [glib::SendValue]);
1204
1205unsafe impl Send for ListRef<'_> {}
1206unsafe impl Sync for ListRef<'_> {}
1207
1208impl<'a> ListRef<'a> {
1209 pub fn new(values: &'a [glib::SendValue]) -> Self {
1210 skip_assert_initialized!();
1211
1212 Self(values)
1213 }
1214
1215 #[inline]
1216 pub fn as_slice(&self) -> &'a [glib::SendValue] {
1217 self.0
1218 }
1219}
1220
1221impl ops::Deref for ListRef<'_> {
1222 type Target = [glib::SendValue];
1223
1224 #[inline]
1225 fn deref(&self) -> &[glib::SendValue] {
1226 self.as_slice()
1227 }
1228}
1229
1230impl AsRef<[glib::SendValue]> for ListRef<'_> {
1231 #[inline]
1232 fn as_ref(&self) -> &[glib::SendValue] {
1233 self.as_slice()
1234 }
1235}
1236
1237unsafe impl<'a> glib::value::FromValue<'a> for ListRef<'a> {
1238 type Checker = glib::value::GenericValueTypeChecker<Self>;
1239
1240 #[inline]
1241 unsafe fn from_value(value: &'a glib::Value) -> Self {
1242 unsafe {
1243 skip_assert_initialized!();
1244 let arr = (*value.as_ptr()).data[0].v_pointer as *const glib::ffi::GArray;
1245 if arr.is_null() || (*arr).len == 0 {
1246 Self(&[])
1247 } else {
1248 #[allow(clippy::cast_ptr_alignment)]
1249 Self(slice::from_raw_parts(
1250 (*arr).data as *const glib::SendValue,
1251 (*arr).len as usize,
1252 ))
1253 }
1254 }
1255 }
1256}
1257
1258impl glib::value::ToValue for ListRef<'_> {
1259 #[inline]
1260 fn to_value(&self) -> glib::Value {
1261 let mut value = glib::Value::for_value_type::<List>();
1262 unsafe {
1263 for v in self.0 {
1264 ffi::gst_value_list_append_value(value.to_glib_none_mut().0, v.to_glib_none().0);
1265 }
1266 }
1267 value
1268 }
1269
1270 #[inline]
1271 fn value_type(&self) -> glib::Type {
1272 Self::static_type()
1273 }
1274}
1275
1276impl<'a> From<ListRef<'a>> for glib::Value {
1277 #[inline]
1278 fn from(v: ListRef<'a>) -> glib::Value {
1279 skip_assert_initialized!();
1280 glib::value::ToValue::to_value(&v)
1281 }
1282}
1283
1284impl glib::types::StaticType for ListRef<'_> {
1285 #[inline]
1286 fn static_type() -> glib::types::Type {
1287 unsafe { from_glib(ffi::gst_value_list_get_type()) }
1288 }
1289}
1290
1291#[cfg(feature = "v1_28")]
1292#[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
1293mod unique_list {
1294 use std::mem;
1295
1296 use super::*;
1297
1298 #[derive(Clone)]
1299 pub struct UniqueList(glib::SendValue);
1300
1301 unsafe impl Send for UniqueList {}
1302 unsafe impl Sync for UniqueList {}
1303
1304 impl fmt::Debug for UniqueList {
1305 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1306 f.debug_tuple("UniqueList").field(&self.as_slice()).finish()
1307 }
1308 }
1309
1310 impl UniqueList {
1311 pub fn new<T: Into<glib::Value> + Send>(values: impl IntoIterator<Item = T>) -> Self {
1312 assert_initialized_main_thread!();
1313
1314 unsafe {
1315 let mut value = glib::Value::for_value_type::<UniqueList>();
1316 for v in values.into_iter() {
1317 let mut v = v.into().into_raw();
1318 ffi::gst_value_unique_list_append_and_take_value(
1319 value.to_glib_none_mut().0,
1320 &mut v,
1321 );
1322 }
1323
1324 Self(glib::SendValue::unsafe_from(value.into_raw()))
1325 }
1326 }
1327
1328 pub fn from_values(values: impl IntoIterator<Item = glib::SendValue>) -> Self {
1329 skip_assert_initialized!();
1330
1331 Self::new(values)
1332 }
1333
1334 #[inline]
1335 pub fn as_slice(&self) -> &[glib::SendValue] {
1336 unsafe {
1337 let arr = (*self.0.as_ptr()).data[0].v_pointer as *const glib::ffi::GArray;
1338 if arr.is_null() || (*arr).len == 0 {
1339 &[]
1340 } else {
1341 #[allow(clippy::cast_ptr_alignment)]
1342 slice::from_raw_parts(
1343 (*arr).data as *const glib::SendValue,
1344 (*arr).len as usize,
1345 )
1346 }
1347 }
1348 }
1349
1350 pub fn append_value(&mut self, value: glib::SendValue) {
1351 unsafe {
1352 ffi::gst_value_unique_list_append_and_take_value(
1353 self.0.to_glib_none_mut().0,
1354 &mut value.into_raw(),
1355 );
1356 }
1357 }
1358
1359 pub fn append(&mut self, value: impl Into<glib::Value> + Send) {
1360 self.append_value(glib::SendValue::from_owned(value));
1361 }
1362
1363 #[doc(alias = "gst_value_list_concat")]
1364 pub fn concat(a: &UniqueList, b: &UniqueList) -> UniqueList {
1365 skip_assert_initialized!();
1366
1367 unsafe {
1368 let mut res = mem::MaybeUninit::zeroed();
1369 ffi::gst_value_list_concat(
1370 res.as_mut_ptr(),
1371 a.0.to_glib_none().0,
1372 b.0.to_glib_none().0,
1373 );
1374 UniqueList(glib::SendValue::unsafe_from(res.assume_init()))
1375 }
1376 }
1377 }
1378
1379 impl Default for UniqueList {
1380 fn default() -> Self {
1381 skip_assert_initialized!();
1382
1383 unsafe {
1384 let value = glib::Value::for_value_type::<UniqueList>();
1385
1386 Self(glib::SendValue::unsafe_from(value.into_raw()))
1387 }
1388 }
1389 }
1390
1391 impl ops::Deref for UniqueList {
1392 type Target = [glib::SendValue];
1393
1394 #[inline]
1395 fn deref(&self) -> &[glib::SendValue] {
1396 self.as_slice()
1397 }
1398 }
1399
1400 impl AsRef<[glib::SendValue]> for UniqueList {
1401 #[inline]
1402 fn as_ref(&self) -> &[glib::SendValue] {
1403 self.as_slice()
1404 }
1405 }
1406
1407 impl std::iter::FromIterator<glib::SendValue> for UniqueList {
1408 fn from_iter<T: IntoIterator<Item = glib::SendValue>>(iter: T) -> Self {
1409 skip_assert_initialized!();
1410 Self::from_values(iter)
1411 }
1412 }
1413
1414 impl std::iter::Extend<glib::SendValue> for UniqueList {
1415 fn extend<T: IntoIterator<Item = glib::SendValue>>(&mut self, iter: T) {
1416 for v in iter.into_iter() {
1417 self.append_value(v);
1418 }
1419 }
1420 }
1421
1422 impl glib::value::ValueType for UniqueList {
1423 type Type = Self;
1424 }
1425
1426 unsafe impl<'a> glib::value::FromValue<'a> for UniqueList {
1427 type Checker = glib::value::GenericValueTypeChecker<Self>;
1428
1429 unsafe fn from_value(value: &'a glib::Value) -> Self {
1430 unsafe {
1431 skip_assert_initialized!();
1432 Self(glib::SendValue::unsafe_from(value.clone().into_raw()))
1433 }
1434 }
1435 }
1436
1437 impl glib::value::ToValue for UniqueList {
1438 fn to_value(&self) -> glib::Value {
1439 self.0.clone().into()
1440 }
1441
1442 fn value_type(&self) -> glib::Type {
1443 Self::static_type()
1444 }
1445 }
1446
1447 impl From<UniqueList> for glib::Value {
1448 fn from(v: UniqueList) -> glib::Value {
1449 skip_assert_initialized!();
1450 v.0.into()
1451 }
1452 }
1453
1454 impl glib::types::StaticType for UniqueList {
1455 #[inline]
1456 fn static_type() -> glib::types::Type {
1457 unsafe { from_glib(ffi::gst_value_unique_list_get_type()) }
1458 }
1459 }
1460
1461 #[derive(Debug, Clone)]
1462 pub struct UniqueListRef<'a>(&'a [glib::SendValue]);
1463
1464 unsafe impl Send for UniqueListRef<'_> {}
1465 unsafe impl Sync for UniqueListRef<'_> {}
1466
1467 impl<'a> UniqueListRef<'a> {
1468 pub fn new(values: &'a [glib::SendValue]) -> Self {
1469 skip_assert_initialized!();
1470
1471 Self(values)
1472 }
1473
1474 #[inline]
1475 pub fn as_slice(&self) -> &'a [glib::SendValue] {
1476 self.0
1477 }
1478 }
1479
1480 impl ops::Deref for UniqueListRef<'_> {
1481 type Target = [glib::SendValue];
1482
1483 #[inline]
1484 fn deref(&self) -> &[glib::SendValue] {
1485 self.as_slice()
1486 }
1487 }
1488
1489 impl AsRef<[glib::SendValue]> for UniqueListRef<'_> {
1490 #[inline]
1491 fn as_ref(&self) -> &[glib::SendValue] {
1492 self.as_slice()
1493 }
1494 }
1495
1496 unsafe impl<'a> glib::value::FromValue<'a> for UniqueListRef<'a> {
1497 type Checker = glib::value::GenericValueTypeChecker<Self>;
1498
1499 #[inline]
1500 unsafe fn from_value(value: &'a glib::Value) -> Self {
1501 unsafe {
1502 skip_assert_initialized!();
1503 let arr = (*value.as_ptr()).data[0].v_pointer as *const glib::ffi::GArray;
1504 if arr.is_null() || (*arr).len == 0 {
1505 Self(&[])
1506 } else {
1507 #[allow(clippy::cast_ptr_alignment)]
1508 Self(slice::from_raw_parts(
1509 (*arr).data as *const glib::SendValue,
1510 (*arr).len as usize,
1511 ))
1512 }
1513 }
1514 }
1515 }
1516
1517 impl glib::value::ToValue for UniqueListRef<'_> {
1518 #[inline]
1519 fn to_value(&self) -> glib::Value {
1520 let mut value = glib::Value::for_value_type::<UniqueList>();
1521 unsafe {
1522 for v in self.0 {
1523 ffi::gst_value_unique_list_append_value(
1524 value.to_glib_none_mut().0,
1525 v.to_glib_none().0,
1526 );
1527 }
1528 }
1529 value
1530 }
1531
1532 #[inline]
1533 fn value_type(&self) -> glib::Type {
1534 Self::static_type()
1535 }
1536 }
1537
1538 impl<'a> From<UniqueListRef<'a>> for glib::Value {
1539 #[inline]
1540 fn from(v: UniqueListRef<'a>) -> glib::Value {
1541 skip_assert_initialized!();
1542 glib::value::ToValue::to_value(&v)
1543 }
1544 }
1545
1546 impl glib::types::StaticType for UniqueListRef<'_> {
1547 #[inline]
1548 fn static_type() -> glib::types::Type {
1549 unsafe { from_glib(ffi::gst_value_unique_list_get_type()) }
1550 }
1551 }
1552}
1553
1554#[cfg(feature = "v1_28")]
1555#[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
1556pub use unique_list::*;
1557
1558pub trait GstValueExt: Sized {
1559 #[doc(alias = "gst_value_can_compare")]
1560 fn can_compare(&self, other: &Self) -> bool;
1561 #[doc(alias = "gst_value_compare")]
1562 fn compare(&self, other: &Self) -> Option<cmp::Ordering>;
1563 fn eq(&self, other: &Self) -> bool;
1564 #[doc(alias = "gst_value_can_intersect")]
1565 fn can_intersect(&self, other: &Self) -> bool;
1566 #[doc(alias = "gst_value_intersect")]
1567 fn intersect(&self, other: &Self) -> Option<Self>;
1568 #[doc(alias = "gst_value_can_subtract")]
1569 fn can_subtract(&self, other: &Self) -> bool;
1570 #[doc(alias = "gst_value_subtract")]
1571 fn subtract(&self, other: &Self) -> Option<Self>;
1572 #[doc(alias = "gst_value_can_union")]
1573 fn can_union(&self, other: &Self) -> bool;
1574 #[doc(alias = "gst_value_union")]
1575 fn union(&self, other: &Self) -> Option<Self>;
1576 #[doc(alias = "gst_value_fixate")]
1577 fn fixate(&self) -> Option<Self>;
1578 #[doc(alias = "gst_value_is_fixed")]
1579 fn is_fixed(&self) -> bool;
1580 #[doc(alias = "gst_value_is_subset")]
1581 fn is_subset(&self, superset: &Self) -> bool;
1582 #[doc(alias = "gst_value_serialize")]
1583 fn serialize(&self) -> Result<glib::GString, glib::BoolError>;
1584 #[doc(alias = "gst_value_deserialize")]
1585 fn deserialize(s: &str, type_: glib::Type) -> Result<glib::Value, glib::BoolError>;
1586 #[cfg(feature = "v1_20")]
1587 #[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
1588 #[doc(alias = "gst_value_deserialize_with_pspec")]
1589 fn deserialize_with_pspec(
1590 s: &str,
1591 pspec: &glib::ParamSpec,
1592 ) -> Result<glib::Value, glib::BoolError>;
1593 #[cfg(feature = "v1_28")]
1594 #[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
1595 #[doc(alias = "gst_value_hash")]
1596 fn hash(&self) -> Result<u32, glib::BoolError>;
1597}
1598
1599impl GstValueExt for glib::Value {
1600 fn can_compare(&self, other: &Self) -> bool {
1601 unsafe {
1602 from_glib(ffi::gst_value_can_compare(
1603 self.to_glib_none().0,
1604 other.to_glib_none().0,
1605 ))
1606 }
1607 }
1608
1609 fn compare(&self, other: &Self) -> Option<cmp::Ordering> {
1610 unsafe {
1611 let val = ffi::gst_value_compare(self.to_glib_none().0, other.to_glib_none().0);
1612
1613 match val {
1614 ffi::GST_VALUE_LESS_THAN => Some(cmp::Ordering::Less),
1615 ffi::GST_VALUE_EQUAL => Some(cmp::Ordering::Equal),
1616 ffi::GST_VALUE_GREATER_THAN => Some(cmp::Ordering::Greater),
1617 _ => None,
1618 }
1619 }
1620 }
1621
1622 fn eq(&self, other: &Self) -> bool {
1623 self.compare(other) == Some(cmp::Ordering::Equal)
1624 }
1625
1626 fn can_intersect(&self, other: &Self) -> bool {
1627 unsafe {
1628 from_glib(ffi::gst_value_can_intersect(
1629 self.to_glib_none().0,
1630 other.to_glib_none().0,
1631 ))
1632 }
1633 }
1634
1635 fn intersect(&self, other: &Self) -> Option<Self> {
1636 unsafe {
1637 let mut value = glib::Value::uninitialized();
1638 let ret: bool = from_glib(ffi::gst_value_intersect(
1639 value.to_glib_none_mut().0,
1640 self.to_glib_none().0,
1641 other.to_glib_none().0,
1642 ));
1643 if ret { Some(value) } else { None }
1644 }
1645 }
1646
1647 fn can_subtract(&self, other: &Self) -> bool {
1648 unsafe {
1649 from_glib(ffi::gst_value_can_subtract(
1650 self.to_glib_none().0,
1651 other.to_glib_none().0,
1652 ))
1653 }
1654 }
1655
1656 fn subtract(&self, other: &Self) -> Option<Self> {
1657 unsafe {
1658 let mut value = glib::Value::uninitialized();
1659 let ret: bool = from_glib(ffi::gst_value_subtract(
1660 value.to_glib_none_mut().0,
1661 self.to_glib_none().0,
1662 other.to_glib_none().0,
1663 ));
1664 if ret { Some(value) } else { None }
1665 }
1666 }
1667
1668 fn can_union(&self, other: &Self) -> bool {
1669 unsafe {
1670 from_glib(ffi::gst_value_can_union(
1671 self.to_glib_none().0,
1672 other.to_glib_none().0,
1673 ))
1674 }
1675 }
1676
1677 fn union(&self, other: &Self) -> Option<Self> {
1678 unsafe {
1679 let mut value = glib::Value::uninitialized();
1680 let ret: bool = from_glib(ffi::gst_value_union(
1681 value.to_glib_none_mut().0,
1682 self.to_glib_none().0,
1683 other.to_glib_none().0,
1684 ));
1685 if ret { Some(value) } else { None }
1686 }
1687 }
1688
1689 fn fixate(&self) -> Option<Self> {
1690 unsafe {
1691 let mut value = glib::Value::uninitialized();
1692 let ret: bool = from_glib(ffi::gst_value_fixate(
1693 value.to_glib_none_mut().0,
1694 self.to_glib_none().0,
1695 ));
1696 if ret { Some(value) } else { None }
1697 }
1698 }
1699
1700 fn is_fixed(&self) -> bool {
1701 unsafe { from_glib(ffi::gst_value_is_fixed(self.to_glib_none().0)) }
1702 }
1703
1704 fn is_subset(&self, superset: &Self) -> bool {
1705 unsafe {
1706 from_glib(ffi::gst_value_is_subset(
1707 self.to_glib_none().0,
1708 superset.to_glib_none().0,
1709 ))
1710 }
1711 }
1712
1713 fn serialize(&self) -> Result<glib::GString, glib::BoolError> {
1714 unsafe {
1715 Option::<_>::from_glib_full(ffi::gst_value_serialize(self.to_glib_none().0))
1716 .ok_or_else(|| glib::bool_error!("Failed to serialize value"))
1717 }
1718 }
1719
1720 fn deserialize(s: &str, type_: glib::Type) -> Result<glib::Value, glib::BoolError> {
1721 skip_assert_initialized!();
1722
1723 unsafe {
1724 let mut value = glib::Value::from_type(type_);
1725 let ret: bool = from_glib(ffi::gst_value_deserialize(
1726 value.to_glib_none_mut().0,
1727 s.to_glib_none().0,
1728 ));
1729 if ret {
1730 Ok(value)
1731 } else {
1732 Err(glib::bool_error!("Failed to deserialize value"))
1733 }
1734 }
1735 }
1736
1737 #[cfg(feature = "v1_20")]
1738 #[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
1739 fn deserialize_with_pspec(
1740 s: &str,
1741 pspec: &glib::ParamSpec,
1742 ) -> Result<glib::Value, glib::BoolError> {
1743 skip_assert_initialized!();
1744
1745 unsafe {
1746 let mut value = glib::Value::from_type_unchecked(pspec.value_type());
1747 let ret: bool = from_glib(ffi::gst_value_deserialize_with_pspec(
1748 value.to_glib_none_mut().0,
1749 s.to_glib_none().0,
1750 pspec.to_glib_none().0,
1751 ));
1752 if ret {
1753 Ok(value)
1754 } else {
1755 Err(glib::bool_error!("Failed to deserialize value"))
1756 }
1757 }
1758 }
1759
1760 #[cfg(feature = "v1_28")]
1761 fn hash(&self) -> Result<u32, glib::BoolError> {
1762 {
1763 unsafe {
1764 let mut hash = 0;
1765 glib::result_from_gboolean!(
1766 gstreamer_sys::gst_value_hash(self.to_glib_none().0, &mut hash),
1767 "Failed to hash {self:?}"
1768 )?;
1769 Ok(hash)
1770 }
1771 }
1772 }
1773}
1774
1775#[doc(hidden)]
1776#[macro_export]
1777macro_rules! impl_builder_gvalue_extra_setters (
1778 (field) => {
1779 #[inline]
1785 pub fn field_if(self, name: impl $crate::glib::IntoGStr, value: impl Into<$crate::glib::Value> + Send, predicate: bool) -> Self {
1786 if predicate {
1787 self.field(name, value)
1788 } else {
1789 self
1790 }
1791 }
1792
1793 #[inline]
1799 pub fn field_with_static_if(self, name: impl AsRef<$crate::glib::GStr> + 'static, value: impl Into<$crate::glib::Value> + Send, predicate: bool) -> Self {
1800 if predicate {
1801 self.field_with_static(name, value)
1802 } else {
1803 self
1804 }
1805 }
1806
1807 #[inline]
1813 pub fn field_with_id_if(self, name: impl AsRef<$crate::IdStr>, value: impl Into<$crate::glib::Value> + Send, predicate: bool) -> Self {
1814 if predicate {
1815 self.field_with_id(name, value)
1816 } else {
1817 self
1818 }
1819 }
1820
1821 #[inline]
1826 pub fn field_if_some(self, name: impl $crate::glib::IntoGStr, value: Option<impl Into<$crate::glib::Value> + Send>) -> Self {
1827 if let Some(value) = value {
1828 self.field(name, value)
1829 } else {
1830 self
1831 }
1832 }
1833
1834 #[inline]
1839 pub fn field_with_static_if_some(self, name: impl AsRef<$crate::glib::GStr> + 'static, value: Option<impl Into<$crate::glib::Value> + Send>) -> Self {
1840 if let Some(value) = value {
1841 self.field_with_static(name, value)
1842 } else {
1843 self
1844 }
1845 }
1846
1847 #[inline]
1852 pub fn field_with_id_if_some(self, name: impl AsRef<$crate::IdStr>, value: Option<impl Into<$crate::glib::Value> + Send>) -> Self {
1853 if let Some(value) = value {
1854 self.field_with_id(name, value)
1855 } else {
1856 self
1857 }
1858 }
1859
1860 #[inline]
1865 pub fn field_from_iter<
1866 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send,
1867 I: $crate::glib::value::ToSendValue,
1868 >(
1869 self,
1870 name: impl $crate::glib::IntoGStr,
1871 iter: impl IntoIterator<Item = I>,
1872 ) -> Self {
1873 let iter = iter.into_iter().map(|item| item.to_send_value());
1874 self.field(name, V::from_iter(iter))
1875 }
1876
1877 #[inline]
1882 pub fn field_with_static_from_iter<
1883 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send,
1884 I: $crate::glib::value::ToSendValue,
1885 >(
1886 self,
1887 name: impl AsRef<$crate::glib::GStr> + 'static,
1888 iter: impl IntoIterator<Item = I>,
1889 ) -> Self {
1890 let iter = iter.into_iter().map(|item| item.to_send_value());
1891 self.field_with_static(name, V::from_iter(iter))
1892 }
1893
1894 #[inline]
1899 pub fn field_with_id_from_iter<
1900 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send,
1901 I: $crate::glib::value::ToSendValue,
1902 >(
1903 self,
1904 name: impl AsRef<$crate::IdStr>,
1905 iter: impl IntoIterator<Item = I>,
1906 ) -> Self {
1907 let iter = iter.into_iter().map(|item| item.to_send_value());
1908 self.field_with_id(name, V::from_iter(iter))
1909 }
1910
1911 #[inline]
1917 pub fn field_if_not_empty<
1918 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send,
1919 I: $crate::glib::value::ToSendValue,
1920 >(
1921 self,
1922 name: impl $crate::glib::IntoGStr,
1923 iter: impl IntoIterator<Item = I>,
1924 ) -> Self {
1925 let mut iter = iter.into_iter().peekable();
1926 if iter.peek().is_some() {
1927 let iter = iter.map(|item| item.to_send_value());
1928 self.field(name, V::from_iter(iter))
1929 } else {
1930 self
1931 }
1932 }
1933
1934 #[inline]
1940 pub fn field_with_static_if_not_empty<
1941 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send,
1942 I: $crate::glib::value::ToSendValue,
1943 >(
1944 self,
1945 name: impl AsRef<$crate::glib::GStr> + 'static,
1946 iter: impl IntoIterator<Item = I>,
1947 ) -> Self {
1948 let mut iter = iter.into_iter().peekable();
1949 if iter.peek().is_some() {
1950 let iter = iter.map(|item| item.to_send_value());
1951 self.field_with_static(name, V::from_iter(iter))
1952 } else {
1953 self
1954 }
1955 }
1956
1957 #[inline]
1963 pub fn field_with_id_if_not_empty
1964 <V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue> + Send,
1965 I: $crate::glib::value::ToSendValue,
1966 >(
1967 self,
1968 name: impl AsRef<IdStr>,
1969 iter: impl IntoIterator<Item = I>,
1970 ) -> Self {
1971 let mut iter = iter.into_iter().peekable();
1972 if iter.peek().is_some() {
1973 let iter = iter.map(|item| item.to_send_value());
1974 self.field_with_id(name, V::from_iter(iter))
1975 } else {
1976 self
1977 }
1978 }
1979 };
1980
1981 (other_field) => {
1982 #[inline]
1988 pub fn other_field_if(self, name: &'a str, value: impl $crate::glib::value::ToSendValue, predicate: bool) -> Self {
1989 if predicate {
1990 self.other_field(name, value)
1991 } else {
1992 self
1993 }
1994 }
1995
1996 #[inline]
2001 pub fn other_field_if_some(self, name: &'a str, value: Option<impl $crate::glib::value::ToSendValue>) -> Self {
2002 if let Some(value) = value {
2003 self.other_field(name, value)
2004 } else {
2005 self
2006 }
2007 }
2008
2009 #[inline]
2014 pub fn other_field_from_iter<
2015 V: $crate::glib::value::ValueType + $crate::glib::value::ToSendValue + FromIterator<$crate::glib::SendValue>,
2016 I: $crate::glib::value::ToSendValue,
2017 >(
2018 self,
2019 name: &'a str,
2020 iter: impl IntoIterator<Item = I>,
2021 ) -> Self {
2022 let iter = iter.into_iter().map(|item| item.to_send_value());
2023 self.other_field(name, V::from_iter(iter))
2024 }
2025
2026 #[inline]
2032 pub fn other_field_if_not_empty<
2033 V: $crate::glib::value::ValueType + $crate::glib::value::ToSendValue + FromIterator<$crate::glib::SendValue>,
2034 I: $crate::glib::value::ToSendValue,
2035 >(
2036 self,
2037 name: &'a str,
2038 iter: impl IntoIterator<Item = I>,
2039 ) -> Self {
2040 let mut iter = iter.into_iter().peekable();
2041 if iter.peek().is_some() {
2042 let iter = iter.map(|item| item.to_send_value());
2043 self.other_field(name, V::from_iter(iter))
2044 } else {
2045 self
2046 }
2047 }
2048 };
2049
2050 (property_and_name) => {
2051 #[inline]
2057 pub fn property_if(self, name: &'a str, value: impl Into<$crate::glib::Value> + 'a, predicate: bool) -> Self {
2058 if predicate {
2059 self.property(name, value)
2060 } else {
2061 self
2062 }
2063 }
2064
2065 #[inline]
2070 pub fn property_if_some(self, name: &'a str, value: Option<impl Into<$crate::glib::Value> + 'a>) -> Self {
2071 if let Some(value) = value {
2072 self.property(name, value)
2073 } else {
2074 self
2075 }
2076 }
2077
2078 #[inline]
2083 pub fn property_from_iter<
2084 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue>,
2085 I: $crate::glib::value::ToSendValue,
2086 >(
2087 self,
2088 name: &'a str,
2089 iter: impl IntoIterator<Item = I>,
2090 ) -> Self {
2091 let iter = iter.into_iter().map(|item| item.to_send_value());
2092 self.property(name, V::from_iter(iter))
2093 }
2094
2095 #[inline]
2101 pub fn property_if_not_empty<
2102 V: $crate::glib::value::ValueType + Into<$crate::glib::Value> + FromIterator<$crate::glib::SendValue>,
2103 I: $crate::glib::value::ToSendValue,
2104 >(
2105 self,
2106 name: &'a str,
2107 iter: impl IntoIterator<Item = I>,
2108 ) -> Self {
2109 let mut iter = iter.into_iter().peekable();
2110 if iter.peek().is_some() {
2111 let iter = iter.map(|item| item.to_send_value());
2112 self.property(name, V::from_iter(iter))
2113 } else {
2114 self
2115 }
2116 }
2117
2118 #[inline]
2124 pub fn property_from_str_if(self, name: &'a str, value: &'a str, predicate: bool) -> Self {
2125 if predicate {
2126 self.property_from_str(name, value)
2127 } else {
2128 self
2129 }
2130 }
2131
2132 #[inline]
2137 pub fn property_from_str_if_some(self, name: &'a str, value: Option<&'a str>) -> Self {
2138 if let Some(value) = value {
2139 self.property_from_str(name, value)
2140 } else {
2141 self
2142 }
2143 }
2144
2145 #[inline]
2148 pub fn name(self, name: impl Into<$crate::glib::GString>) -> Self {
2149 self.property("name", name.into())
2150 }
2151
2152 #[inline]
2158 pub fn name_if(self, name: impl Into<$crate::glib::GString>, predicate: bool) -> Self {
2159 if predicate {
2160 self.name(name)
2161 } else {
2162 self
2163 }
2164 }
2165
2166 #[inline]
2171 pub fn name_if_some(self, name: Option<impl Into<$crate::glib::GString>>) -> Self {
2172 if let Some(name) = name {
2173 self.name(name)
2174 } else {
2175 self
2176 }
2177 }
2178 };
2179);
2180
2181#[cfg(test)]
2182mod tests {
2183 use super::*;
2184
2185 #[test]
2186 fn test_fraction() {
2187 crate::init().unwrap();
2188
2189 let f1 = crate::Fraction::new(1, 2);
2190 let f2 = crate::Fraction::new(2, 3);
2191 let mut f3 = f1 * f2;
2192 let f4 = f1 * f2;
2193 f3 *= f2;
2194 f3 *= f4;
2195
2196 assert_eq!(f3, crate::Fraction::new(2, 27));
2197 }
2198
2199 #[test]
2200 fn test_int_range_constructor() {
2201 crate::init().unwrap();
2202
2203 let _r1 = crate::IntRange::new(1i32, 2i32);
2205 let _r2 = crate::IntRange::with_step(2i64, 10i64, 2i64);
2206 let _r3 = crate::IntRange::with_step(0i64, 6i64, 3i64);
2207 }
2208
2209 #[test]
2210 #[should_panic(expected = "step size must be greater than zero")]
2211 fn test_int_range_constructor_step0() {
2212 crate::init().unwrap();
2213 let _r = crate::IntRange::with_step(0i32, 2i32, 0i32);
2214 }
2215
2216 #[test]
2217 #[should_panic(expected = "maximum value must be greater than minimum value")]
2218 fn test_int_range_constructor_max_min() {
2219 crate::init().unwrap();
2220 let _r = crate::IntRange::with_step(4i32, 2i32, 2i32);
2221 }
2222
2223 #[test]
2224 #[should_panic(expected = "maximum value must be greater than minimum value")]
2225 fn test_int_range_constructor_max_eq_min() {
2226 crate::init().unwrap();
2227 let _r = crate::IntRange::with_step(4i32, 4i32, 2i32);
2228 }
2229
2230 #[test]
2231 #[should_panic(expected = "minimum value must be evenly dividable by step size")]
2232 fn test_int_range_constructor_bad_step() {
2233 crate::init().unwrap();
2234 let _r = crate::IntRange::with_step(1i32, 10i32, 2i32);
2235 }
2236
2237 #[test]
2238 #[should_panic(expected = "maximum value must be evenly dividable by step size")]
2239 fn test_int_range_constructor_bad_step_max() {
2240 crate::init().unwrap();
2241 let _r = crate::IntRange::with_step(0i32, 10i32, 3i32);
2242 }
2243
2244 #[test]
2245 #[should_panic(expected = "step size must be greater than zero")]
2246 fn test_int_range_constructor_step0_i64() {
2247 crate::init().unwrap();
2248 let _r = crate::IntRange::with_step(0i64, 2i64, 0i64);
2249 }
2250
2251 #[test]
2252 #[should_panic(expected = "maximum value must be greater than minimum value")]
2253 fn test_int_range_constructor_max_min_i64() {
2254 crate::init().unwrap();
2255 let _r = crate::IntRange::with_step(4i64, 2i64, 2i64);
2256 }
2257
2258 #[test]
2259 #[should_panic(expected = "maximum value must be greater than minimum value")]
2260 fn test_int_range_constructor_max_eq_min_i64() {
2261 crate::init().unwrap();
2262 let _r = crate::IntRange::with_step(4i64, 4i64, 2i64);
2263 }
2264
2265 #[test]
2266 #[should_panic(expected = "minimum value must be evenly dividable by step size")]
2267 fn test_int_range_constructor_bad_step_i64() {
2268 crate::init().unwrap();
2269 let _r = crate::IntRange::with_step(1i64, 10i64, 2i64);
2270 }
2271 #[test]
2272 #[should_panic(expected = "maximum value must be evenly dividable by step size")]
2273 fn test_int_range_constructor_bad_step_max_i64() {
2274 crate::init().unwrap();
2275 let _r = crate::IntRange::with_step(0i64, 10i64, 3i64);
2276 }
2277
2278 #[test]
2279 fn test_deserialize() {
2280 crate::init().unwrap();
2281
2282 let v = glib::Value::deserialize("123", i32::static_type()).unwrap();
2283 assert_eq!(v.get::<i32>(), Ok(123));
2284 }
2285}