MCPcopy Create free account
hub / github.com/astonbitecode/j4rs / invoke_into_sendable_async

Method invoke_into_sendable_async

rust/src/async_api/mod.rs:58–82  ·  view source on GitHub ↗

Invokes the method `method_name` of a created `Instance` asynchronously, passing an array of `InvocationArg`s. It returns an `Instance` as the result of the invocation. `Instance`s are `Send` and can be safely sent to other threads. However, because of [Send Approximation](https://rust-lang.github.io/async-book/07_workarounds/03_send_approximation.html), the `Future` returned by `invoke_async`

(
        instance: Instance,
        method_name: String,
        inv_args: Vec<InvocationArg>,
    )

Source from the content-addressed store, hash-verified

56 ///
57 /// In order to have a `Future<Instance>` that __is__ `Send`, the `Jvm::invoke_into_sendable_async` can be used. This function does not get a `Jvm` as argument; it creates one internally when needed and applies some scoping workarounds in order to achieve returning a `Future<Instance>` which is also `Send`.
58 pub async fn invoke_into_sendable_async(
59 instance: Instance,
60 method_name: String,
61 inv_args: Vec<InvocationArg>,
62 ) -> errors::Result<Instance> {
63 debug(&format!(
64 "Asynchronously invoking (2) method {} of class {} using {} arguments",
65 method_name,
66 instance.class_name,
67 inv_args.len()
68 ));
69 // Create the channel
70 let (sender, rx) = oneshot::channel::<errors::Result<Instance>>();
71 unsafe {
72 let s = Jvm::attach_thread()?;
73 Self::handle_channel_sender(&s, sender, &instance, &method_name, inv_args.as_ref())?;
74 drop(s);
75 }
76
77 // Create and return the Instance
78 let instance = rx.await?;
79 let new_jvm = Jvm::attach_thread()?;
80 let new_jni_env = new_jvm.jni_env;
81 Self::do_return(new_jni_env, instance)?
82 }
83
84 unsafe fn handle_channel_sender(s: &Jvm, sender: oneshot::Sender<errors::Result<Instance>>, instance: &Instance, method_name: &str, inv_args: &[InvocationArg]) -> errors::Result<()> {
85 let tx = Box::new(sender);

Callers

nothing calls this directly

Calls 2

debugFunction · 0.85
do_returnFunction · 0.85

Tested by

no test coverage detected