#stringify #human #no-std #byte-strings #b

macro no-std bstringify

stringify! that yields byte string literals instead

3 releases

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

#5 in #stringify

Download history 1928/week @ 2023-01-19 2147/week @ 2023-01-26 1592/week @ 2023-02-02 2176/week @ 2023-02-09 1174/week @ 2023-02-16 1428/week @ 2023-02-23 1471/week @ 2023-03-02 2169/week @ 2023-03-09 1433/week @ 2023-03-16 599/week @ 2023-03-23 336/week @ 2023-03-30 538/week @ 2023-04-06 378/week @ 2023-04-13 711/week @ 2023-04-20 522/week @ 2023-04-27 334/week @ 2023-05-04

2,035 downloads per month
Used in 2 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

  • nightly