Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DriverIterator to iterate though the registered drivers #513

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 46 additions & 0 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,45 @@ impl DriverManager {
gdal_sys::GDALDestroyDriverManager();
}
}

/// Get an `Iterator` for all the drivers.
Atreyagaurav marked this conversation as resolved.
Show resolved Hide resolved
///
/// Warning: Adding or removing drivers while comsuming the
Atreyagaurav marked this conversation as resolved.
Show resolved Hide resolved
/// Iterator can have unexpected results.
Atreyagaurav marked this conversation as resolved.
Show resolved Hide resolved
pub fn all() -> DriverIterator {
DriverIterator::new()
Atreyagaurav marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Iterator for the registered `Driver`s in `DriverManager`
Atreyagaurav marked this conversation as resolved.
Show resolved Hide resolved
pub struct DriverIterator {
current: usize,
}

impl DriverIterator {
Atreyagaurav marked this conversation as resolved.
Show resolved Hide resolved
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)]
mod tests {
use std::collections::HashSet;

use super::*;

#[test]
Expand All @@ -466,4 +501,15 @@ 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());

let drivers: HashSet<String> = DriverManager::all().map(|d| d.short_name()).collect();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a little overkill, I'd just keep the count test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the same in the beginning, but repeating the same driver DriverManager::count() times would pass the test. Although that's a very unlikely mistake, this feels like a complete test.

for i in 0..DriverManager::count() {
assert!(drivers.contains(&DriverManager::get_driver(i).unwrap().short_name()))
}
}
}
Loading