Skip to main content

ferroui_android/interop/
mod.rs

1//! The boundary to the system: the Java Native Interface ([`java`]), the
2//! functions of the NDK ([`ndk`]) and the native methods of the Java layer
3//! ([`natives`]). Everything that is `unsafe` for Java or the NDK is in
4//! these modules (`docs/porting/android-platform.md`, section 2).
5
6pub(crate) mod signature;
7
8#[cfg(target_os = "android")]
9pub mod java;
10#[cfg(target_os = "android")]
11pub mod listeners;
12#[cfg(target_os = "android")]
13pub(crate) mod natives;
14#[cfg(target_os = "android")]
15pub(crate) mod ndk;
16
17/// The text of a panic.
18#[cfg_attr(not(target_os = "android"), allow(dead_code))]
19pub(crate) fn panic_message(panic: &(dyn std::any::Any + Send)) -> String {
20    if let Some(message) = panic.downcast_ref::<&str>() {
21        (*message).to_string()
22    } else if let Some(message) = panic.downcast_ref::<String>() {
23        message.clone()
24    } else {
25        "a panic without a message".to_string()
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    // Not from the reference.
32    use super::*;
33
34    #[test]
35    fn the_message_of_a_panic_is_its_text() {
36        let panic = std::panic::catch_unwind(|| panic!("number {}", 7)).unwrap_err();
37        assert_eq!(panic_message(&*panic), "number 7");
38        let panic = std::panic::catch_unwind(|| panic!("plain")).unwrap_err();
39        assert_eq!(panic_message(&*panic), "plain");
40        let panic = std::panic::catch_unwind(|| std::panic::panic_any(5)).unwrap_err();
41        assert_eq!(panic_message(&*panic), "a panic without a message");
42    }
43}