rb_sys/
symbol.rs

1/// Finds or creates a symbol for the given static string. This macro will
2/// memoize the ID to avoid repeated calls to libruby. You should prefer this
3/// macro over [`rb_intern3`] when the string is known at compile time.
4///
5/// # Safety
6///
7/// This macro is safe under two conditions:
8///   - Ruby VM is initialized and that thus safe to call into libruby
9///   - The first call to this macro will be done inside of a managed Ruby thread (i.e. not a native thread)
10///
11/// # Example
12///
13/// ```no_run
14/// use rb_sys::{symbol::rb_intern, rb_funcall, rb_utf8_str_new};
15///
16/// unsafe {
17///   let reverse_id = rb_intern!("reverse");
18///   let msg = rb_utf8_str_new("nice one".as_ptr() as *mut _, 4);
19///   rb_funcall(msg, reverse_id, 0);
20/// }
21/// ```
22#[macro_export]
23macro_rules! rb_intern {
24    ($s:literal) => {{
25        static mut ID: $crate::ID = 0;
26        if ID == 0 {
27            ID = $crate::rb_intern3($s.as_ptr() as _, $s.len() as _, $crate::rb_utf8_encoding());
28        }
29        ID
30    }};
31}