Iterator: Replace ad-hoc fn iter with impl IntoIterator

std has a trait for converting things into Iterators. Make use of it.
This commit is contained in:
Jan Alexander Steffens (heftig) 2019-09-05 10:29:16 +02:00
parent a30935ad1f
commit 7a12c4d5e1
No known key found for this signature in database
GPG key ID: DE5E0C5F25941CA5

View file

@ -181,10 +181,6 @@ where
} }
} }
} }
pub fn iter(self) -> StdIterator<T> {
StdIterator::new(self)
}
} }
impl<T> Iterator<T> impl<T> Iterator<T>
@ -491,6 +487,18 @@ impl<T> Drop for Iterator<T> {
} }
} }
impl<T> iter::IntoIterator for Iterator<T>
where
for<'a> T: FromValueOptional<'a> + 'static,
{
type Item = Result<T, IteratorError>;
type IntoIter = StdIterator<T>;
fn into_iter(self) -> Self::IntoIter {
Self::IntoIter::new(self)
}
}
impl<T> glib::types::StaticType for Iterator<T> { impl<T> glib::types::StaticType for Iterator<T> {
fn static_type() -> glib::types::Type { fn static_type() -> glib::types::Type {
unsafe { glib::translate::from_glib(gst_sys::gst_iterator_get_type()) } unsafe { glib::translate::from_glib(gst_sys::gst_iterator_get_type()) }
@ -752,7 +760,7 @@ mod tests {
#[test] #[test]
fn test_std() { fn test_std() {
let mut it = Iterator::from_vec(vec![1i32, 2, 3]).iter(); let mut it = Iterator::from_vec(vec![1i32, 2, 3]).into_iter();
assert_eq!(it.next(), Some(Ok(1))); assert_eq!(it.next(), Some(Ok(1)));
assert_eq!(it.next(), Some(Ok(2))); assert_eq!(it.next(), Some(Ok(2)));
assert_eq!(it.next(), Some(Ok(3))); assert_eq!(it.next(), Some(Ok(3)));
@ -772,7 +780,7 @@ mod tests {
bin.add(&id1).unwrap(); bin.add(&id1).unwrap();
let mut it = bin.iterate_elements().iter(); let mut it = bin.iterate_elements().into_iter();
assert_eq!(it.next().unwrap().unwrap(), id1); assert_eq!(it.next().unwrap().unwrap(), id1);
bin.add(&id2).unwrap(); bin.add(&id2).unwrap();
@ -803,7 +811,7 @@ mod tests {
bin.add(&id1).unwrap(); bin.add(&id1).unwrap();
let mut it = bin.iterate_elements().iter(); let mut it = bin.iterate_elements().into_iter();
assert_eq!(it.next().unwrap().unwrap(), id1); assert_eq!(it.next().unwrap().unwrap(), id1);
bin.add(&id2).unwrap(); bin.add(&id2).unwrap();