summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/lib.rs21
1 files changed, 7 insertions, 14 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 6c3b403..33ace3e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -35,9 +35,7 @@ struct IfaddrHandle {
addrs: *mut libc::ifaddrs,
}
-struct IfaddrIter<'a> {
- cur: Option<&'a libc::ifaddrs>,
-}
+struct IfaddrIter<'a>(*const libc::ifaddrs, std::marker::PhantomData<&'a libc::ifaddrs>);
impl IfaddrHandle {
fn new() -> Result<IfaddrHandle, errno::Errno> {
@@ -49,10 +47,7 @@ impl IfaddrHandle {
}
fn iter(&self) -> IfaddrIter {
- match self.addrs as usize {
- 0 => IfaddrIter{ cur: None },
- _ => IfaddrIter{ cur: Some(unsafe { &*self.addrs }) },
- }
+ IfaddrIter(self.addrs, std::marker::PhantomData)
}
}
@@ -66,13 +61,11 @@ impl<'a> Iterator for IfaddrIter<'a> {
type Item = &'a libc::ifaddrs;
fn next(&mut self) -> Option<Self::Item> {
- match self.cur {
- None => None,
- Some(addr) => {
- match addr.ifa_next as usize {
- 0 => self.cur = None,
- _ => self.cur = Some(unsafe { &*addr.ifa_next }),
- }
+ match self.0 as usize {
+ 0 => None,
+ _ => {
+ let addr = unsafe { &*self.0 };
+ self.0 = addr.ifa_next;
Some(addr)
}
}