浏览代码

Move macros into their own module.

This allows us to use `enum_with_unknown` in `phy`.
whitequark 7 年之前
父节点
当前提交
f5a3785c18
共有 3 个文件被更改,包括 75 次插入74 次删除
  1. 4 21
      src/lib.rs
  2. 71 0
      src/macros.rs
  3. 0 53
      src/wire/mod.rs

+ 4 - 21
src/lib.rs

@@ -84,35 +84,18 @@ extern crate collections;
 #[macro_use(trace, log, log_enabled)]
 extern crate log;
 
-macro_rules! net_trace {
-    ($($arg:expr),*) => {
-        #[cfg(feature = "log")]
-        trace!($($arg),*);
-        #[cfg(not(feature = "log"))]
-        $( let _ = $arg );*; // suppress unused variable warnings
-    }
-}
-
-macro_rules! net_trace_enabled {
-    () => ({
-        #[cfg(feature = "log")]
-        fn enabled() -> bool { log_enabled!($crate::log::LogLevel::Trace) }
-        #[cfg(not(feature = "log"))]
-        fn enabled() -> bool { false }
-        enabled()
-    })
-}
-
 use core::fmt;
 
+#[macro_use]
+mod macros;
+mod parsing;
+
 pub mod storage;
 pub mod phy;
 pub mod wire;
 pub mod iface;
 pub mod socket;
 
-mod parsing;
-
 /// The error type for the networking stack.
 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
 pub enum Error {

+ 71 - 0
src/macros.rs

@@ -0,0 +1,71 @@
+macro_rules! net_trace {
+    ($($arg:expr),*) => {
+        #[cfg(feature = "log")]
+        trace!($($arg),*);
+        #[cfg(not(feature = "log"))]
+        $( let _ = $arg );*; // suppress unused variable warnings
+    }
+}
+
+macro_rules! net_trace_enabled {
+    () => ({
+        #[cfg(feature = "log")]
+        fn enabled() -> bool { log_enabled!($crate::log::LogLevel::Trace) }
+        #[cfg(not(feature = "log"))]
+        fn enabled() -> bool { false }
+        enabled()
+    })
+}
+
+macro_rules! enum_with_unknown {
+    (
+        $( #[$enum_attr:meta] )*
+        pub enum $name:ident($ty:ty) {
+            $( $variant:ident = $value:expr ),+
+        }
+    ) => {
+        enum_with_unknown! {
+            $( #[$enum_attr] )*
+            pub doc enum $name($ty) {
+                $( #[doc(shown)] $variant = $value ),+
+            }
+        }
+    };
+    (
+        $( #[$enum_attr:meta] )*
+        pub doc enum $name:ident($ty:ty) {
+            $(
+              $( #[$variant_attr:meta] )+
+              $variant:ident = $value:expr
+            ),+
+        }
+    ) => {
+        #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
+        $( #[$enum_attr] )*
+        pub enum $name {
+            $(
+              $( #[$variant_attr] )*
+              $variant
+            ),*,
+            Unknown($ty)
+        }
+
+        impl ::core::convert::From<$ty> for $name {
+            fn from(value: $ty) -> Self {
+                match value {
+                    $( $value => $name::$variant ),*,
+                    other => $name::Unknown(other)
+                }
+            }
+        }
+
+        impl ::core::convert::From<$name> for $ty {
+            fn from(value: $name) -> Self {
+                match value {
+                    $( $name::$variant => $value ),*,
+                    $name::Unknown(other) => other
+                }
+            }
+        }
+    }
+}

+ 0 - 53
src/wire/mod.rs

@@ -68,59 +68,6 @@ let mut buffer = vec![0; repr.buffer_len() + repr.payload_len];
 ```
 */
 
-macro_rules! enum_with_unknown {
-    (
-        $( #[$enum_attr:meta] )*
-        pub enum $name:ident($ty:ty) {
-            $( $variant:ident = $value:expr ),+
-        }
-    ) => {
-        enum_with_unknown! {
-            $( #[$enum_attr] )*
-            pub doc enum $name($ty) {
-                $( #[doc(shown)] $variant = $value ),+
-            }
-        }
-    };
-    (
-        $( #[$enum_attr:meta] )*
-        pub doc enum $name:ident($ty:ty) {
-            $(
-              $( #[$variant_attr:meta] )+
-              $variant:ident = $value:expr
-            ),+
-        }
-    ) => {
-        #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
-        $( #[$enum_attr] )*
-        pub enum $name {
-            $(
-              $( #[$variant_attr] )*
-              $variant
-            ),*,
-            Unknown($ty)
-        }
-
-        impl ::core::convert::From<$ty> for $name {
-            fn from(value: $ty) -> Self {
-                match value {
-                    $( $value => $name::$variant ),*,
-                    other => $name::Unknown(other)
-                }
-            }
-        }
-
-        impl ::core::convert::From<$name> for $ty {
-            fn from(value: $name) -> Self {
-                match value {
-                    $( $name::$variant => $value ),*,
-                    $name::Unknown(other) => other
-                }
-            }
-        }
-    }
-}
-
 mod field {
     pub type Field = ::core::ops::Range<usize>;
     pub type Rest  = ::core::ops::RangeFrom<usize>;