Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // So I want a macro that will generate this kind of construction:
- //
- // #[repr(u8)]
- // #[derive(Clone, Copy, Debug)]
- // #[allow(dead_code)]
- // pub enum CommonCommand {
- // SelectFile = 0xa4,
- // GetResponse = 0xc0,
- // }
- //
- // impl Into<u8> for CommonCommand {
- // fn into(&self) -> u8 {
- // (*self) as u8
- // }
- // }
- //
- // from something like this (happy to change the syntax):
- //
- // command_type! {
- // CommonCommand {
- // SelectFile = 0xa4,
- // GetResponse = 0xc0,
- // }
- // }
- macro_rules! command_type {
- ( $name:ident { $($val:expr),+ } ) => {{
- #[repr(u8)]
- #[derive(Clone, Copy, Debug)]
- #[allow(dead_code)]
- pub enum $name {
- $($val),+
- }
- impl Into<u8> for $name {
- fn into(&self) -> u8 {
- (*self) as u8
- }
- }
- }}
- }
- command_type! {
- CommonCommand {
- SelectFile = 0xa4,
- GetResponse = 0xc0,
- }
- }
Add Comment
Please, Sign In to add comment