Macro fruity__bbqsrc::ns_string [−][src]
macro_rules! ns_string { ($s:expr) => { ... }; }
Expand description
Creates an NSString
from a static
string.
Feature Flag
This macro is defined in foundation
,
which requires the foundation
feature flag.
Examples
This macro takes a either a "string"
literal or const
string slice as
the argument:
let hello = fruity::ns_string!("hello"); assert_eq!(hello.to_string(), "hello"); const WORLD: &str = "world"; let world = fruity::ns_string!(WORLD); assert_eq!(world.to_string(), WORLD);
The result of this macro can even be used to create static
values:
static WORLD: &NSString = fruity::ns_string!("world"); assert_eq!(WORLD.to_string(), "world");
Note that the result cannot be used in a const
because it refers to
static data outside of this library.
Unicode Strings
In Objective-C, non-ASCII strings are UTF-16. However, Rust strings are UTF-8.
This macro transcodes non-ASCII strings to UTF-16:
static HELLO_RU: &NSString = fruity::ns_string!("Привет"); assert_eq!(HELLO_RU.to_string(), "Привет");
Note that because this is implemented with const
evaluation, massive
strings can increase compile time and even hit the const
evaluation limit.
Null-Terminated Strings
If the input string already ends with a 0 byte, then this macro does not append one.
let cstr = fruity::ns_string!("example\0"); let normal = fruity::ns_string!("example"); assert_eq!(cstr, normal);
Interior null bytes are allowed and are not stripped:
let example = fruity::ns_string!("exa\0mple");
Runtime Cost
None.
The result is equivalent to @"string"
syntax in Objective-C.
Because of that, this should be preferred over
NSString::from_str
where possible.
Compile-time Cost
Minimal.
This is implemented entirely with const
evaluation. It is not a procedural
macro that requires dependencies for parsing.