Skip to content

Commit

Permalink
sl-std: Add utility ascii! macro
Browse files Browse the repository at this point in the history
This macro is used to construct ascii strings with ease
  • Loading branch information
simonwuelker committed Jul 23, 2024
1 parent 960d23f commit 8ba6ccb
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions crates/sl-std/src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,30 @@ impl AsciiCharExt for Char {
}
}
}

/// Allows for easy definition of ascii-strings
///
/// Since rust does not allow us to define our own string literals, the use of a macro
/// is necessary. If the provided literal contains non-ascii data then the invocation will
/// panic at compile-time.
///
/// # Example
/// ```
/// # #![feature(ascii_char)]
/// # use sl_std::ascii;
///
/// let foo: &ascii::Str = ascii!("foo");
/// assert_eq!(foo.as_str(), "foo");
///
/// ```
#[macro_export]
macro_rules! ascii {
($s: expr) => {
const {
match $s.as_bytes().as_ascii() {
Some(ascii) => $crate::ascii::Str::from_ascii_chars(ascii),
None => panic!("string literal is not ascii"),
}
}
};
}

0 comments on commit 8ba6ccb

Please sign in to comment.