Repository: brendanzab/gl-rs (language: rust)

Issue:
Remove EnumIterator and CmdIterator
I'm not sure why these are here, but just using regular iteration seems to work fine. Inserting a `panic!` in the `else` branch of the `next` functions does not cause an error when running the tests. Removing them has no obvious effect on compilation performance either.



Discussion / hints:

Fix patch:
diff --git a/gl_generator/generators/debug_struct_gen.rs b/gl_generator/generators/debug_struct_gen.rs
index 8cd65be8..06f2d05e 100644
--- a/gl_generator/generators/debug_struct_gen.rs
+++ b/gl_generator/generators/debug_struct_gen.rs
@@ -62,8 +62,8 @@ fn write_type_aliases<W>(registry: &Registry, dest: &mut W) -> io::Result<()> wh
 
 /// Creates all the `<enum>` elements at the root of the bindings.
 fn write_enums<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: io::Write {
-    for e in registry.enum_iter() {
-        try!(super::gen_enum_item(e, "types::", dest));
+    for enm in &registry.enums {
+        try!(super::gen_enum_item(enm, "types::", dest));
     }
 
     Ok(())
@@ -134,11 +134,11 @@ fn write_struct<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W:
         api = super::gen_struct_name(registry.api)
     ));
 
-    for c in registry.cmd_iter() {
-        if let Some(v) = registry.aliases.get(&c.proto.ident) {
+    for cmd in &registry.cmds {
+        if let Some(v) = registry.aliases.get(&cmd.proto.ident) {
             try!(writeln!(dest, "/// Fallbacks: {}", v.join(", ")));
         }
-        try!(writeln!(dest, "pub {name}: FnPtr,", name = c.proto.ident));
+        try!(writeln!(dest, "pub {name}: FnPtr,", name = cmd.proto.ident));
     }
 
     writeln!(dest, "}}")
@@ -171,12 +171,12 @@ fn write_impl<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: i
         api = super::gen_struct_name(registry.api)
     ));
 
