#ffi #bpf #cstring #cstr

bystr

macro to create compile-time, fixed length, byte array from a string

4 releases

✓ Uses Rust 2018 edition

0.1.3 Nov 29, 2019
0.1.2 Nov 14, 2019
0.1.1 Nov 9, 2019
0.1.0 Nov 9, 2019

#108 in Unix APIs

Download history 8/week @ 2020-02-28 8/week @ 2020-03-06 20/week @ 2020-03-13 41/week @ 2020-03-20 8/week @ 2020-03-27 1/week @ 2020-04-03 21/week @ 2020-04-10 10/week @ 2020-04-17 1/week @ 2020-05-01 4/week @ 2020-05-08 4/week @ 2020-05-22 4/week @ 2020-05-29 8/week @ 2020-06-05 4/week @ 2020-06-12

51 downloads per month

GPL-3.0-or-later

15KB

bystr

docs.rs/bystr Build Status

bystr is a Rust procedural-macro to turn a static string into an array of bytes at compile time. This allows for easier FFI interaction as well as stack-based "static" strings.

A null byte is appended to the given string when converted to an array.

example

Using the macro is fairly simple:

// import the crate
extern crate bystr;
use bystr::bystr;

fn main() {
    // use it as a function call, get a null-terminated byte array
    let as_bytes = bystr!("this will be a [24; u8]");
    println!("{:?}", as_bytes);

    // you may also define the length of the output array.
    // this allows you to create fixed-length arrays larger than your string
    // in order to match the length expected by receivers.
    //
    // an error will be thrown if len(str) >= len_arg.
    let defined_length = bystr!(10, "hello");
    assert_eq!(10, defined_length.len());
    assert_eq!(defined_length, "hello\0\0\0\0\0".as_bytes()[..]);

    // in addition to raw strings, you may also convert an identifier
    // to a static string:
    let ident_str = bystr!(defined_length);
    assert_eq!(15, ident_str.len());
    assert_eq!(ident_str, "defined_length\0".as_bytes()[..]);
}

Dependencies

~395–730KB
~17K SLoC