uiactivity_ios_rs 0.1.1

Start a UIActivity on iOS from Rust code.
Documentation
#![doc = include_str!("../README.md")]

#[cfg(target_os = "ios")]
mod native;

/// Stores the callback to be called when the share activity ends.
static mut END_CALLBACK: Option<Box<dyn FnOnce(Option<String>) + 'static>> = None;

/// Opens a share dialog on iOS with the given text.
pub fn share(_text: &str, _callback: impl FnOnce(Option<String>) + 'static) {
    #[cfg(target_os = "ios")]
    {
        unsafe {
            END_CALLBACK = Some(Box::new(_callback));
        }
        use std::ffi::CString;
        let c_string = CString::new(_text).expect("CString::new failed");
        unsafe {
            let html_ptr = native::share_text(
                c_string.as_ptr() as *const u8,
                _text.len() as u64,
                uiactivity_ios_rs_did_end,
            );
        }
    }
    #[cfg(not(target_os = "ios"))]
    _callback(None);
}

#[unsafe(no_mangle)]
unsafe extern "C" fn uiactivity_ios_rs_did_end(
    activity_type_ptr: *const u8,
    activity_type_len: u64,
) {
    unsafe {
        let ptr = &raw mut END_CALLBACK;
        let old = std::ptr::replace(ptr, None);
        if let Some(callback) = old {
            callback(if activity_type_ptr.is_null() {
                None
            } else {
                let slice =
                    std::slice::from_raw_parts(activity_type_ptr, activity_type_len as usize);
                Some(String::from_utf8_lossy(slice).to_string())
            });
        }
    }
}