-    for c in registry.cmd_iter() {
+    for cmd in &registry.cmds {
         try!(writeln!(dest,
             "{name}: FnPtr::new(metaloadfn(\"{symbol}\", &[{fallbacks}])),",
-            name = c.proto.ident,
-            symbol = super::gen_symbol_name(registry.api, &c.proto.ident),
-            fallbacks = match registry.aliases.get(&c.proto.ident) {
+            name = cmd.proto.ident,
+            symbol = super::gen_symbol_name(registry.api, &cmd.proto.ident),
+            fallbacks = match registry.aliases.get(&cmd.proto.ident) {
                 Some(fbs) => {
                     fbs.iter()
                        .map(|name| format!("\"{}\"", super::gen_symbol_name(registry.api, &name)))
@@ -192,11 +192,11 @@ fn write_impl<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: i
         }}"
     ));
 
-    for c in registry.cmd_iter() {
-        let idents = super::gen_parameters(c, true, false);
-        let typed_params = super::gen_parameters(c, false, true);
+    for cmd in &registry.cmds {
+        let idents = super::gen_parameters(cmd, true, false);
+        let typed_params = super::gen_parameters(cmd, false, true);
         let println = format!("println!(\"[OpenGL] {}({})\" {});",
-                                c.proto.ident,
+                                cmd.proto.ident,
                                 (0 .. idents.len()).map(|_| "{:?}".to_string()).collect::<Vec<_>>().join(", "),
                                 idents.iter().zip(typed_params.iter())
                                       .map(|(name, ty)| {
@@ -216,13 +216,13 @@ fn write_impl<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: i
                 {print_err}
                 r
             }}",
-            name = c.proto.ident,
-            params = super::gen_parameters(c, true, true).join(", "),
+            name = cmd.proto.ident,
+            params = super::gen_parameters(cmd, true, true).join(", "),
             typed_params = typed_params.join(", "),
-            return_suffix = super::gen_return_type(c),
+            return_suffix = super::gen_return_type(cmd),
             idents = idents.join(", "),
             println = println,
-            print_err = if c.proto.ident != "GetError" && registry.cmd_iter().find(|c| c.proto.ident == "GetError").is_some() {
+            print_err = if cmd.proto.ident != "GetError" && registry.cmds.iter().find(|cmd| cmd.proto.ident == "GetError").is_some() {
                 format!(r#"match __gl_imports::mem::transmute::<_, extern "system" fn() -> u32>
                     (self.GetError.f)() {{ 0 => (), r => println!("[OpenGL] ^ GL error triggered: {{}}", r) }}"#)
             } else {
diff --git a/gl_generator/generators/global_gen.rs b/gl_generator/generators/global_gen.rs
index 5772d781..797e5c52 100644
--- a/gl_generator/generators/global_gen.rs
+++ b/gl_generator/generators/global_gen.rs
@@ -85,8 +85,8 @@ fn write_type_aliases<W>(registry: &Registry, dest: &mut W) -> io::Result<()> wh
 
 /// Creates all the `<enum>` elements at the root of the bindings.
 fn write_enums<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: io::Write {
-    for e in registry.enum_iter() {
-        try!(super::gen_enum_item(e, "types::", dest));
+    for enm in &registry.enums {
+        try!(super::gen_enum_item(enm, "types::", dest));
     }
 
     Ok(())
@@ -97,7 +97,7 @@ fn write_enums<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W:
 /// The function calls the corresponding function pointer stored in the `storage` module created
 ///  by `write_ptrs`.
 fn write_fns<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: io::Write {
-    for c in registry.cmd_iter() {
+    for c in &registry.cmds {
         if let Some(v) = registry.aliases.get(&c.proto.ident) {
             try!(writeln!(dest, "/// Fallbacks: {}", v.join(", ")));
         }
@@ -152,7 +152,7 @@ fn write_ptrs<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: i
             use super::__gl_imports::raw;
             use super::FnPtr;"));
 
-    for c in registry.cmd_iter() {
+    for c in &registry.cmds {
         try!(writeln!(dest,
             "pub static mut {name}: FnPtr = FnPtr {{
                 f: super::missing_fn_panic as *const raw::c_void,
@@ -170,7 +170,7 @@ fn write_ptrs<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: i
 /// Each module contains `is_loaded` and `load_with` which interact with the `storage` module
 ///  created by `write_ptrs`.
 fn write_fn_mods<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: io::Write {
-    for c in registry.cmd_iter() {
+    for c in &registry.cmds {
         let fallbacks = match registry.aliases.get(&c.proto.ident) {
             Some(v) => {
                 let names = v.iter().map(|name| format!("\"{}\"", super::gen_symbol_name(registry.api, &name[..]))).collect::<Vec<_>>();
@@ -233,7 +233,7 @@ fn write_load_fn<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W
         pub fn load_with<F>(mut loadfn: F) where F: FnMut(&str) -> *const __gl_imports::raw::c_void {{
     "));
 
-    for c in registry.cmd_iter() {
+    for c in &registry.cmds {
         try!(writeln!(dest, "{cmd_name}::load_with(|s| loadfn(s));",
                       cmd_name = &c.proto.ident[..]));
     }
diff --git a/gl_generator/generators/static_gen.rs b/gl_generator/generators/static_gen.rs
index 818e3f1d..4617e7bf 100644
--- a/gl_generator/generators/static_gen.rs
+++ b/gl_generator/generators/static_gen.rs
@@ -60,8 +60,8 @@ fn write_type_aliases<W>(registry: &Registry, dest: &mut W) -> io::Result<()> wh
 
 /// Creates all the `<enum>` elements at the root of the bindings.
 fn write_enums<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: io::Write {
-    for e in registry.enum_iter() {
-        try!(super::gen_enum_item(e, "types::", dest));
+    for enm in &registry.enums {
+        try!(super::gen_enum_item(enm, "types::", dest));
     }
 
     Ok(())
@@ -75,14 +75,14 @@ fn write_fns<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: io
         #[allow(non_snake_case, unused_variables, dead_code)]
         extern \"system\" {{"));
 
-    for c in registry.cmd_iter() {
+    for cmd in &registry.cmds {
         try!(writeln!(dest,
             "#[link_name=\"{symbol}\"]
             pub fn {name}({params}) -> {return_suffix};",
-            symbol = super::gen_symbol_name(registry.api, &c.proto.ident),
-            name = c.proto.ident,
-            params = super::gen_parameters(c, true, true).join(", "),
-            return_suffix = super::gen_return_type(c)
+            symbol = super::gen_symbol_name(registry.api, &cmd.proto.ident),
+            name = cmd.proto.ident,
+            params = super::gen_parameters(cmd, true, true).join(", "),
+            return_suffix = super::gen_return_type(cmd)
         ));
     }
 
diff --git a/gl_generator/generators/static_struct_gen.rs b/gl_generator/generators/static_struct_gen.rs
index 73fb0128..21d4327d 100644
--- a/gl_generator/generators/static_struct_gen.rs
+++ b/gl_generator/generators/static_struct_gen.rs
@@ -60,8 +60,8 @@ fn write_type_aliases<W>(registry: &Registry, dest: &mut W) -> io::Result<()> wh
 
 /// Creates all the `<enum>` elements at the root of the bindings.
 fn write_enums<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: io::Write {
-    for e in registry.enum_iter() {
-        try!(super::gen_enum_item(e, "types::", dest));
+    for enm in &registry.enums {
+        try!(super::gen_enum_item(enm, "types::", dest));
     }
 
     Ok(())
@@ -93,7 +93,7 @@ fn write_impl<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: i
         api = super::gen_struct_name(registry.api),
     ));
 
-    for c in registry.cmd_iter() {
+    for cmd in &registry.cmds {
         try!(writeln!(dest,
             "#[allow(non_snake_case)]
             // #[allow(unused_variables)]
@@ -102,10 +102,10 @@ fn write_impl<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: i
             pub unsafe fn {name}(&self, {typed_params}) -> {return_suffix} {{
                 {name}({idents})
             }}",
-            name = c.proto.ident,
-            typed_params = super::gen_parameters(c, true, true).join(", "),
-            return_suffix = super::gen_return_type(c),
-            idents = super::gen_parameters(c, true, false).join(", "),
+            name = cmd.proto.ident,
+            typed_params = super::gen_parameters(cmd, true, true).join(", "),
+            return_suffix = super::gen_return_type(cmd),
+            idents = super::gen_parameters(cmd, true, false).join(", "),
         ));
     }
 
@@ -123,13 +123,13 @@ fn write_fns<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: io
         #[allow(dead_code)]
         extern \"system\" {{"));
 
-    for c in registry.cmd_iter() {
+    for cmd in &registry.cmds {
         try!(writeln!(dest,
             "#[link_name=\"{symbol}\"] fn {name}({params}) -> {return_suffix};",
-            symbol = super::gen_symbol_name(registry.api, &c.proto.ident),
-            name = c.proto.ident,
-            params = super::gen_parameters(c, true, true).join(", "),
-            return_suffix = super::gen_return_type(c)
+            symbol = super::gen_symbol_name(registry.api, &cmd.proto.ident),
+            name = cmd.proto.ident,
+            params = super::gen_parameters(cmd, true, true).join(", "),
+            return_suffix = super::gen_return_type(cmd)
         ));
     }
 
diff --git a/gl_generator/generators/struct_gen.rs b/gl_generator/generators/struct_gen.rs
index 38979e63..703d7d4d 100644
--- a/gl_generator/generators/struct_gen.rs
+++ b/gl_generator/generators/struct_gen.rs
@@ -62,8 +62,8 @@ fn write_type_aliases<W>(registry: &Registry, dest: &mut W) -> io::Result<()> wh
 
 /// Creates all the `<enum>` elements at the root of the bindings.
 fn write_enums<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: io::Write {
-    for e in registry.enum_iter() {
-        try!(super::gen_enum_item(e, "types::", dest));
+    for enm in &registry.enums {
+        try!(super::gen_enum_item(enm, "types::", dest));
     }
 
     Ok(())
@@ -134,11 +134,11 @@ fn write_struct<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W:
         api = super::gen_struct_name(registry.api)
     ));
 
-    for c in registry.cmd_iter() {
-        if let Some(v) = registry.aliases.get(&c.proto.ident) {
+    for cmd in &registry.cmds {
+        if let Some(v) = registry.aliases.get(&cmd.proto.ident) {
             try!(writeln!(dest, "/// Fallbacks: {}", v.join(", ")));
         }
-        try!(writeln!(dest, "pub {name}: FnPtr,", name = c.proto.ident));
+        try!(writeln!(dest, "pub {name}: FnPtr,", name = cmd.proto.ident));
     }
 
     writeln!(dest, "}}")
@@ -171,12 +171,12 @@ fn write_impl<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: i
         api = super::gen_struct_name(registry.api)
     ));
 
-    for c in registry.cmd_iter() {
+    for cmd in &registry.cmds {
         try!(writeln!(dest,
             "{name}: FnPtr::new(metaloadfn(\"{symbol}\", &[{fallbacks}])),",
-            name = c.proto.ident,
-            symbol = super::gen_symbol_name(registry.api, &c.proto.ident),
-            fallbacks = match registry.aliases.get(&c.proto.ident) {
+            name = cmd.proto.ident,
+            symbol = super::gen_symbol_name(registry.api, &cmd.proto.ident),
+            fallbacks = match registry.aliases.get(&cmd.proto.ident) {
                 Some(fbs) => {
                     fbs.iter()
                        .map(|name| format!("\"{}\"", super::gen_symbol_name(registry.api, &name)))
@@ -192,18 +192,18 @@ fn write_impl<W>(registry: &Registry, dest: &mut W) -> io::Result<()> where W: i
         }}"
     ));
 
-    for c in registry.cmd_iter() {
+    for cmd in &registry.cmds {
         try!(writeln!(dest,
             "#[allow(non_snake_case)] #[allow(unused_variables)] #[allow(dead_code)]
             #[inline] pub unsafe fn {name}(&self, {params}) -> {return_suffix} {{ \
                 __gl_imports::mem::transmute::<_, extern \"system\" fn({typed_params}) -> {return_suffix}>\
                     (self.{name}.f)({idents}) \
             }}",
-            name = c.proto.ident,
-            params = super::gen_parameters(c, true, true).join(", "),
-            typed_params = super::gen_parameters(c, false, true).join(", "),
-            return_suffix = super::gen_return_type(c),
-            idents = super::gen_parameters(c, true, false).join(", "),
+            name = cmd.proto.ident,
+            params = super::gen_parameters(cmd, true, true).join(", "),
+            typed_params = super::gen_parameters(cmd, false, true).join(", "),
+            return_suffix = super::gen_return_type(cmd),
+            idents = super::gen_parameters(cmd, true, false).join(", "),
         ))
     }
 
diff --git a/gl_generator/registry/mod.rs b/gl_generator/registry/mod.rs
index 48023a92..e6a0d947 100644
--- a/gl_generator/registry/mod.rs
+++ b/gl_generator/registry/mod.rs
@@ -15,12 +15,10 @@
 extern crate khronos_api;
 
 use std::collections::BTreeSet;
-use std::collections::HashSet;
 use std::collections::HashMap;
 use std::fmt;
 use std::io;
 use std::ops::Add;
-use std::slice::Iter;
 
 use Generator;
 
@@ -133,20 +131,6 @@ impl Registry {
         }
         tys
     }
-
-    pub fn enum_iter<'a>(&'a self) -> EnumIterator<'a> {
-        EnumIterator {
-            seen: HashSet::new(),
-            iter: self.enums.iter(),
-        }
-    }
-
-    pub fn cmd_iter<'a>(&'a self) -> CmdIterator<'a> {
-        CmdIterator {
-            seen: HashSet::new(),
-            iter: self.cmds.iter(),
-        }
-    }
 }
 
 impl Add for Registry {
@@ -159,43 +143,3 @@ impl Add for Registry {
         self
     }
 }
-
-pub struct EnumIterator<'a> {
-    seen: HashSet<String>,
-    iter: Iter<'a, Enum>,
-}
-
-impl<'a> Iterator for EnumIterator<'a> {
-    type Item = &'a Enum;
-
-    fn next(&mut self) -> Option<&'a Enum> {
-        self.iter.next().and_then(|def| {
-            if !self.seen.contains(&def.ident) {
-                self.seen.insert(def.ident.clone());
-                Some(def)
-            } else {
-                self.next()
-            }
-        })
-    }
-}
-
-pub struct CmdIterator<'a> {
-    seen: HashSet<String>,
-    iter: Iter<'a, Cmd>,
-}
-
-impl<'a> Iterator for CmdIterator<'a> {
-    type Item = &'a Cmd;
-
-    fn next(&mut self) -> Option<&'a Cmd> {
-        self.iter.next().and_then(|def| {
-            if !self.seen.contains(&def.proto.ident) {
-                self.seen.insert(def.proto.ident.clone());
-                Some(def)
-            } else {
-                self.next()
-            }
-        })
-    }
-}


Test patch: