gstreamer: Add various convenience From impls for Caps, BufferList, CapsFeatures, Buffer and VideoOverlayComposition

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1199>
This commit is contained in:
Sebastian Dröge 2023-01-23 14:31:03 +02:00 committed by GStreamer Marge Bot
parent eadb3c6db2
commit dc5e408c2d
5 changed files with 280 additions and 2 deletions

View file

@ -2,7 +2,7 @@
use std::{fmt, mem}; use std::{fmt, mem};
use glib::translate::{from_glib, from_glib_full, from_glib_none, IntoGlib, ToGlibPtr}; use glib::translate::*;
gst::mini_object_wrapper!( gst::mini_object_wrapper!(
VideoOverlayRectangle, VideoOverlayRectangle,
@ -341,6 +341,72 @@ impl<'a> IntoIterator for &'a VideoOverlayComposition {
} }
} }
impl From<VideoOverlayRectangle> for VideoOverlayComposition {
fn from(value: VideoOverlayRectangle) -> Self {
skip_assert_initialized!();
unsafe {
Self::from_glib_full(ffi::gst_video_overlay_composition_new(
value.into_glib_ptr(),
))
}
}
}
impl<'a> From<&'a VideoOverlayRectangle> for VideoOverlayComposition {
fn from(value: &'a VideoOverlayRectangle) -> Self {
skip_assert_initialized!();
unsafe { Self::from_glib_full(ffi::gst_video_overlay_composition_new(value.as_mut_ptr())) }
}
}
#[cfg(any(feature = "v1_20", feature = "dox"))]
impl<const N: usize> From<[VideoOverlayRectangle; N]> for VideoOverlayComposition {
fn from(value: [VideoOverlayRectangle; N]) -> Self {
assert_initialized_main_thread!();
unsafe {
use std::ptr;
let composition =
Self::from_glib_full(ffi::gst_video_overlay_composition_new(ptr::null_mut()));
value.into_iter().for_each(|rect| {
ffi::gst_video_overlay_composition_add_rectangle(
composition.as_mut_ptr(),
rect.into_glib_ptr(),
);
});
composition
}
}
}
#[cfg(any(feature = "v1_20", feature = "dox"))]
impl<'a, const N: usize> From<[&'a VideoOverlayRectangle; N]> for VideoOverlayComposition {
fn from(value: [&'a VideoOverlayRectangle; N]) -> Self {
assert_initialized_main_thread!();
unsafe {
use std::ptr;
let composition =
Self::from_glib_full(ffi::gst_video_overlay_composition_new(ptr::null_mut()));
value.into_iter().for_each(|rect| {
ffi::gst_video_overlay_composition_add_rectangle(
composition.as_mut_ptr(),
rect.as_mut_ptr(),
);
});
composition
}
}
}
#[cfg(any(feature = "v1_20", feature = "dox"))] #[cfg(any(feature = "v1_20", feature = "dox"))]
impl std::iter::FromIterator<VideoOverlayRectangle> for VideoOverlayComposition { impl std::iter::FromIterator<VideoOverlayRectangle> for VideoOverlayComposition {
fn from_iter<T: IntoIterator<Item = VideoOverlayRectangle>>(iter: T) -> Self { fn from_iter<T: IntoIterator<Item = VideoOverlayRectangle>>(iter: T) -> Self {
@ -355,7 +421,7 @@ impl std::iter::FromIterator<VideoOverlayRectangle> for VideoOverlayComposition
iter.into_iter().for_each(|rect| { iter.into_iter().for_each(|rect| {
ffi::gst_video_overlay_composition_add_rectangle( ffi::gst_video_overlay_composition_add_rectangle(
composition.as_mut_ptr(), composition.as_mut_ptr(),
rect.as_mut_ptr(), rect.into_glib_ptr(),
); );
}); });

View file

@ -998,6 +998,32 @@ impl<'a> IntoIterator for &'a BufferRef {
} }
} }
impl From<Memory> for Buffer {
fn from(value: Memory) -> Self {
skip_assert_initialized!();
let mut buffer = Buffer::new();
{
let buffer = buffer.get_mut().unwrap();
buffer.append_memory(value);
}
buffer
}
}
impl<const N: usize> From<[Memory; N]> for Buffer {
fn from(value: [Memory; N]) -> Self {
skip_assert_initialized!();
let mut buffer = Buffer::new();
{
let buffer = buffer.get_mut().unwrap();
value.into_iter().for_each(|b| buffer.append_memory(b));
}
buffer
}
}
impl std::iter::FromIterator<Memory> for Buffer { impl std::iter::FromIterator<Memory> for Buffer {
fn from_iter<T: IntoIterator<Item = Memory>>(iter: T) -> Self { fn from_iter<T: IntoIterator<Item = Memory>>(iter: T) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();

View file

@ -315,6 +315,32 @@ impl<'a> IntoIterator for &'a BufferListRef {
} }
} }
impl From<Buffer> for BufferList {
fn from(value: Buffer) -> Self {
skip_assert_initialized!();
let mut list = BufferList::new_sized(1);
{
let list = list.get_mut().unwrap();
list.add(value);
}
list
}
}
impl<const N: usize> From<[Buffer; N]> for BufferList {
fn from(value: [Buffer; N]) -> Self {
skip_assert_initialized!();
let mut list = BufferList::new_sized(N);
{
let list = list.get_mut().unwrap();
value.into_iter().for_each(|b| list.add(b));
}
list
}
}
impl std::iter::FromIterator<Buffer> for BufferList { impl std::iter::FromIterator<Buffer> for BufferList {
fn from_iter<T: IntoIterator<Item = Buffer>>(iter: T) -> Self { fn from_iter<T: IntoIterator<Item = Buffer>>(iter: T) -> Self {
assert_initialized_main_thread!(); assert_initialized_main_thread!();

View file

@ -157,6 +157,78 @@ impl str::FromStr for Caps {
} }
} }
impl From<Structure> for Caps {
fn from(v: Structure) -> Caps {
skip_assert_initialized!();
let mut caps = Caps::new_empty();
{
let caps = caps.get_mut().unwrap();
caps.append_structure(v);
}
caps
}
}
impl<const N: usize> From<[Structure; N]> for Caps {
fn from(v: [Structure; N]) -> Caps {
skip_assert_initialized!();
let mut caps = Caps::new_empty();
{
let caps = caps.get_mut().unwrap();
v.into_iter().for_each(|s| caps.append_structure(s));
}
caps
}
}
impl From<(Structure, CapsFeatures)> for Caps {
fn from(v: (Structure, CapsFeatures)) -> Caps {
skip_assert_initialized!();
let mut caps = Caps::new_empty();
{
let caps = caps.get_mut().unwrap();
caps.append_structure_full(v.0, Some(v.1));
}
caps
}
}
impl<const N: usize> From<[(Structure, CapsFeatures); N]> for Caps {
fn from(v: [(Structure, CapsFeatures); N]) -> Caps {
skip_assert_initialized!();
let mut caps = Caps::new_empty();
{
let caps = caps.get_mut().unwrap();
v.into_iter()
.for_each(|s| caps.append_structure_full(s.0, Some(s.1)));
}
caps
}
}
impl<const N: usize> From<[(Structure, Option<CapsFeatures>); N]> for Caps {
fn from(v: [(Structure, Option<CapsFeatures>); N]) -> Caps {
skip_assert_initialized!();
let mut caps = Caps::new_empty();
{
let caps = caps.get_mut().unwrap();
v.into_iter()
.for_each(|s| caps.append_structure_full(s.0, s.1));
}
caps
}
}
impl std::iter::FromIterator<Structure> for Caps { impl std::iter::FromIterator<Structure> for Caps {
fn from_iter<T: IntoIterator<Item = Structure>>(iter: T) -> Self { fn from_iter<T: IntoIterator<Item = Structure>>(iter: T) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();

View file

@ -669,6 +669,94 @@ impl<'a> IntoIterator for &'a CapsFeaturesRef {
} }
} }
impl<'a> From<&'a str> for CapsFeatures {
fn from(value: &'a str) -> Self {
skip_assert_initialized!();
let mut features = CapsFeatures::new_empty();
features.add(value);
features
}
}
impl<'a> From<&'a glib::GStr> for CapsFeatures {
fn from(value: &'a glib::GStr) -> Self {
skip_assert_initialized!();
let mut features = CapsFeatures::new_empty();
features.add(value);
features
}
}
impl From<glib::Quark> for CapsFeatures {
fn from(value: glib::Quark) -> Self {
skip_assert_initialized!();
let mut features = CapsFeatures::new_empty();
features.add_from_quark(value);
features
}
}
impl<'a, const N: usize> From<[&'a str; N]> for CapsFeatures {
fn from(value: [&'a str; N]) -> Self {
skip_assert_initialized!();
let mut features = CapsFeatures::new_empty();
value.into_iter().for_each(|f| features.add(f));
features
}
}
impl<'a, const N: usize> From<[&'a glib::GStr; N]> for CapsFeatures {
fn from(value: [&'a glib::GStr; N]) -> Self {
skip_assert_initialized!();
let mut features = CapsFeatures::new_empty();
value.into_iter().for_each(|f| features.add(f));
features
}
}
impl<const N: usize> From<[String; N]> for CapsFeatures {
fn from(value: [String; N]) -> Self {
skip_assert_initialized!();
let mut features = CapsFeatures::new_empty();
value.into_iter().for_each(|f| features.add(&f));
features
}
}
impl<const N: usize> From<[glib::GString; N]> for CapsFeatures {
fn from(value: [glib::GString; N]) -> Self {
skip_assert_initialized!();
let mut features = CapsFeatures::new_empty();
value.into_iter().for_each(|f| features.add(&f));
features
}
}
impl<const N: usize> From<[glib::Quark; N]> for CapsFeatures {
fn from(value: [glib::Quark; N]) -> Self {
skip_assert_initialized!();
let mut features = CapsFeatures::new_empty();
value.into_iter().for_each(|f| features.add_from_quark(f));
features
}
}
impl<'a> std::iter::FromIterator<&'a str> for CapsFeatures { impl<'a> std::iter::FromIterator<&'a str> for CapsFeatures {
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self { fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();