Skip to content

Commit 82db2fe

Browse files
Add DriverIterator to iterate though the registered drivers (#513)
* Add `DriverIterator` to iterate though the registered drivers * Apply suggestions from code review Co-authored-by: Laurențiu Nicola <[email protected]> --------- Co-authored-by: Laurențiu Nicola <[email protected]>
1 parent 924bdbf commit 82db2fe

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changes
22

33
## Unreleased
4+
- Add `DriverIterator` format to iterate through drivers, as well as `DriverManager::all()` method that provides the iterator.
5+
46
- **Breaking**: `Feature::set_field_xxx` now take `&mut self`
57
- <https://github.com/georust/gdal/pull/505>
68

src/driver.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,39 @@ impl DriverManager {
451451
gdal_sys::GDALDestroyDriverManager();
452452
}
453453
}
454+
455+
/// Get an `Iterator` over for all the loaded drivers.
456+
///
457+
/// Warning: Adding or removing drivers while consuming the
458+
/// iterator is safe, but can produce less useful results.
459+
pub fn all() -> DriverIterator {
460+
DriverIterator { current: 0 }
461+
}
462+
}
463+
464+
/// Iterator for the registered [`Driver`]s in [`DriverManager`]
465+
pub struct DriverIterator {
466+
current: usize,
467+
}
468+
469+
impl Iterator for DriverIterator {
470+
type Item = Driver;
471+
472+
fn next(&mut self) -> Option<Self::Item> {
473+
match DriverManager::get_driver(self.current) {
474+
Ok(d) => {
475+
self.current += 1;
476+
Some(d)
477+
}
478+
Err(_) => None,
479+
}
480+
}
454481
}
455482

456483
#[cfg(test)]
457484
mod tests {
485+
use std::collections::HashSet;
486+
458487
use super::*;
459488

460489
#[test]
@@ -466,4 +495,14 @@ mod tests {
466495
assert!(DriverManager::count() > 0);
467496
assert!(DriverManager::get_driver(0).is_ok());
468497
}
498+
499+
#[test]
500+
fn test_driver_iterator() {
501+
assert_eq!(DriverManager::count(), DriverManager::all().count());
502+
503+
let drivers: HashSet<String> = DriverManager::all().map(|d| d.short_name()).collect();
504+
for i in 0..DriverManager::count() {
505+
assert!(drivers.contains(&DriverManager::get_driver(i).unwrap().short_name()))
506+
}
507+
}
469508
}

0 commit comments

Comments
 (0)