mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs.git
synced 2025-01-09 08:45:27 +00:00
Add a Builder for caps and structures
This commit is contained in:
parent
f823ef9cf7
commit
2fc3470fa1
2 changed files with 87 additions and 0 deletions
|
@ -28,6 +28,11 @@ unsafe impl MiniObject for CapsRef {
|
|||
}
|
||||
|
||||
impl GstRc<CapsRef> {
|
||||
pub fn builder(name: &str) -> Builder {
|
||||
assert_initialized_main_thread!();
|
||||
Builder::new(name)
|
||||
}
|
||||
|
||||
pub fn new_empty() -> Self {
|
||||
assert_initialized_main_thread!();
|
||||
unsafe { from_glib_full(ffi::gst_caps_new_empty()) }
|
||||
|
@ -366,6 +371,29 @@ impl ToOwned for CapsRef {
|
|||
unsafe impl Sync for CapsRef {}
|
||||
unsafe impl Send for CapsRef {}
|
||||
|
||||
pub struct Builder {
|
||||
s: ::Structure,
|
||||
}
|
||||
|
||||
impl Builder {
|
||||
fn new(name: &str) -> Self {
|
||||
Builder {
|
||||
s: ::Structure::new_empty(name),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn field<V: ToValue>(mut self, name: &str, value: V) -> Self {
|
||||
self.s.set(name, value);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> Caps {
|
||||
let mut caps = Caps::new_empty();
|
||||
caps.get_mut().unwrap().append_structure(self.s);
|
||||
caps
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -406,4 +434,21 @@ mod tests {
|
|||
).as_ref()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_builder() {
|
||||
::init().unwrap();
|
||||
|
||||
let caps = Caps::builder("foo/bar")
|
||||
.field("int", 12)
|
||||
.field("bool", true)
|
||||
.field("string", "bla")
|
||||
.field("fraction", Fraction::new(1, 2))
|
||||
.field("array", Array::new(&[&1, &2]))
|
||||
.build();
|
||||
assert_eq!(
|
||||
caps.to_string(),
|
||||
"foo/bar, int=(int)12, bool=(boolean)true, string=(string)bla, fraction=(fraction)1/2, array=(int)< 1, 2 >"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,11 @@ use gobject_ffi;
|
|||
pub struct Structure(*mut StructureRef, PhantomData<StructureRef>);
|
||||
|
||||
impl Structure {
|
||||
pub fn builder(name: &str) -> Builder {
|
||||
assert_initialized_main_thread!();
|
||||
Builder::new(name)
|
||||
}
|
||||
|
||||
pub fn new_empty(name: &str) -> Structure {
|
||||
assert_initialized_main_thread!();
|
||||
Structure(
|
||||
|
@ -591,6 +596,27 @@ impl<'a> DoubleEndedIterator for Iter<'a> {
|
|||
|
||||
impl<'a> ExactSizeIterator for Iter<'a> {}
|
||||
|
||||
pub struct Builder {
|
||||
s: Structure,
|
||||
}
|
||||
|
||||
impl Builder {
|
||||
fn new(name: &str) -> Self {
|
||||
Builder {
|
||||
s: Structure::new_empty(name),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn field<V: ToValue>(mut self, name: &str, value: V) -> Self {
|
||||
self.s.set(name, value);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> Structure {
|
||||
self.s
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -624,4 +650,20 @@ mod tests {
|
|||
let s2 = Structure::new("test", &[("f1", &"abc"), ("f2", &"bcd"), ("f3", &123i32)]);
|
||||
assert_eq!(s, s2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_builder() {
|
||||
::init().unwrap();
|
||||
|
||||
let s = Structure::builder("test")
|
||||
.field("f1", "abc")
|
||||
.field("f2", String::from("bcd"))
|
||||
.field("f3", 123i32)
|
||||
.build();
|
||||
|
||||
assert_eq!(s.get_name(), "test");
|
||||
assert_eq!(s.get::<&str>("f1").unwrap(), "abc");
|
||||
assert_eq!(s.get::<&str>("f2").unwrap(), "bcd");
|
||||
assert_eq!(s.get::<i32>("f3").unwrap(), 123i32);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue