feat: builtin conversions for &str as well

This commit is contained in:
zaaarf 2024-09-22 16:16:42 +02:00
parent 78b57d6e58
commit 006d76eea9
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
3 changed files with 8 additions and 9 deletions

View file

@ -8,7 +8,6 @@ pub(crate) struct AttrsOptions {
} }
impl AttrsOptions { impl AttrsOptions {
pub(crate) fn parse_attr(attrs: TokenStream) -> Result<Self, syn::Error> { pub(crate) fn parse_attr(attrs: TokenStream) -> Result<Self, syn::Error> {
let mut what_next = WhatNext::Nothing; let mut what_next = WhatNext::Nothing;

View file

@ -3,7 +3,6 @@ mod wrapper;
mod args; mod args;
mod ret; mod ret;
/// wrap this function in in a JNI exported fn /// wrap this function in in a JNI exported fn
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn jni( pub fn jni(

View file

@ -23,7 +23,6 @@ pub fn from_java_static<'j, T: FromJava<'j>>(env: &mut jni::JNIEnv<'j>, val: T::
pub trait FromJava<'j> : Sized { pub trait FromJava<'j> : Sized {
type T : Sized; type T : Sized;
fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::T) -> Result<Self, jni::errors::Error>; fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::T) -> Result<Self, jni::errors::Error>;
} }
@ -73,9 +72,6 @@ impl<'j, T: FromJava<'j, T = jni::objects::JObject<'j>>> FromJava<'j> for Option
} }
} }
pub trait IntoJava<'j> { pub trait IntoJava<'j> {
type T; type T;
@ -110,14 +106,20 @@ impl<'j> IntoJava<'j> for bool {
} }
} }
impl<'j> IntoJava<'j> for String { impl<'j> IntoJava<'j> for &str {
type T = jni::sys::jstring; type T = jni::sys::jstring;
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> { fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
Ok(env.new_string(self)?.as_raw()) Ok(env.new_string(self)?.as_raw())
} }
} }
impl<'j> IntoJava<'j> for String {
type T = jni::sys::jstring;
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
self.as_str().into_java(env)
}
}
impl<'j> IntoJava<'j> for Vec<String> { impl<'j> IntoJava<'j> for Vec<String> {
type T = jni::sys::jobjectArray; type T = jni::sys::jobjectArray;
@ -139,5 +141,4 @@ impl<'j, T: IntoJava<'j, T = jni::sys::jobject>> IntoJava<'j> for Option<T> {
None => Ok(std::ptr::null_mut()), None => Ok(std::ptr::null_mut()),
} }
} }
} }