Module const_

Source
Expand description

const-friendly equivalents of the top-level crates.

const-friendly?

The default / naïve implementation of the macros of this crate (ab)used the proc-macro capabilities to inspect the contents / values of a given (byte) string literal. But besides that capability, (procedural) macros can’t do any other form of semantic evaluation.

This means that when fed a constant expression evaluating to a valid (byte) string literal,

  • such as a const:

    #[macro_use]
    extern crate byte_strings;
    
    fn main ()
    {
        const FOO: &str = "foo";
    
        // FAILS with something along the lines of "expected a literal"
        const FOO_BYTES: &[u8] = as_bytes!(FOO);
    }
  • or some macro (besides concat!, stringify!, which are syntactically detected and thus get to feature ad-hoc polyfilled support):

    #[macro_use]
    extern crate byte_strings;
    
    fn main ()
    {
        // FAILS with something along the lines of "expected a literal"
        let _ = as_bytes!(third_party_lib::their_macro!(...));
    }

The macros of this module have been written to use const fns as much as possible, so as to use the semantic evaluation built within the compiler rather than the limited syntactical evaluation of classic macros. This allows the macros in this module to be able to support any kind of const-evaluatable (byte) string:

use ::byte_strings::const_::c_str;
use ::core::ffi::CStr;

const MESSAGE: &str = "Hello, World!";
const C_MESSAGE: &CStr = c_str!(MESSAGE); // OK!

Macros§

as_bytes
const-friendly version of as_bytes!.
c_str
const-friendly version of c_str!.
concat
const-friendly version of concat!.
concat_bytes
const-friendly version of concat_bytes!.