3 releases

0.1.2 Jul 25, 2020
0.1.1 Jul 25, 2020
0.1.0 Jul 25, 2020

#2966 in Rust patterns

Download history 680/week @ 2024-03-14 948/week @ 2024-03-21 1412/week @ 2024-03-28 1314/week @ 2024-04-04 929/week @ 2024-04-11 617/week @ 2024-04-18 684/week @ 2024-04-25 1191/week @ 2024-05-02 692/week @ 2024-05-09 488/week @ 2024-05-16 572/week @ 2024-05-23 600/week @ 2024-05-30 679/week @ 2024-06-06 691/week @ 2024-06-13 888/week @ 2024-06-20 481/week @ 2024-06-27

2,828 downloads per month
Used in 4 crates

Zlib OR MIT OR Apache-2.0

8KB

bstringify!

Like stringify!, but yielding byte string literals instead.

Repository Latest version Documentation License CI

Since .as_bytes() on strings is a const fn, this macro should only be useful to create a byte string literal to match against:

use ::bstringify::bstringify;

/// like FromStr but with [u8] instead
trait FromBytes : Sized {
    fn from_bytes (input: &'_ [u8])
      -> Option<Self>
    ;
}

macro_rules! derive_FromBytes {(
    $(#[$attr:meta])*
    $pub:vis
    enum $EnumName:ident {
        $(
            $Variant:ident
        ),* $(,)?
    }
) => (
    $(#[$attr])*
    $pub
    enum $EnumName {
        $(
            $Variant,
        )*
    }
    
    impl $crate::FromBytes
        for $EnumName
    {
        fn from_bytes (input: &'_ [u8])
          -> Option<Self>
        {
            match input {
            $(
                | bstringify!($Variant) => Some(Self::$Variant),
            )*
                | _ => None,
            }
        }
    }
)}

derive_FromBytes! {
    enum Example {
        Foo,
        Bar,
    }
}

fn main ()
{
    assert!(matches!(
        Example::from_bytes(b"Foo"), Some(Example::Foo)
    ));
    assert!(matches!(
        Example::from_bytes(b"Bar"), Some(Example::Bar)
    ));
    assert!(matches!(
        Example::from_bytes(b"Bad"), None
    ));
}

No runtime deps

Features