diff --git a/gstreamer/src/iterator.rs b/gstreamer/src/iterator.rs index 95d5c9b99..9e40f2e17 100644 --- a/gstreamer/src/iterator.rs +++ b/gstreamer/src/iterator.rs @@ -200,7 +200,22 @@ where { pub fn from_vec(items: Vec) -> Self { skip_assert_initialized!(); - Self::new(VecIteratorImpl::new(items)) + Self::new(ArrayIteratorImpl::new(items)) + } + + pub fn from_array + Send + Clone + 'static>(items: A) -> Self { + skip_assert_initialized!(); + Self::new(ArrayIteratorImpl::new(items)) + } + + pub fn from_option(items: Option) -> Self { + skip_assert_initialized!(); + Self::new(OptionIteratorImpl::new(items)) + } + + pub fn from_single(item: T) -> Self { + skip_assert_initialized!(); + Self::new(OptionIteratorImpl::new(Some(item))) } } @@ -282,28 +297,32 @@ where } #[derive(Clone)] -struct VecIteratorImpl { +struct ArrayIteratorImpl { pos: usize, - items: Vec, + items: A, + phantom: PhantomData, } -impl VecIteratorImpl -where - for<'a> T: StaticType + ToValue + FromValue<'a> + Clone + Send + 'static, -{ - fn new(items: Vec) -> Self { +impl ArrayIteratorImpl { + fn new(items: A) -> Self { skip_assert_initialized!(); - Self { pos: 0, items } + Self { + pos: 0, + items, + phantom: PhantomData, + } } } -impl IteratorImpl for VecIteratorImpl +impl IteratorImpl for ArrayIteratorImpl where + A: AsRef<[T]> + Send + Clone + 'static, for<'a> T: StaticType + ToValue + FromValue<'a> + Clone + Send + 'static, { fn next(&mut self) -> Option> { - if self.pos < self.items.len() { - let res = Ok(self.items[self.pos].clone()); + let items = self.items.as_ref(); + if self.pos < items.len() { + let res = Ok(items[self.pos].clone()); self.pos += 1; return Some(res); } @@ -316,6 +335,40 @@ where } } +#[derive(Clone)] +struct OptionIteratorImpl { + finished: bool, + items: Option, +} + +impl OptionIteratorImpl { + fn new(items: Option) -> Self { + skip_assert_initialized!(); + Self { + finished: false, + items, + } + } +} + +impl IteratorImpl for OptionIteratorImpl +where + for<'a> T: StaticType + ToValue + FromValue<'a> + Clone + Send + 'static, +{ + fn next(&mut self) -> Option> { + if self.finished { + return None; + } + let res = Ok(self.items.clone()).transpose(); + self.finished = true; + res + } + + fn resync(&mut self) { + self.finished = false; + } +} + unsafe impl Send for Iterator {} unsafe impl Sync for Iterator {}