From 006d76eea9f262211dfbee24e206eb1794f1212d Mon Sep 17 00:00:00 2001 From: zaaarf Date: Sun, 22 Sep 2024 16:16:42 +0200 Subject: [PATCH] feat: builtin conversions for &str as well --- macro/src/attrs.rs | 1 - macro/src/lib.rs | 1 - src/lib.rs | 15 ++++++++------- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/macro/src/attrs.rs b/macro/src/attrs.rs index f4ff2d2..3872693 100644 --- a/macro/src/attrs.rs +++ b/macro/src/attrs.rs @@ -8,7 +8,6 @@ pub(crate) struct AttrsOptions { } impl AttrsOptions { - pub(crate) fn parse_attr(attrs: TokenStream) -> Result { let mut what_next = WhatNext::Nothing; diff --git a/macro/src/lib.rs b/macro/src/lib.rs index 0d07be7..4843a24 100644 --- a/macro/src/lib.rs +++ b/macro/src/lib.rs @@ -3,7 +3,6 @@ mod wrapper; mod args; mod ret; - /// wrap this function in in a JNI exported fn #[proc_macro_attribute] pub fn jni( diff --git a/src/lib.rs b/src/lib.rs index 8dc63cd..0c9ade2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { type T : Sized; - fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::T) -> Result; } @@ -73,9 +72,6 @@ impl<'j, T: FromJava<'j, T = jni::objects::JObject<'j>>> FromJava<'j> for Option } } - - - pub trait IntoJava<'j> { 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; - fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result { 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.as_str().into_java(env) + } +} + impl<'j> IntoJava<'j> for Vec { type T = jni::sys::jobjectArray; @@ -139,5 +141,4 @@ impl<'j, T: IntoJava<'j, T = jni::sys::jobject>> IntoJava<'j> for Option { None => Ok(std::ptr::null_mut()), } } - }