Skip to content

Commit

Permalink
Add DriverIterator to iterate though the registered drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
Atreyagaurav committed Jan 5, 2024
1 parent 481a0d9 commit 6286d74
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changes

## Unreleased
- Add `DriverIterator` format to iterate through drivers, as well as `DriverManager::all()` method that provides the iterator.

- **Breaking**: `Feature::set_field_xxx` now take `&mut self`
- <https://github.com/georust/gdal/pull/505>

Expand Down
39 changes: 39 additions & 0 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,39 @@ impl DriverManager {
gdal_sys::GDALDestroyDriverManager();
}
}

/// Get an `Iterator` for all the drivers.
///
/// Warning: Adding or removing drivers while comsuming the
/// Iterator can have unexpected results.
pub fn all() -> DriverIterator {
DriverIterator::new()
}
}

/// Iterator for the registered `Driver`s in `DriverManager`
pub struct DriverIterator {
current: usize,
}

impl DriverIterator {
pub fn new() -> Self {
DriverIterator { current: 0 }
}
}

impl Iterator for DriverIterator {
type Item = Driver;

fn next(&mut self) -> Option<Self::Item> {
match DriverManager::get_driver(self.current) {
Ok(d) => {
self.current += 1;
Some(d)
}
Err(_) => None,
}
}
}

#[cfg(test)]
Expand All @@ -466,4 +499,10 @@ mod tests {
assert!(DriverManager::count() > 0);
assert!(DriverManager::get_driver(0).is_ok());
}

#[test]
fn test_driver_iterator() {
assert_eq!(DriverManager::count(), DriverIterator::new().count());
assert_eq!(DriverManager::count(), DriverManager::all().count());
}
}

0 comments on commit 6286d74

Please sign in to comment.