Skip to content

Commit fe9aee0

Browse files
committed
go on bind forever
1 parent 36c897f commit fe9aee0

File tree

13 files changed

+136
-620
lines changed

13 files changed

+136
-620
lines changed

rust/api/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod primitives;
44

55
use class::Instance;
66
use conversion::{FromJValue, IntoJValue};
7+
use jni::JNIEnv;
78
pub use rosttasse_macros::*;
89

910
pub trait JSignature {
@@ -23,6 +24,11 @@ pub trait JSignature {
2324

2425
out
2526
}
27+
28+
#[inline(always)]
29+
fn class<'local>(env: &mut JNIEnv<'local>) -> primitives::JClass {
30+
env.find_class(Self::CLASS).unwrap().into()
31+
}
2632
}
2733

2834
pub trait JavaClass: JSignature + IntoJValue + FromJValue {

rust/macros/src/bind.rs

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::bind::parse::{
1414
BindDefinition, BindField, BindFieldMethod, BindFieldProperty, BindInput, BindPackage,
1515
BindVariant,
1616
};
17-
use crate::java_class::get_attribute;
17+
use crate::utils::get_attribute;
1818

1919
pub fn bind(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
2020
let BindInput { package, defs } = syn::parse_macro_input!(input as BindInput);
@@ -195,7 +195,9 @@ fn bind_def_class(
195195
.map(|prop| {
196196
let name = prop.ident;
197197
let ty = prop.ty;
198-
quote_spanned! {name.span() => pub const #name: ::rosttasse::prelude::Field<#ty> }
198+
quote_spanned! {name.span() =>
199+
pub const #name: ::rosttasse::prelude::StaticField<#class, #ty> = ::rosttasse::prelude::StaticField::new(stringify!(#name));
200+
}
199201
})
200202
.collect_vec();
201203

@@ -226,7 +228,7 @@ fn bind_def_class(
226228
})
227229
.collect::<Vec<_>>();
228230

229-
let instance_impl = crate::java_class::struct_::generate_instance_field_common(
231+
let instance_impl = generate_instance_field_common(
230232
&class,
231233
quote_spanned! {class.span() => self.raw},
232234
quote_spanned! {class.span() => Self {
@@ -419,3 +421,60 @@ fn prepare_method(method: &BindFieldMethod) -> MethodInfo {
419421
ret,
420422
}
421423
}
424+
425+
fn generate_instance_field_common(
426+
struct_name: &Ident,
427+
self_ident: proc_macro2::TokenStream,
428+
from_raw: proc_macro2::TokenStream,
429+
) -> proc_macro2::TokenStream {
430+
quote_spanned! { self_ident.span() =>
431+
impl From<::rosttasse::prelude::Instance> for #struct_name {
432+
fn from(value: ::rosttasse::prelude::Instance) -> Self {
433+
<#struct_name as ::rosttasse::prelude::JavaClass>::from_raw(value)
434+
}
435+
}
436+
437+
impl Into<::rosttasse::prelude::Instance> for #struct_name {
438+
fn into(self) -> ::rosttasse::prelude::Instance {
439+
#self_ident
440+
}
441+
}
442+
443+
impl ::core::ops::Deref for #struct_name {
444+
type Target = ::rosttasse::prelude::Instance;
445+
446+
fn deref(&self) -> &Self::Target {
447+
&#self_ident
448+
}
449+
}
450+
451+
impl ::rosttasse::prelude::IntoJValue for #struct_name {
452+
fn into_jvalue<'local>(self, env: &mut ::jni::JNIEnv<'local>) -> ::jni::objects::JValueOwned<'local> {
453+
#self_ident.into_jvalue(env)
454+
}
455+
}
456+
457+
impl ::rosttasse::prelude::FromJValue for #struct_name {
458+
fn from_jvalue<'local>(value: ::jni::objects::JValueOwned<'local>) -> Self {
459+
<::rosttasse::prelude::Instance as ::rosttasse::prelude::FromJValue>::from_jvalue(value).into()
460+
}
461+
}
462+
463+
impl ::rosttasse::prelude::JavaClass for #struct_name {
464+
fn get_raw(&self) -> ::rosttasse::prelude::Instance {
465+
#self_ident.clone()
466+
}
467+
468+
fn from_raw(raw: ::rosttasse::prelude::Instance) -> Self {
469+
#from_raw
470+
}
471+
}
472+
473+
impl #struct_name {
474+
#[allow(dead_code)]
475+
pub fn cast_unchecked<T: From<::rosttasse::prelude::Instance>>(&self) -> T {
476+
T::from(#self_ident)
477+
}
478+
}
479+
}
480+
}

rust/macros/src/class/mod.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

rust/macros/src/class/parse.rs

Lines changed: 0 additions & 78 deletions
This file was deleted.

rust/macros/src/java_class.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
mod enum_;
2-
mod sig;
1+
// mod enum_;
2+
// mod sig;
33
pub mod struct_;
44

55
use proc_macro2::Span;
66
use quote::ToTokens;
77
use syn::spanned::Spanned;
88
use syn::{Attribute, Error, Expr, ExprLit, Lit, LitStr, Meta, MetaList, MetaNameValue};
99

10-
pub use enum_::main_enum;
11-
pub use struct_::main_struct;
10+
// pub use enum_::main_enum;
11+
// pub use struct_::main_struct;
1212

1313
pub fn get_string_attr(
1414
span: Span,

rust/macros/src/java_class/enum_.rs

Lines changed: 0 additions & 127 deletions
This file was deleted.

rust/macros/src/java_class/sig.rs

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)