3 releases

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

#2896 in Rust patterns

Download history 1562/week @ 2024-11-16 1269/week @ 2024-11-23 703/week @ 2024-11-30 1289/week @ 2024-12-07 1386/week @ 2024-12-14 635/week @ 2024-12-21 370/week @ 2024-12-28 767/week @ 2025-01-04 1664/week @ 2025-01-11 1572/week @ 2025-01-18 1319/week @ 2025-01-25 1771/week @ 2025-02-01 1642/week @ 2025-02-08 1298/week @ 2025-02-15 2061/week @ 2025-02-22 1370/week @ 2025-03-01

6,766 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