java_oxide/
lib.rs

1//! Common glue code between Rust and JNI, used in auto-generated `java-oxide` glue code.
2//!
3//! See also the [Android JNI tips](https://developer.android.com/training/articles/perf-jni) documentation as well as the
4//! [Java Native Interface Specification](https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/jniTOC.html).
5//!
6//! Just like [jni-rs](https://docs.rs/jni/latest/jni/), thread safety of accessing Java objects are not guaranteed, unless
7//! they are thread-safe by themselves.
8
9#![feature(arbitrary_self_types)]
10
11/// public jni-sys reexport.
12pub use ::jni_sys as sys;
13use std::fmt;
14
15mod refs {
16    mod arg;
17    mod global;
18    mod local;
19    mod ref_;
20    mod return_;
21
22    pub use arg::*;
23    pub use global::*;
24    pub use local::*;
25    pub use ref_::*;
26    pub use return_::*;
27}
28
29mod array;
30mod as_arg;
31mod as_jvalue;
32mod env;
33mod id_cache;
34mod jni_type;
35mod string_chars;
36mod vm;
37
38pub use array::*;
39pub use as_arg::*;
40pub use as_jvalue::*;
41pub use env::*;
42pub use id_cache::*;
43pub use jni_type::JniType;
44pub use refs::*;
45pub use string_chars::*;
46pub use vm::*;
47
48/// Error returned on failed `.cast()`.`
49#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
50pub struct CastError;
51
52impl std::error::Error for CastError {}
53impl fmt::Display for CastError {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        f.write_str("Cast failed")
56    }
57}
58
59/// A marker type indicating this is a valid exception type that all exceptions thrown by java should be compatible with
60pub trait ThrowableType: ReferenceType {}
61
62/// You should generally not be interacting with this type directly, but it must be public for codegen.
63#[doc(hidden)]
64#[warn(clippy::missing_safety_doc)]
65pub unsafe trait ReferenceType: JniType + Sized + 'static {}
66
67/// Marker trait indicating `Self` can be assigned to `T`.
68///
69/// # Safety
70///
71/// `T` is a superclass or superinterface of `Self`.
72pub unsafe trait AssignableTo<T: ReferenceType>: ReferenceType {}
73
74/// A type is always assignable to itself.
75unsafe impl<T: ReferenceType> AssignableTo<T> for T {}
76
77/// A trait similar to `Display`.
78pub trait JavaDisplay: ReferenceType {
79    fn fmt(self: &Ref<'_, Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result;
80}
81
82/// A trait similar to `Debug`. Currently it is implemented by `Throwable` in generated bindings.
83pub trait JavaDebug: ReferenceType {
84    fn fmt(self: &Ref<'_, Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result;
85}
86
87/// Represents a Java `null` value.
88#[derive(Copy, Clone, PartialEq, Eq, Debug)]
89pub struct Null;