2017-09-17 15:50:35 +00:00
|
|
|
// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2017-07-29 13:37:06 +00:00
|
|
|
|
|
|
|
use ffi;
|
|
|
|
use glib::translate::*;
|
|
|
|
use glib_ffi;
|
2017-09-17 00:58:11 +00:00
|
|
|
use glib_ffi::{gconstpointer, gpointer};
|
2017-07-29 13:37:06 +00:00
|
|
|
use gobject_ffi;
|
|
|
|
use std::mem;
|
|
|
|
use std::ptr;
|
2017-09-17 00:58:11 +00:00
|
|
|
use glib;
|
2017-07-29 13:37:06 +00:00
|
|
|
use glib::Value;
|
2017-09-17 15:41:02 +00:00
|
|
|
use glib::StaticType;
|
|
|
|
use glib::value::{FromValueOptional, ToValue};
|
2017-09-17 00:58:11 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::ffi::CString;
|
2017-09-17 15:41:02 +00:00
|
|
|
use std::marker::PhantomData;
|
2017-09-17 11:39:17 +00:00
|
|
|
use std::iter::Iterator as StdIterator;
|
2017-12-16 15:48:38 +00:00
|
|
|
use std::fmt;
|
|
|
|
use std::error::Error;
|
2017-09-17 11:39:17 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
|
|
|
|
pub enum IteratorError {
|
|
|
|
Resync,
|
|
|
|
Error,
|
|
|
|
}
|
2017-07-29 13:37:06 +00:00
|
|
|
|
2017-12-16 15:48:38 +00:00
|
|
|
impl fmt::Display for IteratorError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
IteratorError::Resync => write!(f, "Resync"),
|
|
|
|
IteratorError::Error => write!(f, "Error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for IteratorError {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
match *self {
|
|
|
|
IteratorError::Resync => "Resync",
|
|
|
|
IteratorError::Error => "Error",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
// Implemented manually so that we can use generics for the item
|
|
|
|
pub struct Iterator<T> {
|
|
|
|
iter: *mut ffi::GstIterator,
|
|
|
|
borrowed: bool,
|
|
|
|
phantom: PhantomData<T>,
|
2017-07-29 13:37:06 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
impl<T> Iterator<T>
|
|
|
|
where
|
|
|
|
for<'a> T: FromValueOptional<'a> + 'static,
|
|
|
|
{
|
2017-07-29 13:37:06 +00:00
|
|
|
pub fn resync(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
ffi::gst_iterator_resync(self.to_glib_none_mut().0);
|
|
|
|
}
|
|
|
|
}
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
pub fn filter<F>(self, func: F) -> Self
|
|
|
|
where
|
2017-09-17 15:41:02 +00:00
|
|
|
F: Fn(T) -> bool + Send + Sync + 'static,
|
2017-09-17 00:58:11 +00:00
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
let it = self.to_glib_none().0;
|
|
|
|
mem::forget(self);
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
let func_box: Box<Fn(T) -> bool + Send + Sync + 'static> = Box::new(func);
|
2017-12-06 09:17:59 +00:00
|
|
|
// FIXME: Use Value::from_type once we depend on new enough GLib
|
|
|
|
let mut closure_value = glib::Value::uninitialized();
|
|
|
|
gobject_ffi::g_value_init(
|
|
|
|
closure_value.to_glib_none_mut().0,
|
|
|
|
filter_boxed_get_type::<T>(),
|
|
|
|
);
|
|
|
|
gobject_ffi::g_value_set_boxed(
|
|
|
|
closure_value.to_glib_none_mut().0,
|
2017-09-17 00:58:11 +00:00
|
|
|
Arc::into_raw(Arc::new(func_box)) as gpointer,
|
|
|
|
);
|
|
|
|
|
|
|
|
let it = from_glib_full(ffi::gst_iterator_filter(
|
|
|
|
it as *mut _,
|
2017-09-17 15:41:02 +00:00
|
|
|
Some(filter_trampoline::<T>),
|
2017-12-06 09:17:59 +00:00
|
|
|
closure_value.to_glib_none().0,
|
2017-09-17 00:58:11 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
pub fn find_simple<F>(&mut self, func: F) -> Option<T>
|
2017-09-17 00:58:11 +00:00
|
|
|
where
|
2017-09-17 15:41:02 +00:00
|
|
|
F: FnMut(T) -> bool,
|
2017-09-17 00:58:11 +00:00
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
let mut elem = glib::Value::uninitialized();
|
|
|
|
|
|
|
|
let mut func = func;
|
2017-09-17 15:41:02 +00:00
|
|
|
let func_obj: &mut (FnMut(T) -> bool) = &mut func;
|
|
|
|
let func_ptr = &func_obj as *const &mut (FnMut(T) -> bool) as gpointer;
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
let res = from_glib(ffi::gst_iterator_find_custom(
|
|
|
|
self.to_glib_none_mut().0,
|
2017-09-17 15:41:02 +00:00
|
|
|
Some(find_trampoline::<T>),
|
2017-09-17 00:58:11 +00:00
|
|
|
elem.to_glib_none_mut().0,
|
|
|
|
func_ptr,
|
|
|
|
));
|
|
|
|
if res {
|
2017-09-17 15:41:02 +00:00
|
|
|
Some(elem.get::<T>().unwrap())
|
2017-09-17 00:58:11 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 11:39:17 +00:00
|
|
|
pub fn foreach<F>(&mut self, func: F) -> Result<(), IteratorError>
|
2017-09-17 00:58:11 +00:00
|
|
|
where
|
2017-09-17 15:41:02 +00:00
|
|
|
F: FnMut(T),
|
2017-09-17 00:58:11 +00:00
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
let mut func = func;
|
2017-09-17 15:41:02 +00:00
|
|
|
let func_obj: &mut (FnMut(T)) = &mut func;
|
|
|
|
let func_ptr = &func_obj as *const &mut (FnMut(T)) as gpointer;
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2017-09-17 11:39:17 +00:00
|
|
|
let res = ffi::gst_iterator_foreach(
|
2017-09-17 00:58:11 +00:00
|
|
|
self.to_glib_none_mut().0,
|
2017-09-17 15:41:02 +00:00
|
|
|
Some(foreach_trampoline::<T>),
|
2017-09-17 00:58:11 +00:00
|
|
|
func_ptr,
|
2017-09-17 11:39:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
match res {
|
|
|
|
ffi::GST_ITERATOR_OK | ffi::GST_ITERATOR_DONE => Ok(()),
|
|
|
|
ffi::GST_ITERATOR_RESYNC => Err(IteratorError::Resync),
|
2017-11-05 17:01:15 +00:00
|
|
|
ffi::GST_ITERATOR_ERROR | _ => Err(IteratorError::Error),
|
2017-09-17 11:39:17 +00:00
|
|
|
}
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
pub fn fold_with_early_exit<F, U>(&mut self, init: U, func: F) -> Result<U, IteratorError>
|
2017-09-17 00:58:11 +00:00
|
|
|
where
|
2017-09-17 15:41:02 +00:00
|
|
|
F: FnMut(U, T) -> Result<U, U>,
|
2017-09-17 00:58:11 +00:00
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
let mut func = func;
|
2017-09-17 15:41:02 +00:00
|
|
|
let func_obj: &mut (FnMut(U, T) -> Result<U, U>) = &mut func;
|
|
|
|
let func_ptr = &func_obj as *const &mut (FnMut(U, T) -> Result<U, U>) as gpointer;
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
let mut accum = Some(init);
|
2017-12-06 09:17:59 +00:00
|
|
|
// FIXME: Use Value::from_type once we depend on new enough GLib
|
|
|
|
let mut ret = glib::Value::uninitialized();
|
2017-09-17 00:58:11 +00:00
|
|
|
gobject_ffi::g_value_init(ret.to_glib_none_mut().0, gobject_ffi::G_TYPE_POINTER);
|
|
|
|
gobject_ffi::g_value_set_pointer(
|
|
|
|
ret.to_glib_none_mut().0,
|
|
|
|
&mut accum as *mut _ as gpointer,
|
|
|
|
);
|
|
|
|
|
2017-09-17 11:39:17 +00:00
|
|
|
let res = ffi::gst_iterator_fold(
|
2017-09-17 00:58:11 +00:00
|
|
|
self.to_glib_none_mut().0,
|
2017-09-17 15:41:02 +00:00
|
|
|
Some(fold_trampoline::<T, U>),
|
2017-09-17 00:58:11 +00:00
|
|
|
ret.to_glib_none_mut().0,
|
|
|
|
func_ptr,
|
2017-09-17 11:39:17 +00:00
|
|
|
);
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
match res {
|
2017-09-17 11:39:17 +00:00
|
|
|
ffi::GST_ITERATOR_OK | ffi::GST_ITERATOR_DONE => Ok(accum.unwrap()),
|
|
|
|
ffi::GST_ITERATOR_RESYNC => Err(IteratorError::Resync),
|
2017-11-05 17:01:15 +00:00
|
|
|
ffi::GST_ITERATOR_ERROR | _ => Err(IteratorError::Error),
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
impl<T> Iterator<T>
|
|
|
|
where
|
|
|
|
for<'a> T: FromValueOptional<'a> + StaticType + ToValue + Send + 'static,
|
|
|
|
{
|
|
|
|
pub fn new<I: IteratorImpl<T>>(imp: I) -> Self {
|
2017-09-17 00:58:11 +00:00
|
|
|
static DUMMY_COOKIE: u32 = 0;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let it = ffi::gst_iterator_new(
|
2017-09-17 15:41:02 +00:00
|
|
|
mem::size_of::<RsIterator<T, I>>() as u32,
|
|
|
|
T::static_type().to_glib(),
|
2017-09-17 00:58:11 +00:00
|
|
|
ptr::null_mut(),
|
|
|
|
&DUMMY_COOKIE as *const _ as *mut _,
|
2017-09-17 15:41:02 +00:00
|
|
|
Some(rs_iterator_copy::<T, I>),
|
|
|
|
Some(rs_iterator_next::<T, I>),
|
2017-09-17 00:58:11 +00:00
|
|
|
None,
|
2017-09-17 15:41:02 +00:00
|
|
|
Some(rs_iterator_resync::<T, I>),
|
|
|
|
Some(rs_iterator_free::<T, I>),
|
2017-09-17 00:58:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
{
|
2017-09-17 15:41:02 +00:00
|
|
|
let it = it as *mut RsIterator<T, I>;
|
2017-09-17 00:58:11 +00:00
|
|
|
(*it).imp = Some(imp);
|
|
|
|
}
|
|
|
|
|
|
|
|
from_glib_full(it)
|
|
|
|
}
|
|
|
|
}
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
impl<T> Iterator<T>
|
|
|
|
where
|
|
|
|
for<'a> T: FromValueOptional<'a> + StaticType + ToValue + Clone + Send + 'static,
|
|
|
|
{
|
|
|
|
pub fn from_vec(items: Vec<T>) -> Self {
|
2017-09-17 00:58:11 +00:00
|
|
|
Self::new(VecIteratorImpl::new(items))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
impl<T> StdIterator for Iterator<T>
|
|
|
|
where
|
|
|
|
for<'a> T: FromValueOptional<'a> + 'static,
|
|
|
|
{
|
|
|
|
type Item = Result<T, IteratorError>;
|
2017-09-17 11:39:17 +00:00
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
fn next(&mut self) -> Option<Result<T, IteratorError>> {
|
2017-09-17 11:39:17 +00:00
|
|
|
unsafe {
|
|
|
|
let mut value = Value::uninitialized();
|
|
|
|
let res = ffi::gst_iterator_next(self.to_glib_none_mut().0, value.to_glib_none_mut().0);
|
|
|
|
match res {
|
2017-09-17 15:41:02 +00:00
|
|
|
ffi::GST_ITERATOR_OK => match value.get::<T>() {
|
|
|
|
Some(value) => Some(Ok(value)),
|
|
|
|
None => Some(Err(IteratorError::Error)),
|
|
|
|
},
|
2017-09-17 11:39:17 +00:00
|
|
|
ffi::GST_ITERATOR_DONE => None,
|
|
|
|
ffi::GST_ITERATOR_RESYNC => Some(Err(IteratorError::Resync)),
|
2017-11-05 17:01:15 +00:00
|
|
|
ffi::GST_ITERATOR_ERROR | _ => Some(Err(IteratorError::Error)),
|
2017-09-17 11:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 00:58:11 +00:00
|
|
|
#[repr(C)]
|
2017-09-17 15:41:02 +00:00
|
|
|
struct RsIterator<T, I: IteratorImpl<T>>
|
|
|
|
where
|
|
|
|
for<'a> T: FromValueOptional<'a> + StaticType + ToValue + Send + 'static,
|
|
|
|
{
|
2017-09-17 00:58:11 +00:00
|
|
|
iter: ffi::GstIterator,
|
|
|
|
imp: Option<I>,
|
2017-09-17 15:41:02 +00:00
|
|
|
phantom: PhantomData<T>,
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
pub trait IteratorImpl<T>: Clone + Send + 'static
|
|
|
|
where
|
|
|
|
for<'a> T: FromValueOptional<'a> + StaticType + ToValue + Send + 'static,
|
|
|
|
{
|
|
|
|
fn next(&mut self) -> Option<Result<T, IteratorError>>;
|
2017-09-17 00:58:11 +00:00
|
|
|
fn resync(&mut self);
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
unsafe extern "C" fn rs_iterator_copy<T, I: IteratorImpl<T>>(
|
2017-09-17 00:58:11 +00:00
|
|
|
it: *const ffi::GstIterator,
|
|
|
|
copy: *mut ffi::GstIterator,
|
2017-09-17 15:41:02 +00:00
|
|
|
) where
|
|
|
|
for<'a> T: FromValueOptional<'a> + StaticType + ToValue + Send + 'static,
|
|
|
|
{
|
2017-09-17 00:58:11 +00:00
|
|
|
callback_guard!();
|
2017-09-17 15:41:02 +00:00
|
|
|
let it = it as *const RsIterator<T, I>;
|
|
|
|
let copy = copy as *mut RsIterator<T, I>;
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
ptr::write(&mut (*copy).imp, (*it).imp.clone());
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
unsafe extern "C" fn rs_iterator_free<T, I: IteratorImpl<T>>(it: *mut ffi::GstIterator)
|
|
|
|
where
|
|
|
|
for<'a> T: FromValueOptional<'a> + StaticType + ToValue + Send + 'static,
|
|
|
|
{
|
2017-09-17 00:58:11 +00:00
|
|
|
callback_guard!();
|
2017-09-17 15:41:02 +00:00
|
|
|
let it = it as *mut RsIterator<T, I>;
|
2017-09-17 00:58:11 +00:00
|
|
|
let _ = (*it).imp.take();
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
unsafe extern "C" fn rs_iterator_next<T, I: IteratorImpl<T>>(
|
2017-09-17 00:58:11 +00:00
|
|
|
it: *mut ffi::GstIterator,
|
|
|
|
result: *mut gobject_ffi::GValue,
|
2017-09-17 15:41:02 +00:00
|
|
|
) -> ffi::GstIteratorResult
|
|
|
|
where
|
|
|
|
for<'a> T: FromValueOptional<'a> + StaticType + ToValue + Send + 'static,
|
|
|
|
{
|
2017-09-17 00:58:11 +00:00
|
|
|
callback_guard!();
|
2017-09-17 15:41:02 +00:00
|
|
|
let it = it as *mut RsIterator<T, I>;
|
2017-09-17 00:58:11 +00:00
|
|
|
match (*it).imp.as_mut().map(|imp| imp.next()).unwrap() {
|
2017-09-17 11:39:17 +00:00
|
|
|
Some(Ok(value)) => {
|
2017-09-17 15:41:02 +00:00
|
|
|
let value = value.to_value();
|
2017-09-17 00:58:11 +00:00
|
|
|
ptr::write(result, ptr::read(value.to_glib_none().0));
|
|
|
|
mem::forget(value);
|
|
|
|
ffi::GST_ITERATOR_OK
|
|
|
|
}
|
2017-09-17 11:39:17 +00:00
|
|
|
None => ffi::GST_ITERATOR_DONE,
|
|
|
|
Some(Err(res)) => match res {
|
|
|
|
IteratorError::Resync => ffi::GST_ITERATOR_RESYNC,
|
|
|
|
IteratorError::Error => ffi::GST_ITERATOR_ERROR,
|
|
|
|
},
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
unsafe extern "C" fn rs_iterator_resync<T, I: IteratorImpl<T>>(it: *mut ffi::GstIterator)
|
|
|
|
where
|
|
|
|
for<'a> T: FromValueOptional<'a> + StaticType + ToValue + Send + 'static,
|
|
|
|
{
|
2017-09-17 00:58:11 +00:00
|
|
|
callback_guard!();
|
2017-09-17 15:41:02 +00:00
|
|
|
let it = it as *mut RsIterator<T, I>;
|
2017-09-17 00:58:11 +00:00
|
|
|
(*it).imp.as_mut().map(|imp| imp.resync()).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2017-09-17 15:41:02 +00:00
|
|
|
struct VecIteratorImpl<T> {
|
2017-09-17 00:58:11 +00:00
|
|
|
pos: usize,
|
|
|
|
items: Vec<T>,
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
impl<T> VecIteratorImpl<T>
|
|
|
|
where
|
|
|
|
for<'a> T: StaticType + ToValue + FromValueOptional<'a> + Clone + Send + 'static,
|
|
|
|
{
|
2017-09-17 00:58:11 +00:00
|
|
|
fn new(items: Vec<T>) -> Self {
|
|
|
|
Self {
|
|
|
|
pos: 0,
|
|
|
|
items: items,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
impl<T> IteratorImpl<T> for VecIteratorImpl<T>
|
|
|
|
where
|
|
|
|
for<'a> T: StaticType + ToValue + FromValueOptional<'a> + Clone + Send + 'static,
|
|
|
|
{
|
|
|
|
fn next(&mut self) -> Option<Result<T, IteratorError>> {
|
2017-09-17 00:58:11 +00:00
|
|
|
if self.pos < self.items.len() {
|
2017-09-17 15:41:02 +00:00
|
|
|
let res = Ok(self.items[self.pos].clone());
|
2017-09-17 00:58:11 +00:00
|
|
|
self.pos += 1;
|
2017-09-17 11:39:17 +00:00
|
|
|
return Some(res);
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 11:39:17 +00:00
|
|
|
None
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resync(&mut self) {
|
|
|
|
self.pos = 0;
|
|
|
|
}
|
2017-07-29 13:37:06 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
unsafe impl<T> Send for Iterator<T> {}
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
unsafe extern "C" fn filter_trampoline<T>(value: gconstpointer, func: gconstpointer) -> i32
|
|
|
|
where
|
|
|
|
for<'a> T: FromValueOptional<'a> + 'static,
|
|
|
|
{
|
2017-09-17 00:58:11 +00:00
|
|
|
callback_guard!();
|
|
|
|
let value = value as *const gobject_ffi::GValue;
|
|
|
|
|
|
|
|
let func = func as *const gobject_ffi::GValue;
|
|
|
|
let func = gobject_ffi::g_value_get_boxed(func);
|
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))]
|
2017-09-17 15:41:02 +00:00
|
|
|
let func: &&(Fn(T) -> bool + Send + Sync + 'static) = mem::transmute(func);
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
let value = &*(value as *const glib::Value);
|
2017-09-17 15:41:02 +00:00
|
|
|
let value = value.get::<T>().unwrap();
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
if func(value) {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
-1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
unsafe extern "C" fn filter_boxed_ref<T: 'static>(boxed: gpointer) -> gpointer {
|
2017-09-17 00:58:11 +00:00
|
|
|
callback_guard!();
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
let boxed = Arc::from_raw(boxed as *const (Box<Fn(T) -> bool + Send + Sync + 'static>));
|
2017-11-26 21:59:03 +00:00
|
|
|
let copy = Arc::clone(&boxed);
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
// Forget it and keep it alive, we will still need it later
|
|
|
|
let _ = Arc::into_raw(boxed);
|
|
|
|
|
|
|
|
Arc::into_raw(copy) as gpointer
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
unsafe extern "C" fn filter_boxed_unref<T: 'static>(boxed: gpointer) {
|
2017-09-17 00:58:11 +00:00
|
|
|
callback_guard!();
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
let _ = Arc::from_raw(boxed as *const (Box<Fn(T) -> bool + Send + Sync + 'static>));
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
unsafe extern "C" fn filter_boxed_get_type<T: StaticType + 'static>() -> glib_ffi::GType {
|
2017-09-17 00:58:11 +00:00
|
|
|
use std::sync::{Once, ONCE_INIT};
|
|
|
|
|
|
|
|
callback_guard!();
|
|
|
|
|
|
|
|
static mut TYPE: glib_ffi::GType = gobject_ffi::G_TYPE_INVALID;
|
|
|
|
static ONCE: Once = ONCE_INIT;
|
|
|
|
|
|
|
|
ONCE.call_once(|| {
|
|
|
|
let type_name = {
|
|
|
|
let mut idx = 0;
|
|
|
|
|
|
|
|
loop {
|
2017-09-17 15:41:02 +00:00
|
|
|
let type_name = CString::new(format!(
|
|
|
|
"GstRsIteratorFilterBoxed-{}-{}",
|
|
|
|
T::static_type().name(),
|
|
|
|
idx
|
|
|
|
)).unwrap();
|
2017-09-17 00:58:11 +00:00
|
|
|
if gobject_ffi::g_type_from_name(type_name.as_ptr()) == gobject_ffi::G_TYPE_INVALID
|
|
|
|
{
|
|
|
|
break type_name;
|
|
|
|
}
|
|
|
|
idx += 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TYPE = gobject_ffi::g_boxed_type_register_static(
|
|
|
|
type_name.as_ptr(),
|
2017-09-17 15:41:02 +00:00
|
|
|
Some(filter_boxed_ref::<T>),
|
|
|
|
Some(filter_boxed_unref::<T>),
|
2017-09-17 00:58:11 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
TYPE
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
unsafe extern "C" fn find_trampoline<T>(value: gconstpointer, func: gconstpointer) -> i32
|
|
|
|
where
|
|
|
|
for<'a> T: FromValueOptional<'a> + 'static,
|
|
|
|
{
|
2017-09-17 00:58:11 +00:00
|
|
|
callback_guard!();
|
|
|
|
let value = value as *const gobject_ffi::GValue;
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
let func = func as *const &mut (FnMut(T) -> bool);
|
2017-09-17 00:58:11 +00:00
|
|
|
let value = &*(value as *const glib::Value);
|
2017-09-17 15:41:02 +00:00
|
|
|
let value = value.get::<T>().unwrap();
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
if (*func)(value) {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
-1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
unsafe extern "C" fn foreach_trampoline<T>(value: *const gobject_ffi::GValue, func: gpointer)
|
|
|
|
where
|
|
|
|
for<'a> T: FromValueOptional<'a> + 'static,
|
|
|
|
{
|
2017-09-17 00:58:11 +00:00
|
|
|
callback_guard!();
|
2017-09-17 15:41:02 +00:00
|
|
|
let func = func as *const &mut (FnMut(T));
|
2017-09-17 00:58:11 +00:00
|
|
|
let value = &*(value as *const glib::Value);
|
2017-09-17 15:41:02 +00:00
|
|
|
let value = value.get::<T>().unwrap();
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
(*func)(value);
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
unsafe extern "C" fn fold_trampoline<T, U>(
|
2017-09-17 00:58:11 +00:00
|
|
|
value: *const gobject_ffi::GValue,
|
|
|
|
ret: *mut gobject_ffi::GValue,
|
|
|
|
func: gpointer,
|
2017-09-17 15:41:02 +00:00
|
|
|
) -> glib_ffi::gboolean
|
|
|
|
where
|
|
|
|
for<'a> T: FromValueOptional<'a> + 'static,
|
|
|
|
{
|
2017-09-17 00:58:11 +00:00
|
|
|
callback_guard!();
|
2017-09-17 15:41:02 +00:00
|
|
|
let func = func as *const &mut (FnMut(U, T) -> Result<U, U>);
|
2017-09-17 00:58:11 +00:00
|
|
|
let value = &*(value as *const glib::Value);
|
2017-09-17 15:41:02 +00:00
|
|
|
let value = value.get::<T>().unwrap();
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
let accum = &mut *(gobject_ffi::g_value_get_pointer(ret) as *mut Option<U>);
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
match (*func)(accum.take().unwrap(), value) {
|
|
|
|
Ok(next_accum) => {
|
|
|
|
*accum = Some(next_accum);
|
|
|
|
glib_ffi::GTRUE
|
|
|
|
}
|
|
|
|
Err(next_accum) => {
|
|
|
|
*accum = Some(next_accum);
|
|
|
|
glib_ffi::GFALSE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
impl<T: StaticType + 'static> Clone for Iterator<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
unsafe { from_glib_full(ffi::gst_iterator_copy(self.to_glib_none().0)) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Drop for Iterator<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if !self.borrowed {
|
|
|
|
unsafe {
|
|
|
|
ffi::gst_iterator_free(self.iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> glib::types::StaticType for Iterator<T> {
|
|
|
|
fn static_type() -> glib::types::Type {
|
|
|
|
unsafe { glib::translate::from_glib(ffi::gst_iterator_get_type()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl<'a, T: StaticType> glib::value::FromValueOptional<'a> for Iterator<T> {
|
|
|
|
unsafe fn from_value_optional(value: &glib::Value) -> Option<Self> {
|
|
|
|
Option::<Iterator<T>>::from_glib_none(
|
|
|
|
gobject_ffi::g_value_get_boxed(value.to_glib_none().0) as *mut ffi::GstIterator,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl<T: 'static> glib::value::SetValue for Iterator<T> {
|
|
|
|
unsafe fn set_value(value: &mut glib::Value, this: &Self) {
|
|
|
|
gobject_ffi::g_value_set_boxed(
|
|
|
|
value.to_glib_none_mut().0,
|
2017-10-17 09:06:51 +00:00
|
|
|
glib::translate::ToGlibPtr::<*const ffi::GstIterator>::to_glib_none(this).0
|
|
|
|
as glib_ffi::gpointer,
|
2017-09-17 15:41:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl<T: 'static> glib::value::SetValueOptional for Iterator<T> {
|
|
|
|
unsafe fn set_value_optional(value: &mut glib::Value, this: Option<&Self>) {
|
|
|
|
gobject_ffi::g_value_set_boxed(
|
|
|
|
value.to_glib_none_mut().0,
|
2017-10-17 09:06:51 +00:00
|
|
|
glib::translate::ToGlibPtr::<*const ffi::GstIterator>::to_glib_none(&this).0
|
|
|
|
as glib_ffi::gpointer,
|
2017-09-17 15:41:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl<T> glib::translate::GlibPtrDefault for Iterator<T> {
|
|
|
|
type GlibType = *mut ffi::GstIterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl<'a, T: 'static> glib::translate::ToGlibPtr<'a, *const ffi::GstIterator> for Iterator<T> {
|
|
|
|
type Storage = &'a Iterator<T>;
|
|
|
|
|
|
|
|
fn to_glib_none(&'a self) -> glib::translate::Stash<'a, *const ffi::GstIterator, Self> {
|
|
|
|
glib::translate::Stash(self.iter, self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_glib_full(&self) -> *const ffi::GstIterator {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl<'a, T: 'static> glib::translate::ToGlibPtrMut<'a, *mut ffi::GstIterator> for Iterator<T> {
|
|
|
|
type Storage = &'a mut Iterator<T>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_glib_none_mut(
|
|
|
|
&'a mut self,
|
|
|
|
) -> glib::translate::StashMut<'a, *mut ffi::GstIterator, Self> {
|
|
|
|
glib::translate::StashMut(self.iter, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl<T: StaticType> glib::translate::FromGlibPtrNone<*const ffi::GstIterator> for Iterator<T> {
|
|
|
|
#[inline]
|
|
|
|
unsafe fn from_glib_none(ptr: *const ffi::GstIterator) -> Self {
|
|
|
|
assert_ne!(
|
|
|
|
gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().to_glib()),
|
|
|
|
glib_ffi::GFALSE
|
|
|
|
);
|
|
|
|
from_glib_full(ffi::gst_iterator_copy(ptr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl<T: StaticType> glib::translate::FromGlibPtrNone<*mut ffi::GstIterator> for Iterator<T> {
|
|
|
|
#[inline]
|
|
|
|
unsafe fn from_glib_none(ptr: *mut ffi::GstIterator) -> Self {
|
|
|
|
assert_ne!(
|
|
|
|
gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().to_glib()),
|
|
|
|
glib_ffi::GFALSE
|
|
|
|
);
|
|
|
|
from_glib_full(ffi::gst_iterator_copy(ptr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl<T: StaticType> glib::translate::FromGlibPtrBorrow<*mut ffi::GstIterator> for Iterator<T> {
|
|
|
|
#[inline]
|
|
|
|
unsafe fn from_glib_borrow(ptr: *mut ffi::GstIterator) -> Self {
|
|
|
|
assert_ne!(
|
|
|
|
gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().to_glib()),
|
|
|
|
glib_ffi::GFALSE
|
|
|
|
);
|
|
|
|
Self {
|
|
|
|
iter: ptr,
|
|
|
|
borrowed: true,
|
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl<T: StaticType> glib::translate::FromGlibPtrFull<*mut ffi::GstIterator> for Iterator<T> {
|
|
|
|
#[inline]
|
|
|
|
unsafe fn from_glib_full(ptr: *mut ffi::GstIterator) -> Self {
|
|
|
|
assert_ne!(
|
|
|
|
gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().to_glib()),
|
|
|
|
glib_ffi::GFALSE
|
|
|
|
);
|
|
|
|
Self {
|
|
|
|
iter: ptr,
|
|
|
|
borrowed: false,
|
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 00:58:11 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vec() {
|
|
|
|
::init().unwrap();
|
|
|
|
|
|
|
|
let vec = vec![1i32, 2, 3];
|
|
|
|
let mut it = Iterator::from_vec(vec);
|
|
|
|
let val = it.next().unwrap();
|
2017-09-17 15:41:02 +00:00
|
|
|
assert_eq!(val, Ok(1));
|
2017-09-17 00:58:11 +00:00
|
|
|
let val = it.next().unwrap();
|
2017-09-17 15:41:02 +00:00
|
|
|
assert_eq!(val, Ok(2));
|
2017-09-17 00:58:11 +00:00
|
|
|
let val = it.next().unwrap();
|
2017-09-17 15:41:02 +00:00
|
|
|
assert_eq!(val, Ok(3));
|
2017-09-17 11:39:17 +00:00
|
|
|
assert!(it.next().is_none());
|
|
|
|
|
|
|
|
let vec = vec![1i32, 2, 3];
|
|
|
|
let it = Iterator::from_vec(vec);
|
2017-09-17 15:41:02 +00:00
|
|
|
let vals: Vec<_> = it.map(|v| v.unwrap()).collect();
|
2017-09-17 11:39:17 +00:00
|
|
|
assert_eq!(vals, [1, 2, 3]);
|
|
|
|
|
2017-09-17 11:59:01 +00:00
|
|
|
let vec = vec![1i32, 2, 3];
|
|
|
|
let mut it = Iterator::from_vec(vec);
|
|
|
|
let mut vals = Vec::new();
|
|
|
|
while let Some(res) = it.next() {
|
|
|
|
match res {
|
2017-09-17 15:41:02 +00:00
|
|
|
Ok(v) => vals.push(v),
|
2017-09-17 11:59:01 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(vals, [1, 2, 3]);
|
|
|
|
|
2017-09-17 11:39:17 +00:00
|
|
|
let vec = vec![1i32, 2, 3];
|
|
|
|
let it = Iterator::from_vec(vec);
|
|
|
|
let mut vals = Vec::new();
|
|
|
|
for v in it {
|
2017-09-17 15:41:02 +00:00
|
|
|
vals.push(v.unwrap());
|
2017-09-17 11:39:17 +00:00
|
|
|
}
|
|
|
|
assert_eq!(vals, [1, 2, 3]);
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_filter() {
|
|
|
|
::init().unwrap();
|
|
|
|
|
|
|
|
let vec = vec![1i32, 2, 3];
|
2017-09-17 15:41:02 +00:00
|
|
|
let it = Iterator::from_vec(vec).filter(|val| val % 2 == 1);
|
|
|
|
let vals: Vec<_> = it.map(|v| v.unwrap()).collect();
|
2017-09-17 11:39:17 +00:00
|
|
|
assert_eq!(vals, [1, 3]);
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find() {
|
|
|
|
::init().unwrap();
|
|
|
|
|
2017-09-17 11:39:17 +00:00
|
|
|
// Our find
|
2017-09-17 00:58:11 +00:00
|
|
|
let vec = vec![1i32, 2, 3];
|
2017-09-17 15:41:02 +00:00
|
|
|
let val = Iterator::from_vec(vec).find_simple(|val| val == 2);
|
|
|
|
assert_eq!(val.unwrap(), 2);
|
2017-09-17 11:39:17 +00:00
|
|
|
|
|
|
|
// Find from std::iter::Iterator
|
|
|
|
let vec = vec![1i32, 2, 3];
|
2017-09-17 15:41:02 +00:00
|
|
|
let val = Iterator::from_vec(vec).find(|val| val.unwrap() == 2);
|
|
|
|
assert_eq!(val.unwrap(), Ok(2));
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_foreach() {
|
|
|
|
::init().unwrap();
|
|
|
|
|
|
|
|
let vec = vec![1i32, 2, 3];
|
|
|
|
let mut sum = 0;
|
2017-09-17 15:41:02 +00:00
|
|
|
let res = Iterator::from_vec(vec).foreach(|val| sum += val);
|
2017-09-17 11:39:17 +00:00
|
|
|
assert_eq!(res, Ok(()));
|
2017-09-17 00:58:11 +00:00
|
|
|
assert_eq!(sum, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fold() {
|
|
|
|
::init().unwrap();
|
|
|
|
|
2017-09-17 11:39:17 +00:00
|
|
|
// Our fold
|
2017-09-17 00:58:11 +00:00
|
|
|
let vec = vec![1i32, 2, 3];
|
2017-09-17 11:39:17 +00:00
|
|
|
let res = Iterator::from_vec(vec).fold_with_early_exit(0, |mut sum, val| {
|
2017-09-17 15:41:02 +00:00
|
|
|
sum += val;
|
2017-09-17 00:58:11 +00:00
|
|
|
Ok(sum)
|
|
|
|
});
|
|
|
|
assert_eq!(res.unwrap(), 6);
|
2017-09-17 11:39:17 +00:00
|
|
|
|
|
|
|
// Fold from std::iter::Iterator
|
|
|
|
let vec = vec![1i32, 2, 3];
|
|
|
|
let res = Iterator::from_vec(vec).fold(0, |mut sum, val| {
|
2017-09-17 15:41:02 +00:00
|
|
|
sum += val.unwrap();
|
2017-09-17 11:39:17 +00:00
|
|
|
sum
|
|
|
|
});
|
|
|
|
assert_eq!(res, 6);
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
}
|