2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2017-07-12 10:25:11 +00:00
|
|
|
|
|
|
|
use num_rational::Rational32;
|
2018-04-01 08:30:03 +00:00
|
|
|
use std::borrow::{Borrow, Cow};
|
2018-08-10 11:22:15 +00:00
|
|
|
use std::cmp;
|
2017-07-12 10:25:11 +00:00
|
|
|
use std::fmt;
|
|
|
|
use std::ops;
|
2017-07-28 13:52:35 +00:00
|
|
|
use std::slice;
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-12-17 19:00:42 +00:00
|
|
|
use glib::translate::{from_glib, FromGlibPtrFull, ToGlibPtr, ToGlibPtrMut, Uninitialized};
|
2018-04-01 08:30:03 +00:00
|
|
|
use glib::value::{FromValue, FromValueOptional, SetValue, ToSendValue, Value};
|
2017-07-12 10:25:11 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
|
|
|
|
pub struct Fraction(pub Rational32);
|
|
|
|
|
|
|
|
impl Fraction {
|
|
|
|
pub fn new(num: i32, den: i32) -> Fraction {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
2017-07-12 10:25:11 +00:00
|
|
|
(num, den).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn approximate_f32(x: f32) -> Option<Fraction> {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
2017-07-12 10:25:11 +00:00
|
|
|
Rational32::approximate_float(x).map(|r| r.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn approximate_f64(x: f64) -> Option<Fraction> {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
2017-07-12 10:25:11 +00:00
|
|
|
Rational32::approximate_float(x).map(|r| r.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Fraction {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Deref for Fraction {
|
|
|
|
type Target = Rational32;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Rational32 {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::DerefMut for Fraction {
|
|
|
|
fn deref_mut(&mut self) -> &mut Rational32 {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<Rational32> for Fraction {
|
|
|
|
fn as_ref(&self) -> &Rational32 {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
macro_rules! impl_fraction_binop {
|
|
|
|
($name:ident, $f:ident, $name_assign:ident, $f_assign:ident) => {
|
|
|
|
impl ops::$name<Fraction> for Fraction {
|
|
|
|
type Output = Fraction;
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
fn $f(self, other: Fraction) -> Fraction {
|
|
|
|
Fraction((self.0).$f(other.0))
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
impl ops::$name<Fraction> for &Fraction {
|
|
|
|
type Output = Fraction;
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
fn $f(self, other: Fraction) -> Fraction {
|
|
|
|
Fraction((self.0).$f(other.0))
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
impl ops::$name<&Fraction> for Fraction {
|
|
|
|
type Output = Fraction;
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
fn $f(self, other: &Fraction) -> Fraction {
|
|
|
|
Fraction((self.0).$f(other.0))
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
impl ops::$name<&Fraction> for &Fraction {
|
|
|
|
type Output = Fraction;
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
fn $f(self, other: &Fraction) -> Fraction {
|
|
|
|
Fraction((self.0).$f(other.0))
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
impl ops::$name<i32> for Fraction {
|
|
|
|
type Output = Fraction;
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
fn $f(self, other: i32) -> Fraction {
|
|
|
|
self.$f(Fraction::from(other))
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
impl ops::$name<i32> for &Fraction {
|
|
|
|
type Output = Fraction;
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
fn $f(self, other: i32) -> Fraction {
|
|
|
|
self.$f(Fraction::from(other))
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
impl ops::$name<&i32> for Fraction {
|
|
|
|
type Output = Fraction;
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
fn $f(self, other: &i32) -> Fraction {
|
|
|
|
self.$f(Fraction::from(*other))
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
impl ops::$name<&i32> for &Fraction {
|
|
|
|
type Output = Fraction;
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
fn $f(self, other: &i32) -> Fraction {
|
|
|
|
self.$f(Fraction::from(*other))
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
impl ops::$name<Fraction> for i32 {
|
|
|
|
type Output = Fraction;
|
2017-07-12 10:25:11 +00:00
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
fn $f(self, other: Fraction) -> Fraction {
|
|
|
|
Fraction::from(self).$f(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::$name<&Fraction> for i32 {
|
|
|
|
type Output = Fraction;
|
|
|
|
|
|
|
|
fn $f(self, other: &Fraction) -> Fraction {
|
|
|
|
Fraction::from(self).$f(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::$name<Fraction> for &i32 {
|
|
|
|
type Output = Fraction;
|
|
|
|
|
|
|
|
fn $f(self, other: Fraction) -> Fraction {
|
|
|
|
Fraction::from(*self).$f(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::$name<&Fraction> for &i32 {
|
|
|
|
type Output = Fraction;
|
|
|
|
|
|
|
|
fn $f(self, other: &Fraction) -> Fraction {
|
|
|
|
Fraction::from(*self).$f(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::$name_assign<Fraction> for Fraction {
|
|
|
|
fn $f_assign(&mut self, other: Fraction) {
|
|
|
|
(self.0).$f_assign(other.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::$name_assign<&Fraction> for Fraction {
|
|
|
|
fn $f_assign(&mut self, other: &Fraction) {
|
|
|
|
(self.0).$f_assign(other.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::$name_assign<i32> for Fraction {
|
|
|
|
fn $f_assign(&mut self, other: i32) {
|
|
|
|
(self.0).$f_assign(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::$name_assign<&i32> for Fraction {
|
|
|
|
fn $f_assign(&mut self, other: &i32) {
|
|
|
|
(self.0).$f_assign(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-07-12 10:25:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
impl_fraction_binop!(Add, add, AddAssign, add_assign);
|
|
|
|
impl_fraction_binop!(Sub, sub, SubAssign, sub_assign);
|
|
|
|
impl_fraction_binop!(Div, div, DivAssign, div_assign);
|
|
|
|
impl_fraction_binop!(Mul, mul, MulAssign, mul_assign);
|
|
|
|
impl_fraction_binop!(Rem, rem, RemAssign, rem_assign);
|
|
|
|
|
|
|
|
impl ops::Neg for Fraction {
|
2017-07-12 10:25:11 +00:00
|
|
|
type Output = Fraction;
|
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
fn neg(self) -> Fraction {
|
|
|
|
Fraction(self.0.neg())
|
2017-07-12 10:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 07:56:40 +00:00
|
|
|
impl ops::Neg for &Fraction {
|
2017-07-12 10:25:11 +00:00
|
|
|
type Output = Fraction;
|
|
|
|
|
|
|
|
fn neg(self) -> Fraction {
|
|
|
|
Fraction(self.0.neg())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<i32> for Fraction {
|
|
|
|
fn from(x: i32) -> Fraction {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
2017-07-12 10:25:11 +00:00
|
|
|
Fraction(x.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<(i32, i32)> for Fraction {
|
|
|
|
fn from(x: (i32, i32)) -> Fraction {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
2017-07-12 10:25:11 +00:00
|
|
|
Fraction(x.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<(i32, i32)> for Fraction {
|
|
|
|
fn into(self) -> (i32, i32) {
|
|
|
|
self.0.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Rational32> for Fraction {
|
|
|
|
fn from(x: Rational32) -> Fraction {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
2017-07-12 10:25:11 +00:00
|
|
|
Fraction(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Fraction> for Rational32 {
|
|
|
|
fn from(x: Fraction) -> Rational32 {
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
2017-07-12 10:25:11 +00:00
|
|
|
x.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl glib::types::StaticType for Fraction {
|
|
|
|
fn static_type() -> glib::types::Type {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { from_glib(ffi::gst_fraction_get_type()) }
|
2017-07-12 10:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValue<'a> for Fraction {
|
|
|
|
unsafe fn from_value(v: &'a Value) -> Fraction {
|
2020-11-21 13:46:48 +00:00
|
|
|
let n = ffi::gst_value_get_fraction_numerator(v.to_glib_none().0);
|
|
|
|
let d = ffi::gst_value_get_fraction_denominator(v.to_glib_none().0);
|
2017-07-12 10:25:11 +00:00
|
|
|
|
|
|
|
Fraction::new(n, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValueOptional<'a> for Fraction {
|
|
|
|
unsafe fn from_value_optional(v: &'a Value) -> Option<Fraction> {
|
|
|
|
Some(Fraction::from_value(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SetValue for Fraction {
|
|
|
|
unsafe fn set_value(v: &mut Value, f: &Self) {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_value_set_fraction(v.to_glib_none_mut().0, *f.numer(), *f.denom());
|
2017-07-12 10:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
2020-11-21 13:46:48 +00:00
|
|
|
#[cfg_attr(feature = "ser_de", derive(serde::Serialize, serde::Deserialize))]
|
2017-07-28 13:52:35 +00:00
|
|
|
pub struct IntRange<T> {
|
|
|
|
min: T,
|
|
|
|
max: T,
|
|
|
|
step: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Copy> IntRange<T> {
|
|
|
|
pub fn min(&self) -> T {
|
|
|
|
self.min
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn max(&self) -> T {
|
|
|
|
self.max
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn step(&self) -> T {
|
|
|
|
self.step
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntRange<i32> {
|
|
|
|
pub fn new(min: i32, max: i32) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
2020-06-11 08:53:35 +00:00
|
|
|
Self::with_step(min, max, 1)
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 08:53:35 +00:00
|
|
|
pub fn with_step(min: i32, max: i32, step: i32) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
assert!(min <= max);
|
|
|
|
assert!(step > 0);
|
|
|
|
|
2018-07-27 10:36:40 +00:00
|
|
|
Self { min, max, step }
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntRange<i64> {
|
|
|
|
pub fn new(min: i64, max: i64) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
2020-06-11 08:53:35 +00:00
|
|
|
Self::with_step(min, max, 1)
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 08:53:35 +00:00
|
|
|
pub fn with_step(min: i64, max: i64, step: i64) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
assert!(min <= max);
|
|
|
|
assert!(step > 0);
|
|
|
|
|
2018-07-27 10:36:40 +00:00
|
|
|
Self { min, max, step }
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<(i32, i32)> for IntRange<i32> {
|
|
|
|
fn from((min, max): (i32, i32)) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
2017-07-28 13:52:35 +00:00
|
|
|
Self::new(min, max)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<(i32, i32, i32)> for IntRange<i32> {
|
|
|
|
fn from((min, max, step): (i32, i32, i32)) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
2020-06-11 08:53:35 +00:00
|
|
|
Self::with_step(min, max, step)
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<(i64, i64)> for IntRange<i64> {
|
|
|
|
fn from((min, max): (i64, i64)) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
2017-07-28 13:52:35 +00:00
|
|
|
Self::new(min, max)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<(i64, i64, i64)> for IntRange<i64> {
|
|
|
|
fn from((min, max, step): (i64, i64, i64)) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
2020-06-11 08:53:35 +00:00
|
|
|
Self::with_step(min, max, step)
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl glib::types::StaticType for IntRange<i32> {
|
|
|
|
fn static_type() -> glib::types::Type {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { from_glib(ffi::gst_int_range_get_type()) }
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValue<'a> for IntRange<i32> {
|
|
|
|
unsafe fn from_value(v: &'a Value) -> Self {
|
2020-11-21 13:46:48 +00:00
|
|
|
let min = ffi::gst_value_get_int_range_min(v.to_glib_none().0);
|
|
|
|
let max = ffi::gst_value_get_int_range_max(v.to_glib_none().0);
|
|
|
|
let step = ffi::gst_value_get_int_range_step(v.to_glib_none().0);
|
2017-07-28 13:52:35 +00:00
|
|
|
|
2020-06-11 08:53:35 +00:00
|
|
|
Self::with_step(min, max, step)
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValueOptional<'a> for IntRange<i32> {
|
|
|
|
unsafe fn from_value_optional(v: &'a Value) -> Option<Self> {
|
|
|
|
Some(Self::from_value(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SetValue for IntRange<i32> {
|
|
|
|
unsafe fn set_value(v: &mut Value, r: &Self) {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_value_set_int_range_step(v.to_glib_none_mut().0, r.min(), r.max(), r.step());
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl glib::types::StaticType for IntRange<i64> {
|
|
|
|
fn static_type() -> glib::types::Type {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { from_glib(ffi::gst_int64_range_get_type()) }
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValue<'a> for IntRange<i64> {
|
|
|
|
unsafe fn from_value(v: &'a Value) -> Self {
|
2020-11-21 13:46:48 +00:00
|
|
|
let min = ffi::gst_value_get_int64_range_min(v.to_glib_none().0);
|
|
|
|
let max = ffi::gst_value_get_int64_range_max(v.to_glib_none().0);
|
|
|
|
let step = ffi::gst_value_get_int64_range_step(v.to_glib_none().0);
|
2017-07-28 13:52:35 +00:00
|
|
|
|
2020-06-11 08:53:35 +00:00
|
|
|
Self::with_step(min, max, step)
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValueOptional<'a> for IntRange<i64> {
|
|
|
|
unsafe fn from_value_optional(v: &'a Value) -> Option<Self> {
|
|
|
|
Some(Self::from_value(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SetValue for IntRange<i64> {
|
|
|
|
unsafe fn set_value(v: &mut Value, r: &Self) {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_value_set_int64_range_step(v.to_glib_none_mut().0, r.min(), r.max(), r.step());
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
2020-11-21 13:46:48 +00:00
|
|
|
#[cfg_attr(feature = "ser_de", derive(serde::Serialize, serde::Deserialize))]
|
2017-07-28 13:52:35 +00:00
|
|
|
pub struct FractionRange {
|
|
|
|
min: Fraction,
|
|
|
|
max: Fraction,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FractionRange {
|
|
|
|
pub fn new<T: Into<Fraction>, U: Into<Fraction>>(min: T, max: U) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
let min = min.into();
|
|
|
|
let max = max.into();
|
|
|
|
|
|
|
|
assert!(min <= max);
|
|
|
|
|
2018-07-20 07:21:06 +00:00
|
|
|
FractionRange { min, max }
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn min(&self) -> Fraction {
|
|
|
|
self.min
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn max(&self) -> Fraction {
|
|
|
|
self.max
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<(Fraction, Fraction)> for FractionRange {
|
|
|
|
fn from((min, max): (Fraction, Fraction)) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
Self::new(min, max)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl glib::types::StaticType for FractionRange {
|
|
|
|
fn static_type() -> glib::types::Type {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { from_glib(ffi::gst_fraction_range_get_type()) }
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValue<'a> for FractionRange {
|
|
|
|
unsafe fn from_value(v: &'a Value) -> Self {
|
2020-11-21 13:46:48 +00:00
|
|
|
let min = ffi::gst_value_get_fraction_range_min(v.to_glib_none().0);
|
|
|
|
let max = ffi::gst_value_get_fraction_range_max(v.to_glib_none().0);
|
2017-07-28 13:52:35 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
let min_n = ffi::gst_value_get_fraction_numerator(min);
|
|
|
|
let min_d = ffi::gst_value_get_fraction_denominator(min);
|
|
|
|
let max_n = ffi::gst_value_get_fraction_numerator(max);
|
|
|
|
let max_d = ffi::gst_value_get_fraction_denominator(max);
|
2017-07-28 13:52:35 +00:00
|
|
|
|
|
|
|
Self::new((min_n, min_d), (max_n, max_d))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValueOptional<'a> for FractionRange {
|
|
|
|
unsafe fn from_value_optional(v: &'a Value) -> Option<Self> {
|
|
|
|
Some(Self::from_value(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SetValue for FractionRange {
|
|
|
|
unsafe fn set_value(v: &mut Value, r: &Self) {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_value_set_fraction_range_full(
|
2017-07-31 11:16:42 +00:00
|
|
|
v.to_glib_none_mut().0,
|
|
|
|
*r.min().numer(),
|
|
|
|
*r.min().denom(),
|
|
|
|
*r.max().numer(),
|
|
|
|
*r.max().denom(),
|
|
|
|
);
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 17:00:23 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
2020-11-21 13:46:48 +00:00
|
|
|
#[cfg_attr(feature = "ser_de", derive(serde::Serialize, serde::Deserialize))]
|
2017-12-17 09:57:37 +00:00
|
|
|
pub struct Bitmask(pub u64);
|
2017-08-09 17:00:23 +00:00
|
|
|
|
|
|
|
impl Bitmask {
|
|
|
|
pub fn new(v: u64) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
2017-08-09 17:00:23 +00:00
|
|
|
Bitmask(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Deref for Bitmask {
|
|
|
|
type Target = u64;
|
|
|
|
|
|
|
|
fn deref(&self) -> &u64 {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::DerefMut for Bitmask {
|
|
|
|
fn deref_mut(&mut self) -> &mut u64 {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::BitAnd for Bitmask {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn bitand(self, rhs: Self) -> Self {
|
|
|
|
Bitmask(self.0.bitand(rhs.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::BitOr for Bitmask {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn bitor(self, rhs: Self) -> Self {
|
|
|
|
Bitmask(self.0.bitor(rhs.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::BitXor for Bitmask {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn bitxor(self, rhs: Self) -> Self {
|
|
|
|
Bitmask(self.0.bitxor(rhs.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Not for Bitmask {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn not(self) -> Self {
|
|
|
|
Bitmask(self.0.not())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u64> for Bitmask {
|
|
|
|
fn from(v: u64) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
2017-08-09 17:00:23 +00:00
|
|
|
Self::new(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl glib::types::StaticType for Bitmask {
|
|
|
|
fn static_type() -> glib::types::Type {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { from_glib(ffi::gst_bitmask_get_type()) }
|
2017-08-09 17:00:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValue<'a> for Bitmask {
|
|
|
|
unsafe fn from_value(v: &'a Value) -> Self {
|
2020-11-21 13:46:48 +00:00
|
|
|
let v = ffi::gst_value_get_bitmask(v.to_glib_none().0);
|
2017-08-09 17:00:23 +00:00
|
|
|
Self::new(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValueOptional<'a> for Bitmask {
|
|
|
|
unsafe fn from_value_optional(v: &'a Value) -> Option<Self> {
|
|
|
|
Some(Self::from_value(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SetValue for Bitmask {
|
|
|
|
unsafe fn set_value(v: &mut Value, r: &Self) {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_value_set_bitmask(v.to_glib_none_mut().0, r.0);
|
2017-08-09 17:00:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2017-12-17 12:06:22 +00:00
|
|
|
pub struct Array<'a>(Cow<'a, [glib::SendValue]>);
|
2017-07-28 13:52:35 +00:00
|
|
|
|
2017-11-15 17:18:58 +00:00
|
|
|
unsafe impl<'a> Send for Array<'a> {}
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
impl<'a> Array<'a> {
|
2019-06-06 06:09:34 +00:00
|
|
|
pub fn new(values: &[&dyn ToSendValue]) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
2017-12-17 12:06:22 +00:00
|
|
|
Array(values.iter().map(|v| v.to_send_value()).collect())
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 13:14:10 +00:00
|
|
|
pub fn from_borrowed<T: AsRef<[glib::SendValue]>>(values: &'a T) -> Self {
|
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
|
|
|
Array(Cow::Borrowed(values.as_ref()))
|
|
|
|
}
|
|
|
|
|
2018-07-16 19:46:10 +00:00
|
|
|
pub fn from_owned(values: Vec<glib::SendValue>) -> Self {
|
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
|
|
|
Array(Cow::Owned(values))
|
|
|
|
}
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
pub fn into_owned(self) -> Array<'static> {
|
|
|
|
Array(self.0.into_owned().into())
|
|
|
|
}
|
|
|
|
|
2017-12-17 12:06:22 +00:00
|
|
|
pub fn as_slice(&self) -> &[glib::SendValue] {
|
2017-07-28 13:52:35 +00:00
|
|
|
self.0.borrow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 06:09:34 +00:00
|
|
|
impl<'a> From<&'a [&'a dyn ToSendValue]> for Array<'a> {
|
|
|
|
fn from(values: &'a [&'a dyn ToSendValue]) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
Self::new(values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 12:06:22 +00:00
|
|
|
impl<'a> From<&'a [glib::SendValue]> for Array<'a> {
|
|
|
|
fn from(values: &'a [glib::SendValue]) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
Array(Cow::Borrowed(values))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValue<'a> for Array<'a> {
|
|
|
|
unsafe fn from_value(v: &'a Value) -> Self {
|
2020-11-21 13:46:48 +00:00
|
|
|
let arr = (*v.to_glib_none().0).data[0].v_pointer as *const glib::ffi::GArray;
|
2017-07-28 13:52:35 +00:00
|
|
|
if arr.is_null() {
|
|
|
|
Array(Cow::Borrowed(&[]))
|
|
|
|
} else {
|
2019-02-28 08:32:13 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2017-07-31 11:16:42 +00:00
|
|
|
Array(Cow::Borrowed(slice::from_raw_parts(
|
2017-12-17 12:06:22 +00:00
|
|
|
(*arr).data as *const glib::SendValue,
|
2017-07-31 11:16:42 +00:00
|
|
|
(*arr).len as usize,
|
|
|
|
)))
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValueOptional<'a> for Array<'a> {
|
|
|
|
unsafe fn from_value_optional(v: &'a Value) -> Option<Self> {
|
|
|
|
Some(Array::from_value(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> SetValue for Array<'a> {
|
|
|
|
unsafe fn set_value(v: &mut Value, a: &Self) {
|
|
|
|
for value in a.as_slice() {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_value_array_append_value(v.to_glib_none_mut().0, value.to_glib_none().0);
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> glib::types::StaticType for Array<'a> {
|
|
|
|
fn static_type() -> glib::types::Type {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { from_glib(ffi::gst_value_array_get_type()) }
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2017-12-17 12:06:22 +00:00
|
|
|
pub struct List<'a>(Cow<'a, [glib::SendValue]>);
|
2017-07-28 13:52:35 +00:00
|
|
|
|
2017-11-15 17:18:58 +00:00
|
|
|
unsafe impl<'a> Send for List<'a> {}
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
impl<'a> List<'a> {
|
2019-06-06 06:09:34 +00:00
|
|
|
pub fn new(values: &[&dyn ToSendValue]) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
2017-12-17 12:06:22 +00:00
|
|
|
List(values.iter().map(|v| v.to_send_value()).collect())
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 13:14:10 +00:00
|
|
|
pub fn from_borrowed<T: AsRef<[glib::SendValue]>>(values: &'a T) -> Self {
|
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
|
|
|
List(Cow::Borrowed(values.as_ref()))
|
|
|
|
}
|
|
|
|
|
2018-07-16 19:46:10 +00:00
|
|
|
pub fn from_owned(values: Vec<glib::SendValue>) -> Self {
|
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
|
|
|
List(Cow::Owned(values))
|
|
|
|
}
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
pub fn into_owned(self) -> List<'static> {
|
|
|
|
List(self.0.into_owned().into())
|
|
|
|
}
|
|
|
|
|
2017-12-17 12:06:22 +00:00
|
|
|
pub fn as_slice(&self) -> &[glib::SendValue] {
|
2017-07-28 13:52:35 +00:00
|
|
|
self.0.borrow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 06:09:34 +00:00
|
|
|
impl<'a> From<&'a [&'a dyn ToSendValue]> for List<'a> {
|
|
|
|
fn from(values: &'a [&'a dyn ToSendValue]) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
Self::new(values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 12:06:22 +00:00
|
|
|
impl<'a> From<&'a [glib::SendValue]> for List<'a> {
|
|
|
|
fn from(values: &'a [glib::SendValue]) -> Self {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
2017-07-28 13:52:35 +00:00
|
|
|
List(Cow::Borrowed(values))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValue<'a> for List<'a> {
|
|
|
|
unsafe fn from_value(v: &'a Value) -> Self {
|
2020-11-21 13:46:48 +00:00
|
|
|
let arr = (*v.to_glib_none().0).data[0].v_pointer as *const glib::ffi::GArray;
|
2017-07-28 13:52:35 +00:00
|
|
|
if arr.is_null() {
|
|
|
|
List(Cow::Borrowed(&[]))
|
|
|
|
} else {
|
2019-02-28 08:32:13 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2017-07-31 11:16:42 +00:00
|
|
|
List(Cow::Borrowed(slice::from_raw_parts(
|
2017-12-17 12:06:22 +00:00
|
|
|
(*arr).data as *const glib::SendValue,
|
2017-07-31 11:16:42 +00:00
|
|
|
(*arr).len as usize,
|
|
|
|
)))
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromValueOptional<'a> for List<'a> {
|
|
|
|
unsafe fn from_value_optional(v: &'a Value) -> Option<Self> {
|
|
|
|
Some(List::from_value(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> SetValue for List<'a> {
|
|
|
|
unsafe fn set_value(v: &mut Value, a: &Self) {
|
|
|
|
for value in a.as_slice() {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_value_list_append_value(v.to_glib_none_mut().0, value.to_glib_none().0);
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> glib::types::StaticType for List<'a> {
|
|
|
|
fn static_type() -> glib::types::Type {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { from_glib(ffi::gst_value_list_get_type()) }
|
2017-07-28 13:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 15:47:00 +00:00
|
|
|
pub trait GstValueExt: Sized {
|
|
|
|
fn can_compare(&self, other: &Self) -> bool;
|
2018-08-10 11:22:15 +00:00
|
|
|
fn compare(&self, other: &Self) -> Option<cmp::Ordering>;
|
|
|
|
fn eq(&self, other: &Self) -> bool;
|
2017-07-28 15:47:00 +00:00
|
|
|
fn can_intersect(&self, other: &Self) -> bool;
|
|
|
|
fn intersect(&self, other: &Self) -> Option<Self>;
|
|
|
|
fn can_subtract(&self, other: &Self) -> bool;
|
|
|
|
fn subtract(&self, other: &Self) -> Option<Self>;
|
|
|
|
fn can_union(&self, other: &Self) -> bool;
|
|
|
|
fn union(&self, other: &Self) -> Option<Self>;
|
|
|
|
fn fixate(&self) -> Option<Self>;
|
|
|
|
fn is_fixed(&self) -> bool;
|
|
|
|
fn is_subset(&self, superset: &Self) -> bool;
|
2019-12-17 19:00:42 +00:00
|
|
|
fn serialize(&self) -> Result<glib::GString, glib::BoolError>;
|
|
|
|
fn deserialize<'a, T: Into<&'a str>>(s: T) -> Result<glib::Value, glib::BoolError>;
|
2017-07-28 15:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GstValueExt for glib::Value {
|
|
|
|
fn can_compare(&self, other: &Self) -> bool {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
from_glib(ffi::gst_value_can_compare(
|
2017-07-31 11:16:42 +00:00
|
|
|
self.to_glib_none().0,
|
|
|
|
other.to_glib_none().0,
|
|
|
|
))
|
2017-07-28 15:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 11:22:15 +00:00
|
|
|
fn compare(&self, other: &Self) -> Option<cmp::Ordering> {
|
2017-07-28 15:47:00 +00:00
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
let val = ffi::gst_value_compare(self.to_glib_none().0, other.to_glib_none().0);
|
2018-08-10 11:22:15 +00:00
|
|
|
|
|
|
|
match val {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::GST_VALUE_LESS_THAN => Some(cmp::Ordering::Less),
|
|
|
|
ffi::GST_VALUE_EQUAL => Some(cmp::Ordering::Equal),
|
|
|
|
ffi::GST_VALUE_GREATER_THAN => Some(cmp::Ordering::Greater),
|
2018-08-10 11:22:15 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
2017-07-28 15:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 11:22:15 +00:00
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.compare(other) == Some(cmp::Ordering::Equal)
|
|
|
|
}
|
|
|
|
|
2017-07-28 15:47:00 +00:00
|
|
|
fn can_intersect(&self, other: &Self) -> bool {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
from_glib(ffi::gst_value_can_intersect(
|
2017-07-31 11:16:42 +00:00
|
|
|
self.to_glib_none().0,
|
|
|
|
other.to_glib_none().0,
|
|
|
|
))
|
2017-07-28 15:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn intersect(&self, other: &Self) -> Option<Self> {
|
|
|
|
unsafe {
|
|
|
|
let mut value = glib::Value::uninitialized();
|
2020-11-21 13:46:48 +00:00
|
|
|
let ret: bool = from_glib(ffi::gst_value_intersect(
|
2017-07-31 11:16:42 +00:00
|
|
|
value.to_glib_none_mut().0,
|
|
|
|
self.to_glib_none().0,
|
|
|
|
other.to_glib_none().0,
|
|
|
|
));
|
2017-07-28 15:47:00 +00:00
|
|
|
if ret {
|
|
|
|
Some(value)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn can_subtract(&self, other: &Self) -> bool {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
from_glib(ffi::gst_value_can_subtract(
|
2017-07-31 11:16:42 +00:00
|
|
|
self.to_glib_none().0,
|
|
|
|
other.to_glib_none().0,
|
|
|
|
))
|
2017-07-28 15:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn subtract(&self, other: &Self) -> Option<Self> {
|
|
|
|
unsafe {
|
|
|
|
let mut value = glib::Value::uninitialized();
|
2020-11-21 13:46:48 +00:00
|
|
|
let ret: bool = from_glib(ffi::gst_value_subtract(
|
2017-07-31 11:16:42 +00:00
|
|
|
value.to_glib_none_mut().0,
|
|
|
|
self.to_glib_none().0,
|
|
|
|
other.to_glib_none().0,
|
|
|
|
));
|
2017-07-28 15:47:00 +00:00
|
|
|
if ret {
|
|
|
|
Some(value)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn can_union(&self, other: &Self) -> bool {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
from_glib(ffi::gst_value_can_union(
|
2017-07-31 11:16:42 +00:00
|
|
|
self.to_glib_none().0,
|
|
|
|
other.to_glib_none().0,
|
|
|
|
))
|
2017-07-28 15:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn union(&self, other: &Self) -> Option<Self> {
|
|
|
|
unsafe {
|
|
|
|
let mut value = glib::Value::uninitialized();
|
2020-11-21 13:46:48 +00:00
|
|
|
let ret: bool = from_glib(ffi::gst_value_union(
|
2017-07-31 11:16:42 +00:00
|
|
|
value.to_glib_none_mut().0,
|
|
|
|
self.to_glib_none().0,
|
|
|
|
other.to_glib_none().0,
|
|
|
|
));
|
2017-07-28 15:47:00 +00:00
|
|
|
if ret {
|
|
|
|
Some(value)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fixate(&self) -> Option<Self> {
|
|
|
|
unsafe {
|
|
|
|
let mut value = glib::Value::uninitialized();
|
2020-11-21 13:46:48 +00:00
|
|
|
let ret: bool = from_glib(ffi::gst_value_fixate(
|
2017-07-31 11:16:42 +00:00
|
|
|
value.to_glib_none_mut().0,
|
|
|
|
self.to_glib_none().0,
|
|
|
|
));
|
2017-07-28 15:47:00 +00:00
|
|
|
if ret {
|
|
|
|
Some(value)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_fixed(&self) -> bool {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { from_glib(ffi::gst_value_is_fixed(self.to_glib_none().0)) }
|
2017-07-28 15:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_subset(&self, superset: &Self) -> bool {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
from_glib(ffi::gst_value_is_subset(
|
2017-07-31 11:16:42 +00:00
|
|
|
self.to_glib_none().0,
|
|
|
|
superset.to_glib_none().0,
|
|
|
|
))
|
2017-07-28 15:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 19:00:42 +00:00
|
|
|
fn serialize(&self) -> Result<glib::GString, glib::BoolError> {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
Option::<_>::from_glib_full(ffi::gst_value_serialize(self.to_glib_none().0))
|
2020-12-17 22:38:06 +00:00
|
|
|
.ok_or_else(|| glib::bool_error!("Failed to serialize value"))
|
2019-12-17 19:00:42 +00:00
|
|
|
}
|
2017-07-28 15:47:00 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 19:00:42 +00:00
|
|
|
fn deserialize<'a, T: Into<&'a str>>(s: T) -> Result<glib::Value, glib::BoolError> {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
2017-07-28 15:47:00 +00:00
|
|
|
let s = s.into();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut value = glib::Value::uninitialized();
|
2020-11-21 13:46:48 +00:00
|
|
|
let ret: bool = from_glib(ffi::gst_value_deserialize(
|
2017-07-31 11:16:42 +00:00
|
|
|
value.to_glib_none_mut().0,
|
|
|
|
s.to_glib_none().0,
|
|
|
|
));
|
2017-07-28 15:47:00 +00:00
|
|
|
if ret {
|
2019-12-17 19:00:42 +00:00
|
|
|
Ok(value)
|
2017-07-28 15:47:00 +00:00
|
|
|
} else {
|
2020-12-17 22:38:06 +00:00
|
|
|
Err(glib::bool_error!("Failed to deserialize value"))
|
2017-07-28 15:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 07:56:40 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn test_fraction() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2019-06-03 07:56:40 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
let f1 = crate::Fraction::new(1, 2);
|
|
|
|
let f2 = crate::Fraction::new(2, 3);
|
2019-06-03 07:56:40 +00:00
|
|
|
let mut f3 = f1 * f2;
|
2019-12-22 07:59:23 +00:00
|
|
|
let f4 = f1 * f2;
|
2019-06-03 07:56:40 +00:00
|
|
|
f3 *= f2;
|
2019-12-22 07:59:23 +00:00
|
|
|
f3 *= f4;
|
2019-06-03 07:56:40 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
assert_eq!(f3, crate::Fraction::new(2, 27));
|
2019-06-03 07:56:40 +00:00
|
|
|
}
|
|
|
|
}
|