Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to use references in callbacks? #455

Open
MrKekovichPersonal opened this issue Mar 10, 2024 · 1 comment
Open

Is there a way to use references in callbacks? #455

MrKekovichPersonal opened this issue Mar 10, 2024 · 1 comment
Labels
Java/JNI Case specific only for Java/JNI interface generation question

Comments

@MrKekovichPersonal
Copy link

When I am trying to create callback with pointers, flapigen cannot convert &Type to java:
rust code:

#[generate_interface]
pub trait GetScoreCallback {
    fn get_score(&mut self, network: &NeuralNetwork) -> f64;
}

generated interface:

foreign_callback!(
	callback GetScoreCallback {
		self_type GetScoreCallback;
		getScore = GetScoreCallback::get_score(& mut self , network : & NeuralNetwork)->f64;
	}
);

error:

  parsing of java-api-for-rust: src/generated/java_glue.rs.in failed
  error: Do not know conversion from such rust type '& NeuralNetwork' to Java type
  		getScore = GetScoreCallback::get_score(& mut self , network : & NeuralNetwork)->f64;

However, when I am using references wtih class method - it works:

pub struct Something;
impl Something {
    #[generate_interface]
    pub fn something(net: &NeuralNetwork) {}
}
foreign_class!(
	class Something {
		fn Something::something(net : & NeuralNetwork); alias something;
	}
);

Copying network is a compute-intense process.
Is there any way to solve this problem without copying?

@Dushistov Dushistov added question Java/JNI Case specific only for Java/JNI interface generation labels Apr 8, 2024
@Dushistov
Copy link
Owner

@MrKekovichPersonal

Is there any way to solve this problem without copying?

You can copy, but use something like std::sync::Arc for this to make it cheap.

The problem is that in callback you call Java code from Rust.
So there is need to pass reference to Rust object to Java with restricted lifetime.

But Java has no such thing as reference with restrited lifetime.
So I can no see how to create "safe" way to do it.
For example If flapigen generates Java class like NeuralNetworkReference,
how can anybody prevent situation, when Java side user save object of type NeuralNetworkReference, somewhere and try to reuse it later?

That's why default binding require to pass ownership to Java side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Java/JNI Case specific only for Java/JNI interface generation question
Projects
None yet
Development

No branches or pull requests

2 participants