unofficial_extras/
util.rs1use std::{
2 ffi::{CStr, c_char},
3 slice,
4};
5
6#[inline]
8pub unsafe fn str_from_cstr<'a>(ptr: *const c_char) -> Option<&'a str> {
9 if ptr.is_null() {
10 None
11 } else {
12 unsafe { CStr::from_ptr(ptr) }.to_str().ok()
13 }
14}
15
16#[inline]
20#[allow(dead_code)]
21pub unsafe fn str_from_cstr_len<'a>(ptr: *const c_char, len: u64) -> &'a str {
22 let slice = unsafe { slice::from_raw_parts(ptr as *const u8, len as usize) };
23 str::from_utf8(slice).expect("cstr with invalid utf8")
24}
25
26#[inline]
28pub fn strip_account_prefix(account_name: &str) -> &str {
29 account_name.strip_prefix(':').unwrap_or(account_name)
30}
31
32macro_rules! abi {
34 ( $( $vis:vis type $name:ident = unsafe extern fn( $( $args:tt )* ) $( -> $ret:ty )? ; )* ) => {
35 $(
36 #[cfg(feature = "unwind")]
37 $vis type $name = unsafe extern "C-unwind" fn( $( $args )* ) $( -> $ret )?;
38
39 #[cfg(not(feature = "unwind"))]
40 $vis type $name = unsafe extern "C" fn( $( $args )* ) $( -> $ret )?;
41 )*
42 };
43}
44
45pub(crate) use abi;