Debian Codegraph @

src/collection/disks/unix/macos/io_kit/io_iterator.rs

raw symbols SCIP
1 //! Based on [heim's](https://github.com/heim-rs/heim/blob/master/heim-common/src/sys/macos/iokit/io_iterator.rs).
2 //! implementation.
3
4 use std::ops::{Deref, DerefMut};
5
6 use mach2::kern_return;
7
8 use super::{bindings::*, io_object::IoObject};
9
10 /// Safe wrapper around the IOKit `io_iterator_t` type.
11 #[derive(Debug)]
12 pub struct IoIterator(io_iterator_t);
13
14 impl From<io_iterator_t> for IoIterator {
15 fn from(iter: io_iterator_t) -> IoIterator {
16 IoIterator(iter)
17 }
18 }
19
20 impl Deref for IoIterator {
21 type Target = io_iterator_t;
22
23 fn deref(&self) -> &Self::Target {
24 &self.0
25 }
26 }
27
28 impl DerefMut for IoIterator {
29 fn deref_mut(&mut self) -> &mut Self::Target {
30 &mut self.0
31 }
32 }
33
34 impl Iterator for IoIterator {
35 type Item = IoObject;
36
37 fn next(&mut self) -> Option<Self::Item> {
38 // Basically, we just stop when we hit 0.
39
40 // SAFETY: IOKit call, the passed argument (an `io_iterator_t`) is what is
41 // expected.
42 match unsafe { IOIteratorNext(self.0) } {
43 0 => None,
44 io_object => Some(IoObject::from(io_object)),
45 }
46 }
47 }
48
49 impl Drop for IoIterator {
50 fn drop(&mut self) {
51 // SAFETY: IOKit call, the passed argument (an `io_iterator_t`) is what is
52 // expected.
53 let result = unsafe { IOObjectRelease(self.0) };
54 assert_eq!(result, kern_return::KERN_SUCCESS);
55 }
56 }