« first day (2610 days earlier)      last day (862 days later) » 

2:41 PM
 
 
2 hours later…
4:55 PM
nice
 
 
1 hour later…
6:24 PM
damm I'm terrible at bit operation
 
6:52 PM
that's what she said
 
I will answer: "Ok boomer" ^^
any good crates with a macro that shortcut this:
#[rustfmt::skip]
impl IPProtocol {
  pub const HOPOPT    : Self = Self::new(0);
  pub const ICMP      : Self = Self::new(1);
  pub const IGMP      : Self = Self::new(2);
  pub const GGP       : Self = Self::new(3);
  pub const IPINIP    : Self = Self::new(4);
  pub const ST        : Self = Self::new(5);
  pub const TCP       : Self = Self::new(6);
  pub const CBT       : Self = Self::new(7);
  pub const EGP       : Self = Self::new(8);
  pub const IGP       : Self = Self::new(9);
  pub const BBNRCCMON : Self = Self::new(10);
 
7:11 PM
Why not make your own specific macros of 4 lines ? I do that all the time, it makes it easy to add efficient functions like lookups
 
lookups ?
 
I mean if you need function to find by name or by number, you can use the macro to build the match case without repeating yourself
 
well, if you have this macro under the hand...
I was searching for a crates but why not just copy paste a macro too
 
In broot I also define a Skin struct whose field are defined the same way, very similar to consts
 
macro_rules! xxx {
    ($($name:ident => $val:expr,)*) => {
        $(pub const $name: Self = Self($val);)*
    };
}

struct IPProtocol(u8);

impl IPProtocol {
    xxx!{
        HOPOPT => 0,
        ICMP => 1,
    }
}
(I ignored the ::new, beware)
 
7:22 PM
ah thx
 
@Shepmaster Thanks
 
I guess SafeCloset 0.6 can't be the 1.0
 
7:23 PM
@Shepmaster but I really think my pattern is/should be a lot used, how would you name such thing ?
 
Thanks again!
 
@Stargateur there's so little generic in such macro there's no need to try reuse it. You'll soon discover you want to add a function in the macro anyway
 
@DenysSéguret maybe true
 
@Stargateur Hmm. It's mostly seen for "I am modeling a number outside of my control that may get a new value and I need to represent that value instead of panicking"
otherwise you could use an enum
so I'd probably look for tools around C-like enums
 
7:25 PM
@DenysSéguret well, nothing prevent you to add at hand any function
@Shepmaster yep
let me search
 
I think Denys means that you'll want to use that table to generate the function
 
@Stargateur I mean you'll want to have a function with a match iterating over the values
(what shep said)
 
which you can do from your table of data
 
ahhhh
 
but couldn't as easily with a proc macro, unless the procmacro author already knew what you might want
 
7:27 PM
yes, that's why I showed this example with actions and their keys
 
@DenysSéguret A silly thing I've been trying recently is to have multiple macros, and have one macro call another
 
well my research fail, all I find was for enum
but my point is to use a struct to handle "unknown" variant
cause enum would require 2 octets for nothing
 
@Shepmaster I've see a few things, like when you break the compiler
 
This is just trying to avoid have One Very Big Macro
define the table in one spot, then use the table in different ways
 
 
2 hours later…
9:50 PM
macro_rules! display_variants {
  ($struct_name:ident: $($variant_name:ident,)*) => {
      impl core::fmt::Display for $struct_name {
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
          match self {
            $(&Self::$variant_name => write!(f, "{}", const_format::map_ascii_case!(
              const_format::Case::Pascal,
              core::stringify!($variant_name)
            )),)*
            _ => write!(f, "Unknown: {}", self.get()),
          }
        }
didn't test yet
hope it's work
it's compile
but doesn't it work as I want
 
Might also want to look at docs.rs/paste/latest/paste
for me, it's easier to read how things are being concatenated
 

« first day (2610 days earlier)      last day (862 days later) »