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-29 13:37:06 +00:00
|
|
|
|
2023-01-07 14:46:38 +00:00
|
|
|
use std::{any::Any, fmt, iter, marker::PhantomData, mem, ptr, sync::Arc};
|
2023-01-03 18:58:25 +00:00
|
|
|
|
|
|
|
use glib::{
|
|
|
|
ffi::{gconstpointer, gpointer},
|
|
|
|
translate::*,
|
|
|
|
value::{FromValue, ToValue},
|
|
|
|
StaticType, Value,
|
|
|
|
};
|
2020-01-10 11:33:03 +00:00
|
|
|
use thiserror::Error;
|
2017-09-17 11:39:17 +00:00
|
|
|
|
2020-01-10 11:33:03 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Error)]
|
2017-09-17 11:39:17 +00:00
|
|
|
pub enum IteratorError {
|
2020-01-10 11:33:03 +00:00
|
|
|
#[error("Resync")]
|
2017-09-17 11:39:17 +00:00
|
|
|
Resync,
|
2020-01-10 11:33:03 +00:00
|
|
|
#[error("Error")]
|
2017-09-17 11:39:17 +00:00
|
|
|
Error,
|
|
|
|
}
|
2017-07-29 13:37:06 +00:00
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
// Implemented manually so that we can use generics for the item
|
|
|
|
pub struct Iterator<T> {
|
2020-11-21 13:46:48 +00:00
|
|
|
iter: ptr::NonNull<ffi::GstIterator>,
|
2017-09-17 15:41:02 +00:00
|
|
|
phantom: PhantomData<T>,
|
2017-07-29 13:37:06 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
impl<T> Iterator<T>
|
|
|
|
where
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
2019-02-28 08:54:32 +00:00
|
|
|
#[allow(clippy::should_implement_trait)]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_iterator_next")]
|
2018-08-13 17:35:28 +00:00
|
|
|
pub fn next(&mut self) -> Result<Option<T>, IteratorError> {
|
|
|
|
unsafe {
|
|
|
|
let mut value = Value::uninitialized();
|
2020-11-21 13:46:48 +00:00
|
|
|
let res = ffi::gst_iterator_next(self.to_glib_none_mut().0, value.to_glib_none_mut().0);
|
2020-03-19 11:22:20 +00:00
|
|
|
|
|
|
|
#[allow(clippy::wildcard_in_or_patterns)]
|
2018-08-13 17:35:28 +00:00
|
|
|
match res {
|
2021-04-20 07:19:02 +00:00
|
|
|
ffi::GST_ITERATOR_OK => match value.get::<T>() {
|
|
|
|
Ok(value) => Ok(Some(value)),
|
|
|
|
Err(_) => Err(IteratorError::Error),
|
2018-08-13 17:35:28 +00:00
|
|
|
},
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::GST_ITERATOR_DONE => Ok(None),
|
|
|
|
ffi::GST_ITERATOR_RESYNC => Err(IteratorError::Resync),
|
|
|
|
ffi::GST_ITERATOR_ERROR | _ => Err(IteratorError::Error),
|
2018-08-13 17:35:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_iterator_resync")]
|
2017-07-29 13:37:06 +00:00
|
|
|
pub fn resync(&mut self) {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_iterator_resync(self.to_glib_none_mut().0);
|
2017-07-29 13:37:06 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_iterator_filter")]
|
2022-01-04 18:43:07 +00:00
|
|
|
#[must_use]
|
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,
|
2021-04-20 07:19:02 +00:00
|
|
|
T: StaticType,
|
2017-09-17 00:58:11 +00:00
|
|
|
{
|
|
|
|
unsafe {
|
2023-01-07 14:46:38 +00:00
|
|
|
let func_box: Box<dyn Any + Send + Sync + 'static> = Box::new(func);
|
|
|
|
let mut closure_value = glib::Value::from_type_unchecked(filter_boxed_get_type());
|
2020-11-21 13:46:48 +00:00
|
|
|
glib::gobject_ffi::g_value_take_boxed(
|
2017-12-06 09:17:59 +00:00
|
|
|
closure_value.to_glib_none_mut().0,
|
2017-09-17 00:58:11 +00:00
|
|
|
Arc::into_raw(Arc::new(func_box)) as gpointer,
|
|
|
|
);
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
from_glib_full(ffi::gst_iterator_filter(
|
2022-05-06 19:41:15 +00:00
|
|
|
self.into_glib_ptr(),
|
2023-01-07 14:46:38 +00:00
|
|
|
Some(filter_trampoline::<T, F>),
|
2017-12-06 09:17:59 +00:00
|
|
|
closure_value.to_glib_none().0,
|
2017-12-20 19:46:58 +00:00
|
|
|
))
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_iterator_find_custom")]
|
2018-08-13 17:35:28 +00:00
|
|
|
pub fn find<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;
|
2019-01-30 13:02:03 +00:00
|
|
|
let func_ptr = &mut func as *mut F as gpointer;
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
let res = from_glib(ffi::gst_iterator_find_custom(
|
2017-09-17 00:58:11 +00:00
|
|
|
self.to_glib_none_mut().0,
|
2019-01-30 13:02:03 +00:00
|
|
|
Some(find_trampoline::<T, F>),
|
2017-09-17 00:58:11 +00:00
|
|
|
elem.to_glib_none_mut().0,
|
|
|
|
func_ptr,
|
|
|
|
));
|
|
|
|
if res {
|
2021-04-20 07:19:02 +00:00
|
|
|
Some(elem.get::<T>().expect("Iterator::find"))
|
2017-09-17 00:58:11 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_iterator_foreach")]
|
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;
|
2019-01-30 13:02:03 +00:00
|
|
|
let func_ptr = &mut func as *mut F as gpointer;
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
let res = ffi::gst_iterator_foreach(
|
2017-09-17 00:58:11 +00:00
|
|
|
self.to_glib_none_mut().0,
|
2019-01-30 13:02:03 +00:00
|
|
|
Some(foreach_trampoline::<T, F>),
|
2017-09-17 00:58:11 +00:00
|
|
|
func_ptr,
|
2017-09-17 11:39:17 +00:00
|
|
|
);
|
|
|
|
|
2020-03-19 11:22:20 +00:00
|
|
|
#[allow(clippy::wildcard_in_or_patterns)]
|
2017-09-17 11:39:17 +00:00
|
|
|
match res {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::GST_ITERATOR_OK | ffi::GST_ITERATOR_DONE => Ok(()),
|
|
|
|
ffi::GST_ITERATOR_RESYNC => Err(IteratorError::Resync),
|
|
|
|
ffi::GST_ITERATOR_ERROR | _ => Err(IteratorError::Error),
|
2017-09-17 11:39:17 +00:00
|
|
|
}
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_iterator_fold")]
|
2018-08-13 17:35:28 +00:00
|
|
|
pub fn fold<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;
|
2019-01-30 13:02:03 +00:00
|
|
|
let func_ptr = &mut func as *mut F as gpointer;
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
let mut accum = Some(init);
|
2023-01-05 16:55:38 +00:00
|
|
|
let mut ret = glib::Value::from_type_unchecked(glib::Type::POINTER);
|
2020-11-21 13:46:48 +00:00
|
|
|
glib::gobject_ffi::g_value_set_pointer(
|
2017-09-17 00:58:11 +00:00
|
|
|
ret.to_glib_none_mut().0,
|
|
|
|
&mut accum as *mut _ as gpointer,
|
|
|
|
);
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
let res = ffi::gst_iterator_fold(
|
2017-09-17 00:58:11 +00:00
|
|
|
self.to_glib_none_mut().0,
|
2019-01-30 13:02:03 +00:00
|
|
|
Some(fold_trampoline::<T, U, F>),
|
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
|
|
|
|
2020-03-19 11:22:20 +00:00
|
|
|
#[allow(clippy::wildcard_in_or_patterns)]
|
2017-09-17 00:58:11 +00:00
|
|
|
match res {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::GST_ITERATOR_OK | ffi::GST_ITERATOR_DONE => Ok(accum.unwrap()),
|
|
|
|
ffi::GST_ITERATOR_RESYNC => Err(IteratorError::Resync),
|
|
|
|
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
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + StaticType + ToValue + Send + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_iterator_new")]
|
2017-09-17 15:41:02 +00:00
|
|
|
pub fn new<I: IteratorImpl<T>>(imp: I) -> Self {
|
2020-03-22 14:18:47 +00:00
|
|
|
assert_initialized_main_thread!();
|
2017-09-17 00:58:11 +00:00
|
|
|
static DUMMY_COOKIE: u32 = 0;
|
|
|
|
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
let it = ffi::gst_iterator_new(
|
2017-09-17 15:41:02 +00:00
|
|
|
mem::size_of::<RsIterator<T, I>>() as u32,
|
2021-04-27 15:15:46 +00:00
|
|
|
T::static_type().into_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>;
|
2020-03-09 10:55:14 +00:00
|
|
|
ptr::write(&mut (*it).imp, imp);
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + StaticType + ToValue + Clone + Send + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
|
|
|
pub fn from_vec(items: Vec<T>) -> Self {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2017-09-17 00:58:11 +00:00
|
|
|
Self::new(VecIteratorImpl::new(items))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:56:53 +00:00
|
|
|
impl<T: 'static> IntoGlibPtr<*mut ffi::GstIterator> for Iterator<T> {
|
2022-12-18 08:18:31 +00:00
|
|
|
#[inline]
|
2022-05-06 19:41:15 +00:00
|
|
|
unsafe fn into_glib_ptr(self) -> *mut ffi::GstIterator {
|
|
|
|
let s = mem::ManuallyDrop::new(self);
|
|
|
|
let it = s.to_glib_none().0;
|
|
|
|
it as *mut _
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + StaticType + ToValue + Send + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
2020-11-21 13:46:48 +00:00
|
|
|
iter: ffi::GstIterator,
|
2020-03-09 10:55:14 +00:00
|
|
|
imp: 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
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + StaticType + ToValue + Send + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
|
|
|
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>>(
|
2020-11-21 13:46:48 +00:00
|
|
|
it: *const ffi::GstIterator,
|
|
|
|
copy: *mut ffi::GstIterator,
|
2017-09-17 15:41:02 +00:00
|
|
|
) where
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + StaticType + ToValue + Send + 'static,
|
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());
|
|
|
|
}
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe extern "C" fn rs_iterator_free<T, I: IteratorImpl<T>>(it: *mut ffi::GstIterator)
|
2017-09-17 15:41:02 +00:00
|
|
|
where
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + StaticType + ToValue + Send + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
|
|
|
let it = it as *mut RsIterator<T, I>;
|
2020-03-09 10:55:14 +00:00
|
|
|
ptr::drop_in_place(&mut (*it).imp);
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
unsafe extern "C" fn rs_iterator_next<T, I: IteratorImpl<T>>(
|
2020-11-21 13:46:48 +00:00
|
|
|
it: *mut ffi::GstIterator,
|
|
|
|
result: *mut glib::gobject_ffi::GValue,
|
|
|
|
) -> ffi::GstIteratorResult
|
2017-09-17 15:41:02 +00:00
|
|
|
where
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + StaticType + ToValue + Send + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
|
|
|
let it = it as *mut RsIterator<T, I>;
|
2020-03-09 10:55:14 +00:00
|
|
|
match (*it).imp.next() {
|
2017-09-17 11:39:17 +00:00
|
|
|
Some(Ok(value)) => {
|
2017-09-17 15:41:02 +00:00
|
|
|
let value = value.to_value();
|
2020-03-09 10:55:14 +00:00
|
|
|
ptr::write(result, value.into_raw());
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::GST_ITERATOR_OK
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
2020-11-21 13:46:48 +00:00
|
|
|
None => ffi::GST_ITERATOR_DONE,
|
2017-09-17 11:39:17 +00:00
|
|
|
Some(Err(res)) => match res {
|
2020-11-21 13:46:48 +00:00
|
|
|
IteratorError::Resync => ffi::GST_ITERATOR_RESYNC,
|
|
|
|
IteratorError::Error => ffi::GST_ITERATOR_ERROR,
|
2017-09-17 11:39:17 +00:00
|
|
|
},
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe extern "C" fn rs_iterator_resync<T, I: IteratorImpl<T>>(it: *mut ffi::GstIterator)
|
2017-09-17 15:41:02 +00:00
|
|
|
where
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + StaticType + ToValue + Send + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
|
|
|
let it = it as *mut RsIterator<T, I>;
|
2020-03-09 10:55:14 +00:00
|
|
|
(*it).imp.resync();
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: StaticType + ToValue + FromValue<'a> + Clone + Send + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
2017-09-17 00:58:11 +00:00
|
|
|
fn new(items: Vec<T>) -> Self {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-27 10:36:40 +00:00
|
|
|
Self { pos: 0, items }
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
impl<T> IteratorImpl<T> for VecIteratorImpl<T>
|
|
|
|
where
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: StaticType + ToValue + FromValue<'a> + Clone + Send + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
|
|
|
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> {}
|
2019-12-18 15:04:42 +00:00
|
|
|
unsafe impl<T> Sync for Iterator<T> {}
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2023-01-07 14:46:38 +00:00
|
|
|
unsafe extern "C" fn filter_trampoline<T: StaticType, F: Fn(T) -> bool + Send + Sync + 'static>(
|
|
|
|
value: gconstpointer,
|
|
|
|
func: gconstpointer,
|
|
|
|
) -> i32
|
2017-09-17 15:41:02 +00:00
|
|
|
where
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
2020-11-21 13:46:48 +00:00
|
|
|
let value = value as *const glib::gobject_ffi::GValue;
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
let func = func as *const glib::gobject_ffi::GValue;
|
|
|
|
let func = glib::gobject_ffi::g_value_get_boxed(func);
|
2023-01-07 14:46:38 +00:00
|
|
|
let func = &*(func as *const &(dyn Any + Send + Sync + 'static));
|
|
|
|
let func = func.downcast_ref::<F>().unwrap();
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
let value = &*(value as *const glib::Value);
|
2021-04-20 07:19:02 +00:00
|
|
|
let value = value.get::<T>().expect("Iterator filter_trampoline");
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
if func(value) {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
-1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:46:38 +00:00
|
|
|
unsafe extern "C" fn filter_boxed_ref(boxed: gpointer) -> gpointer {
|
|
|
|
let boxed = Arc::from_raw(boxed as *const Box<dyn Any + 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
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:46:38 +00:00
|
|
|
unsafe extern "C" fn filter_boxed_unref(boxed: gpointer) {
|
|
|
|
let _ = Arc::from_raw(boxed as *const Box<dyn Any + Send + Sync + 'static>);
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:46:38 +00:00
|
|
|
unsafe extern "C" fn filter_boxed_get_type() -> glib::Type {
|
|
|
|
use std::sync::Once;
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2023-01-07 14:46:38 +00:00
|
|
|
static mut TYPE: glib::Type = glib::Type::INVALID;
|
|
|
|
static ONCE: Once = Once::new();
|
2020-06-04 09:35:11 +00:00
|
|
|
|
2023-01-07 14:46:38 +00:00
|
|
|
ONCE.call_once(|| {
|
2020-06-04 09:35:11 +00:00
|
|
|
let iter_type_name = {
|
2017-09-17 00:58:11 +00:00
|
|
|
let mut idx = 0;
|
|
|
|
|
|
|
|
loop {
|
2023-01-07 14:46:38 +00:00
|
|
|
let iter_type_name = glib::gformat!("GstRsIteratorFilterBoxed-{}", idx);
|
2020-11-21 13:46:48 +00:00
|
|
|
if glib::gobject_ffi::g_type_from_name(iter_type_name.as_ptr())
|
|
|
|
== glib::gobject_ffi::G_TYPE_INVALID
|
2017-09-17 00:58:11 +00:00
|
|
|
{
|
2020-06-04 09:35:11 +00:00
|
|
|
break iter_type_name;
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
idx += 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-07 14:46:38 +00:00
|
|
|
TYPE = from_glib(glib::gobject_ffi::g_boxed_type_register_static(
|
2020-06-04 09:35:11 +00:00
|
|
|
iter_type_name.as_ptr(),
|
2023-01-07 14:46:38 +00:00
|
|
|
Some(filter_boxed_ref),
|
|
|
|
Some(filter_boxed_unref),
|
|
|
|
));
|
2020-06-04 09:35:11 +00:00
|
|
|
|
2023-01-07 14:46:38 +00:00
|
|
|
assert!(TYPE.is_valid());
|
|
|
|
});
|
2020-06-04 09:35:11 +00:00
|
|
|
|
2023-01-07 14:46:38 +00:00
|
|
|
TYPE
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 13:02:03 +00:00
|
|
|
unsafe extern "C" fn find_trampoline<T, F: FnMut(T) -> bool>(
|
|
|
|
value: gconstpointer,
|
|
|
|
func: gconstpointer,
|
|
|
|
) -> i32
|
2017-09-17 15:41:02 +00:00
|
|
|
where
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
2020-11-21 13:46:48 +00:00
|
|
|
let value = value as *const glib::gobject_ffi::GValue;
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2019-01-30 13:02:03 +00:00
|
|
|
let func = func as *mut F;
|
2017-09-17 00:58:11 +00:00
|
|
|
let value = &*(value as *const glib::Value);
|
2021-04-20 07:19:02 +00:00
|
|
|
let value = value.get::<T>().expect("Iterator find_trampoline");
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
if (*func)(value) {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
-1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 13:02:03 +00:00
|
|
|
unsafe extern "C" fn foreach_trampoline<T, F: FnMut(T)>(
|
2020-11-21 13:46:48 +00:00
|
|
|
value: *const glib::gobject_ffi::GValue,
|
2019-01-30 13:02:03 +00:00
|
|
|
func: gpointer,
|
|
|
|
) where
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
2019-01-30 13:02:03 +00:00
|
|
|
let func = func as *mut F;
|
2017-09-17 00:58:11 +00:00
|
|
|
let value = &*(value as *const glib::Value);
|
2021-04-20 07:19:02 +00:00
|
|
|
let value = value.get::<T>().expect("Iterator foreach_trampoline");
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
(*func)(value);
|
|
|
|
}
|
|
|
|
|
2019-01-30 13:02:03 +00:00
|
|
|
unsafe extern "C" fn fold_trampoline<T, U, F: FnMut(U, T) -> Result<U, U>>(
|
2020-11-21 13:46:48 +00:00
|
|
|
value: *const glib::gobject_ffi::GValue,
|
|
|
|
ret: *mut glib::gobject_ffi::GValue,
|
2017-09-17 00:58:11 +00:00
|
|
|
func: gpointer,
|
2020-11-21 13:46:48 +00:00
|
|
|
) -> glib::ffi::gboolean
|
2017-09-17 15:41:02 +00:00
|
|
|
where
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + 'static,
|
2017-09-17 15:41:02 +00:00
|
|
|
{
|
2019-01-30 13:02:03 +00:00
|
|
|
let func = func as *mut F;
|
2017-09-17 00:58:11 +00:00
|
|
|
let value = &*(value as *const glib::Value);
|
2021-04-20 07:19:02 +00:00
|
|
|
let value = value.get::<T>().expect("Iterator fold_trampoline");
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
let accum = &mut *(glib::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);
|
2020-11-21 13:46:48 +00:00
|
|
|
glib::ffi::GTRUE
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
Err(next_accum) => {
|
|
|
|
*accum = Some(next_accum);
|
2020-11-21 13:46:48 +00:00
|
|
|
glib::ffi::GFALSE
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
impl<T: StaticType + 'static> Clone for Iterator<T> {
|
2022-12-18 08:18:31 +00:00
|
|
|
#[inline]
|
2017-09-17 15:41:02 +00:00
|
|
|
fn clone(&self) -> Self {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { from_glib_full(ffi::gst_iterator_copy(self.to_glib_none().0)) }
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 12:34:04 +00:00
|
|
|
impl<T> fmt::Debug for Iterator<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("Iterator")
|
|
|
|
.field("iter", &self.iter)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
impl<T> Drop for Iterator<T> {
|
2022-12-18 08:18:31 +00:00
|
|
|
#[inline]
|
2017-09-17 15:41:02 +00:00
|
|
|
fn drop(&mut self) {
|
2020-04-05 14:52:56 +00:00
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
ffi::gst_iterator_free(self.iter.as_ptr());
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 08:29:16 +00:00
|
|
|
impl<T> iter::IntoIterator for Iterator<T>
|
|
|
|
where
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + 'static,
|
2019-09-05 08:29:16 +00:00
|
|
|
{
|
|
|
|
type Item = Result<T, IteratorError>;
|
|
|
|
type IntoIter = StdIterator<T>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
Self::IntoIter::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
impl<T> glib::types::StaticType for Iterator<T> {
|
2022-12-18 08:18:31 +00:00
|
|
|
#[inline]
|
2017-09-17 15:41:02 +00:00
|
|
|
fn static_type() -> glib::types::Type {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { glib::translate::from_glib(ffi::gst_iterator_get_type()) }
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 07:19:02 +00:00
|
|
|
impl<T: StaticType + 'static> glib::value::ValueType for Iterator<T> {
|
|
|
|
type Type = Self;
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 17:39:43 +00:00
|
|
|
impl<T: StaticType + 'static> glib::value::ValueTypeOptional for Iterator<T> {}
|
|
|
|
|
2021-04-20 07:19:02 +00:00
|
|
|
unsafe impl<'a, T: StaticType + 'static> glib::value::FromValue<'a> for Iterator<T> {
|
|
|
|
type Checker = glib::value::GenericValueTypeOrNoneChecker<Self>;
|
|
|
|
|
|
|
|
unsafe fn from_value(value: &'a glib::Value) -> Self {
|
|
|
|
skip_assert_initialized!();
|
|
|
|
from_glib_none(
|
|
|
|
glib::gobject_ffi::g_value_get_boxed(value.to_glib_none().0) as *mut ffi::GstIterator
|
2017-09-17 15:41:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 07:19:02 +00:00
|
|
|
impl<T: StaticType + 'static> glib::value::ToValue for Iterator<T> {
|
|
|
|
fn to_value(&self) -> glib::Value {
|
|
|
|
let mut value = glib::Value::for_value_type::<Self>();
|
|
|
|
unsafe {
|
|
|
|
glib::gobject_ffi::g_value_set_boxed(
|
|
|
|
value.to_glib_none_mut().0,
|
|
|
|
self.to_glib_none().0 as *mut _,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
value
|
|
|
|
}
|
|
|
|
|
|
|
|
fn value_type(&self) -> glib::Type {
|
|
|
|
Self::static_type()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: StaticType + 'static> glib::value::ToValueOptional for Iterator<T> {
|
|
|
|
fn to_value_optional(s: Option<&Self>) -> glib::Value {
|
|
|
|
skip_assert_initialized!();
|
|
|
|
let mut value = glib::Value::for_value_type::<Self>();
|
|
|
|
unsafe {
|
|
|
|
glib::gobject_ffi::g_value_set_boxed(
|
|
|
|
value.to_glib_none_mut().0,
|
|
|
|
s.to_glib_none().0 as *mut _,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
value
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:56:53 +00:00
|
|
|
impl<T: StaticType + 'static> From<Iterator<T>> for glib::Value {
|
|
|
|
fn from(v: Iterator<T>) -> glib::Value {
|
|
|
|
skip_assert_initialized!();
|
|
|
|
let mut value = glib::Value::for_value_type::<Iterator<T>>();
|
|
|
|
unsafe {
|
|
|
|
glib::gobject_ffi::g_value_take_boxed(
|
|
|
|
value.to_glib_none_mut().0,
|
|
|
|
v.into_glib_ptr() as *mut _,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
impl<T> glib::translate::GlibPtrDefault for Iterator<T> {
|
2020-11-21 13:46:48 +00:00
|
|
|
type GlibType = *mut ffi::GstIterator;
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 17:03:08 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
unsafe impl<T: StaticType + 'static> TransparentPtrType for Iterator<T> {}
|
|
|
|
|
2017-09-17 15:41:02 +00:00
|
|
|
#[doc(hidden)]
|
2020-11-21 13:46:48 +00:00
|
|
|
impl<'a, T: 'static> glib::translate::ToGlibPtr<'a, *const ffi::GstIterator> for Iterator<T> {
|
2022-12-17 16:48:35 +00:00
|
|
|
type Storage = PhantomData<&'a Iterator<T>>;
|
2017-09-17 15:41:02 +00:00
|
|
|
|
2022-12-18 08:18:31 +00:00
|
|
|
#[inline]
|
2020-11-21 13:46:48 +00:00
|
|
|
fn to_glib_none(&'a self) -> glib::translate::Stash<'a, *const ffi::GstIterator, Self> {
|
2022-12-17 16:48:35 +00:00
|
|
|
glib::translate::Stash(self.iter.as_ptr(), PhantomData)
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
fn to_glib_full(&self) -> *const ffi::GstIterator {
|
2017-09-17 15:41:02 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2020-11-21 13:46:48 +00:00
|
|
|
impl<'a, T: 'static> glib::translate::ToGlibPtrMut<'a, *mut ffi::GstIterator> for Iterator<T> {
|
2022-12-17 16:48:35 +00:00
|
|
|
type Storage = PhantomData<&'a mut Iterator<T>>;
|
2017-09-17 15:41:02 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_glib_none_mut(
|
|
|
|
&'a mut self,
|
2020-11-21 13:46:48 +00:00
|
|
|
) -> glib::translate::StashMut<'a, *mut ffi::GstIterator, Self> {
|
2022-12-17 16:48:35 +00:00
|
|
|
glib::translate::StashMut(self.iter.as_ptr(), PhantomData)
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2020-11-21 13:46:48 +00:00
|
|
|
impl<T: StaticType> glib::translate::FromGlibPtrNone<*const ffi::GstIterator> for Iterator<T> {
|
2017-09-17 15:41:02 +00:00
|
|
|
#[inline]
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe fn from_glib_none(ptr: *const ffi::GstIterator) -> Self {
|
2022-12-25 10:47:02 +00:00
|
|
|
debug_assert_ne!(
|
2021-04-27 15:15:46 +00:00
|
|
|
glib::gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().into_glib()),
|
2020-11-21 13:46:48 +00:00
|
|
|
glib::ffi::GFALSE
|
2017-09-17 15:41:02 +00:00
|
|
|
);
|
2020-11-21 13:46:48 +00:00
|
|
|
from_glib_full(ffi::gst_iterator_copy(ptr))
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2020-11-21 13:46:48 +00:00
|
|
|
impl<T: StaticType> glib::translate::FromGlibPtrNone<*mut ffi::GstIterator> for Iterator<T> {
|
2017-09-17 15:41:02 +00:00
|
|
|
#[inline]
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe fn from_glib_none(ptr: *mut ffi::GstIterator) -> Self {
|
2022-12-25 10:47:02 +00:00
|
|
|
debug_assert_ne!(
|
2021-04-27 15:15:46 +00:00
|
|
|
glib::gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().into_glib()),
|
2020-11-21 13:46:48 +00:00
|
|
|
glib::ffi::GFALSE
|
2017-09-17 15:41:02 +00:00
|
|
|
);
|
2020-11-21 13:46:48 +00:00
|
|
|
from_glib_full(ffi::gst_iterator_copy(ptr))
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2020-11-21 13:46:48 +00:00
|
|
|
impl<T: StaticType> glib::translate::FromGlibPtrBorrow<*mut ffi::GstIterator> for Iterator<T> {
|
2017-09-17 15:41:02 +00:00
|
|
|
#[inline]
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe fn from_glib_borrow(ptr: *mut ffi::GstIterator) -> Borrowed<Self> {
|
2022-12-25 10:47:02 +00:00
|
|
|
debug_assert!(!ptr.is_null());
|
|
|
|
debug_assert_ne!(
|
2021-04-27 15:15:46 +00:00
|
|
|
glib::gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().into_glib()),
|
2020-11-21 13:46:48 +00:00
|
|
|
glib::ffi::GFALSE
|
2017-09-17 15:41:02 +00:00
|
|
|
);
|
2020-04-05 14:52:56 +00:00
|
|
|
Borrowed::new(Self {
|
2018-04-01 08:29:15 +00:00
|
|
|
iter: ptr::NonNull::new_unchecked(ptr),
|
2017-09-17 15:41:02 +00:00
|
|
|
phantom: PhantomData,
|
2020-04-05 14:52:56 +00:00
|
|
|
})
|
2017-09-17 15:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2020-11-21 13:46:48 +00:00
|
|
|
impl<T: StaticType> glib::translate::FromGlibPtrFull<*mut ffi::GstIterator> for Iterator<T> {
|
2017-09-17 15:41:02 +00:00
|
|
|
#[inline]
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe fn from_glib_full(ptr: *mut ffi::GstIterator) -> Self {
|
2022-12-25 10:47:02 +00:00
|
|
|
debug_assert!(!ptr.is_null());
|
|
|
|
debug_assert_ne!(
|
2021-04-27 15:15:46 +00:00
|
|
|
glib::gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().into_glib()),
|
2020-11-21 13:46:48 +00:00
|
|
|
glib::ffi::GFALSE
|
2017-09-17 15:41:02 +00:00
|
|
|
);
|
|
|
|
Self {
|
2018-04-01 08:29:15 +00:00
|
|
|
iter: ptr::NonNull::new_unchecked(ptr),
|
2017-09-17 15:41:02 +00:00
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 12:34:05 +00:00
|
|
|
pub struct StdIterator<T> {
|
|
|
|
inner: Iterator<T>,
|
|
|
|
error: Option<IteratorError>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> StdIterator<T> {
|
|
|
|
fn new(inner: Iterator<T>) -> Self {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2019-09-04 12:34:05 +00:00
|
|
|
Self { inner, error: None }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: StaticType + 'static> Clone for StdIterator<T> {
|
2022-12-18 08:18:31 +00:00
|
|
|
#[inline]
|
2019-09-04 12:34:05 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: self.inner.clone(),
|
|
|
|
error: self.error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> fmt::Debug for StdIterator<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("StdIterator")
|
|
|
|
.field("inner", &self.inner)
|
|
|
|
.field("error", &self.error)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> iter::Iterator for StdIterator<T>
|
|
|
|
where
|
2021-04-20 07:19:02 +00:00
|
|
|
for<'a> T: FromValue<'a> + 'static,
|
2019-09-04 12:34:05 +00:00
|
|
|
{
|
|
|
|
type Item = Result<T, IteratorError>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
match self.error {
|
|
|
|
// Fuse the iterator after returning IteratorError::Error
|
|
|
|
Some(IteratorError::Error) => return None,
|
|
|
|
|
|
|
|
// The iterator needs a resync
|
2019-09-24 09:31:28 +00:00
|
|
|
Some(IteratorError::Resync) => self.inner.resync(),
|
2019-09-04 12:34:05 +00:00
|
|
|
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = self.inner.next();
|
2019-09-24 09:31:28 +00:00
|
|
|
self.error = res.as_ref().err().copied();
|
2019-09-04 12:34:05 +00:00
|
|
|
res.transpose()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 13:58:46 +00:00
|
|
|
impl<T> iter::FusedIterator for StdIterator<T> where for<'a> T: FromValue<'a> + 'static {}
|
|
|
|
|
2017-09-17 00:58:11 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vec() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
let vec = vec![1i32, 2, 3];
|
|
|
|
let mut it = Iterator::from_vec(vec);
|
2018-08-13 17:35:28 +00:00
|
|
|
let val = it.next();
|
|
|
|
assert_eq!(val, Ok(Some(1)));
|
|
|
|
let val = it.next();
|
|
|
|
assert_eq!(val, Ok(Some(2)));
|
|
|
|
let val = it.next();
|
|
|
|
assert_eq!(val, Ok(Some(3)));
|
|
|
|
assert_eq!(it.next(), Ok(None));
|
2017-09-17 11:39:17 +00:00
|
|
|
|
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();
|
2018-08-13 17:35:28 +00:00
|
|
|
while let Ok(Some(res)) = it.next() {
|
|
|
|
vals.push(res);
|
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() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
let vec = vec![1i32, 2, 3];
|
2018-08-13 17:35:28 +00:00
|
|
|
let mut it = Iterator::from_vec(vec).filter(|val| val % 2 == 1);
|
|
|
|
|
|
|
|
let mut vals = Vec::new();
|
|
|
|
while let Ok(Some(res)) = it.next() {
|
|
|
|
vals.push(res);
|
|
|
|
}
|
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() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2017-09-17 11:39:17 +00:00
|
|
|
// Our find
|
2017-09-17 00:58:11 +00:00
|
|
|
let vec = vec![1i32, 2, 3];
|
2018-08-13 17:35:28 +00:00
|
|
|
let val = Iterator::from_vec(vec).find(|val| val == 2);
|
2017-09-17 15:41:02 +00:00
|
|
|
assert_eq!(val.unwrap(), 2);
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_foreach() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2017-09-17 00:58:11 +00:00
|
|
|
|
|
|
|
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() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2017-09-17 00:58:11 +00:00
|
|
|
|
2017-09-17 11:39:17 +00:00
|
|
|
// Our fold
|
2017-09-17 00:58:11 +00:00
|
|
|
let vec = vec![1i32, 2, 3];
|
2018-08-13 17:35:28 +00:00
|
|
|
let res = Iterator::from_vec(vec).fold(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);
|
|
|
|
}
|
2019-09-04 12:34:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_std() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2020-04-11 16:57:09 +00:00
|
|
|
|
2019-09-05 08:29:16 +00:00
|
|
|
let mut it = Iterator::from_vec(vec![1i32, 2, 3]).into_iter();
|
2019-09-04 12:34:05 +00:00
|
|
|
assert_eq!(it.next(), Some(Ok(1)));
|
|
|
|
assert_eq!(it.next(), Some(Ok(2)));
|
|
|
|
assert_eq!(it.next(), Some(Ok(3)));
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
2019-09-05 09:01:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_into_iter() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2020-04-11 16:57:09 +00:00
|
|
|
|
2019-09-05 09:01:16 +00:00
|
|
|
let mut v = vec![1i32, 2, 3].into_iter();
|
|
|
|
for x in Iterator::from_vec(vec![1i32, 2, 3]) {
|
|
|
|
assert_eq!(x.unwrap(), v.next().unwrap());
|
|
|
|
}
|
|
|
|
assert_eq!(v.next(), None);
|
|
|
|
}
|
|
|
|
|
2019-09-04 12:34:05 +00:00
|
|
|
#[test]
|
|
|
|
fn test_std_resync_collect() {
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2019-09-04 12:34:05 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
let bin = crate::Bin::new(None);
|
2022-10-19 11:13:57 +00:00
|
|
|
let id1 = crate::ElementFactory::make("identity").build().unwrap();
|
|
|
|
let id2 = crate::ElementFactory::make("identity").build().unwrap();
|
2019-09-04 12:34:05 +00:00
|
|
|
|
|
|
|
bin.add(&id1).unwrap();
|
|
|
|
|
2019-09-05 08:29:16 +00:00
|
|
|
let mut it = bin.iterate_elements().into_iter();
|
2019-09-04 12:34:05 +00:00
|
|
|
assert_eq!(it.next().unwrap().unwrap(), id1);
|
|
|
|
|
|
|
|
bin.add(&id2).unwrap();
|
|
|
|
|
|
|
|
let res = it.by_ref().collect::<Result<Vec<_>, _>>().unwrap_err();
|
|
|
|
assert_eq!(res, IteratorError::Resync);
|
|
|
|
|
|
|
|
let mut elems = BTreeSet::new();
|
|
|
|
elems.insert(id1);
|
|
|
|
elems.insert(id2);
|
|
|
|
|
|
|
|
let res = it.by_ref().collect::<Result<BTreeSet<_>, _>>().unwrap();
|
|
|
|
assert_eq!(res, elems);
|
|
|
|
|
|
|
|
let res = it.collect::<Result<Vec<_>, _>>().unwrap();
|
|
|
|
assert!(res.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_std_resync_find() {
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::prelude::*;
|
2019-09-04 12:34:05 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2019-09-04 12:34:05 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
let bin = crate::Bin::new(None);
|
2022-10-19 11:13:57 +00:00
|
|
|
let id1 = crate::ElementFactory::make("identity").build().unwrap();
|
|
|
|
let id2 = crate::ElementFactory::make("identity").build().unwrap();
|
2019-09-04 12:34:05 +00:00
|
|
|
|
|
|
|
bin.add(&id1).unwrap();
|
|
|
|
|
2019-09-05 08:29:16 +00:00
|
|
|
let mut it = bin.iterate_elements().into_iter();
|
2019-09-04 12:34:05 +00:00
|
|
|
assert_eq!(it.next().unwrap().unwrap(), id1);
|
|
|
|
|
|
|
|
bin.add(&id2).unwrap();
|
|
|
|
|
|
|
|
let res = it.find(|x| x.as_ref() == Ok(&id1));
|
|
|
|
assert_eq!(res.unwrap().unwrap(), id1);
|
|
|
|
}
|
2017-09-17 00:58:11 +00:00
|
|
|
}
|