gstreamer: Refactor gst::IntRange constructors to not require specifying the contained type necessarily

This commit is contained in:
Sebastian Dröge 2021-11-05 22:39:33 +02:00
parent 9901f0c6a2
commit a3015ab507
4 changed files with 43 additions and 16 deletions

View file

@ -59,8 +59,8 @@ pub fn audio_make_raw_caps(
let builder = gst::caps::Caps::builder("audio/x-raw") let builder = gst::caps::Caps::builder("audio/x-raw")
.field("format", gst::List::from_values(formats)) .field("format", gst::List::from_values(formats))
.field("rate", gst::IntRange::<i32>::new(1, i32::MAX)) .field("rate", gst::IntRange::new(1, i32::MAX))
.field("channels", gst::IntRange::<i32>::new(1, i32::MAX)); .field("channels", gst::IntRange::new(1, i32::MAX));
match layout { match layout {
crate::AudioLayout::Interleaved => builder.field("layout", "interleaved"), crate::AudioLayout::Interleaved => builder.field("layout", "interleaved"),

View file

@ -209,8 +209,8 @@ pub fn video_make_raw_caps(
gst::caps::Caps::builder("video/x-raw") gst::caps::Caps::builder("video/x-raw")
.field("format", gst::List::from_values(formats)) .field("format", gst::List::from_values(formats))
.field("width", gst::IntRange::<i32>::new(1, i32::MAX)) .field("width", gst::IntRange::new(1, i32::MAX))
.field("height", gst::IntRange::<i32>::new(1, i32::MAX)) .field("height", gst::IntRange::new(1, i32::MAX))
.field( .field(
"framerate", "framerate",
gst::FractionRange::new(gst::Fraction::new(0, 1), gst::Fraction::new(i32::MAX, 1)), gst::FractionRange::new(gst::Fraction::new(0, 1), gst::Fraction::new(i32::MAX, 1)),

View file

@ -295,35 +295,53 @@ impl<T: Copy> IntRange<T> {
} }
} }
impl IntRange<i32> { #[doc(hidden)]
pub fn new(min: i32, max: i32) -> Self { pub trait IntRangeType: Sized + Clone + Copy + 'static {
fn with_min_max(min: Self, max: Self) -> IntRange<Self>;
fn with_step(min: Self, max: Self, step: Self) -> IntRange<Self>;
}
impl IntRangeType for i32 {
fn with_min_max(min: i32, max: i32) -> IntRange<Self> {
skip_assert_initialized!(); skip_assert_initialized!();
Self::with_step(min, max, 1) IntRange { min, max, step: 1 }
} }
pub fn with_step(min: i32, max: i32, step: i32) -> Self { fn with_step(min: i32, max: i32, step: i32) -> IntRange<Self> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
assert!(min <= max); assert!(min <= max);
assert!(step > 0); assert!(step > 0);
Self { min, max, step } IntRange { min, max, step }
} }
} }
impl IntRange<i64> { impl IntRangeType for i64 {
pub fn new(min: i64, max: i64) -> Self { fn with_min_max(min: i64, max: i64) -> IntRange<Self> {
skip_assert_initialized!(); skip_assert_initialized!();
Self::with_step(min, max, 1) IntRange { min, max, step: 1 }
} }
pub fn with_step(min: i64, max: i64, step: i64) -> Self { fn with_step(min: i64, max: i64, step: i64) -> IntRange<Self> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
assert!(min <= max); assert!(min <= max);
assert!(step > 0); assert!(step > 0);
Self { min, max, step } IntRange { min, max, step }
}
}
impl<T: IntRangeType> IntRange<T> {
pub fn new(min: T, max: T) -> IntRange<T> {
assert_initialized_main_thread!();
T::with_min_max(min, max)
}
pub fn with_step(min: T, max: T, step: T) -> IntRange<T> {
assert_initialized_main_thread!();
T::with_step(min, max, step)
} }
} }
@ -1193,6 +1211,15 @@ mod tests {
assert_eq!(f3, crate::Fraction::new(2, 27)); assert_eq!(f3, crate::Fraction::new(2, 27));
} }
#[test]
fn test_int_range_constructor() {
crate::init().unwrap();
// Type inference should figure out the type
let _r1 = crate::IntRange::new(1i32, 2i32);
let _r2 = crate::IntRange::with_step(2i64, 3i64, 4i64);
}
#[test] #[test]
fn test_deserialize() { fn test_deserialize() {
crate::init().unwrap(); crate::init().unwrap();

View file

@ -322,7 +322,7 @@ mod tests {
assert_eq!(r#"{"min":[1,3],"max":[1,2]}"#.to_owned(), res); assert_eq!(r#"{"min":[1,3],"max":[1,2]}"#.to_owned(), res);
// IntRange // IntRange
let int_range = IntRange::<i32>::with_step(0, 42, 21); let int_range = IntRange::with_step(0, 42, 21);
let res = ron::ser::to_string_pretty(&int_range, pretty_config.clone()); let res = ron::ser::to_string_pretty(&int_range, pretty_config.clone());
assert_eq!(Ok("( min: 0, max: 42, step: 21,)".to_owned()), res,); assert_eq!(Ok("( min: 0, max: 42, step: 21,)".to_owned()), res,);
@ -455,7 +455,7 @@ mod tests {
); );
// IntRange // IntRange
let int_range = IntRange::<i32>::with_step(0, 42, 21); let int_range = IntRange::with_step(0, 42, 21);
let int_range_ser = ron::ser::to_string(&int_range).unwrap(); let int_range_ser = ron::ser::to_string(&int_range).unwrap();
let int_range_de: IntRange<i32> = ron::de::from_str(int_range_ser.as_str()).unwrap(); let int_range_de: IntRange<i32> = ron::de::from_str(int_range_ser.as_str()).unwrap();
assert_eq!(int_range_de.min(), int_range.min()); assert_eq!(int_range_de.min(), int_range.min());