Skip to content

Commit 0fdc3ed

Browse files
committed
Release 0.19.0
1 parent a6d3325 commit 0fdc3ed

File tree

12 files changed

+53
-25
lines changed

12 files changed

+53
-25
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
**/settings/
88
**/primer/
99
**/.settings
10-
**/.classpath
10+
**/.classpath
11+
*.code-workspace

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# j4rs
22

33
[![crates.io](https://img.shields.io/crates/v/j4rs.svg)](https://crates.io/crates/j4rs)
4-
[![Maven Central](https://img.shields.io/badge/Maven%20Central-0.18.0-blue.svg)](https://central.sonatype.com/artifact/io.github.astonbitecode/j4rs/)
4+
[![Maven Central](https://img.shields.io/badge/Maven%20Central-0.19.0-blue.svg)](https://central.sonatype.com/artifact/io.github.astonbitecode/j4rs/)
55
![Build](https://github.com/astonbitecode/j4rs/actions/workflows/ci-workflow.yml/badge.svg)
66

77
j4rs stands for __'Java for Rust'__ and allows effortless calls to Java code from Rust and vice-versa.
@@ -260,6 +260,14 @@ using an internal one-threaded `ScheduledExecutorService`.
260260

261261
This has apparent performance issues.
262262

263+
#### `invoke_async` and `Send`
264+
265+
`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` is _not_ `Send`, even if it just contains an `Instance`. This is because the `Jvm` is being captured by the `async` call as well and the `Jvm` is __not__ `Send`.
266+
267+
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`.
268+
269+
Discussion [here](https://github.com/astonbitecode/j4rs/issues/103).
270+
263271
### Casting
264272

265273
An `Instance` may be casted to some other Class:
@@ -446,7 +454,7 @@ The jar for `j4rs` is available in the Maven Central. It may be used by adding t
446454
<dependency>
447455
<groupId>io.github.astonbitecode</groupId>
448456
<artifactId>j4rs</artifactId>
449-
<version>0.18.0</version>
457+
<version>0.19.0</version>
450458
<scope>provided</scope>
451459
</dependency>
452460
```

java/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>io.github.astonbitecode</groupId>
55
<artifactId>j4rs</artifactId>
6-
<version>0.19.0-SNAPSHOT</version>
6+
<version>0.19.0</version>
77
<name>j4rs</name>
88
<description>Java for Rust</description>
99
<url>https://github.com/astonbitecode/j4rs</url>

rust/README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# j4rs
22

33
[![crates.io](https://img.shields.io/crates/v/j4rs.svg)](https://crates.io/crates/j4rs)
4-
[![Maven Central](https://img.shields.io/badge/Maven%20Central-0.18.0-blue.svg)](https://central.sonatype.com/artifact/io.github.astonbitecode/j4rs/)
4+
[![Maven Central](https://img.shields.io/badge/Maven%20Central-0.19.0-blue.svg)](https://central.sonatype.com/artifact/io.github.astonbitecode/j4rs/)
55
![Build](https://github.com/astonbitecode/j4rs/actions/workflows/ci-workflow.yml/badge.svg)
66

77
j4rs stands for __'Java for Rust'__ and allows effortless calls to Java code from Rust and vice-versa.
@@ -260,6 +260,14 @@ using an internal one-threaded `ScheduledExecutorService`.
260260

261261
This has apparent performance issues.
262262

263+
#### `invoke_async` and `Send`
264+
265+
`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` is _not_ `Send`, even if it just contains an `Instance`. This is because the `Jvm` is being captured by the `async` call as well and the `Jvm` is __not__ `Send`.
266+
267+
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`.
268+
269+
Discussion [here](https://github.com/astonbitecode/j4rs/issues/103).
270+
263271
### Casting
264272

265273
An `Instance` may be casted to some other Class:
@@ -446,7 +454,7 @@ The jar for `j4rs` is available in the Maven Central. It may be used by adding t
446454
<dependency>
447455
<groupId>io.github.astonbitecode</groupId>
448456
<artifactId>j4rs</artifactId>
449-
<version>0.18.0</version>
457+
<version>0.19.0</version>
450458
<scope>provided</scope>
451459
</dependency>
452460
```
@@ -486,7 +494,7 @@ pub extern fn jni_onload(env: *mut JavaVM, _reserved: jobject) -> jint {
486494
Create an `Activity` and define your native methods normally, as described [here](#java-to-rust-support).
487495

488496
Note:
489-
If you encounter any issues when using j4rs in older Android versions, this may be caused by Java 8 compatibility problems.
497+
If you encounter any issues when using j4rs in older Android versions, this may be caused by Java 8 compatibility problems.
490498
This is why there is a `Java 7` version of `j4rs`:
491499

492500
```xml

rust/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use java_locator;
2727
use sha2::{Digest, Sha256};
2828

2929
// This is the version of the jar that should be used
30-
const VERSION: &'static str = "0.19.0-SNAPSHOT";
30+
const VERSION: &'static str = "0.19.0";
3131
const JAVA_FX_VERSION: &'static str = "21.0.2";
3232

3333
fn main() -> Result<(), J4rsBuildError> {
2.18 MB
Binary file not shown.

rust/src/api/instance.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::convert::TryFrom;
2222
use std::sync::mpsc::{Receiver, Sender};
2323

2424
/// A Java instance
25+
/// Instances contain global Java references and can be sent to other threads
2526
#[derive(Serialize)]
2627
pub struct Instance {
2728
/// The name of the class of this instance
@@ -127,12 +128,13 @@ impl Drop for Instance {
127128
}
128129
}
129130

131+
/// Instances contain global Java references and can be sent to other threads
130132
unsafe impl Send for Instance {}
131133

132134
/// A receiver for Java Instances.
133135
///
134136
/// It keeps a channel Receiver to get callback Instances from the Java world
135-
/// and the address of a Box<Sender<Instance>> Box in the heap. This Box is used by Java to communicate
137+
/// and the address of a `Box<Sender<Instance>>` Box in the heap. This Box is used by Java to communicate
136138
/// asynchronously Instances to Rust.
137139
///
138140
/// On Drop, the InstanceReceiver removes the Box from the heap.

rust/src/api/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,17 +1756,17 @@ impl<'a> JvmBuilder<'a> {
17561756
///
17571757
/// If not, you will get exceptions like the following:
17581758
///
1759-
/// java.lang.NoSuchMethodError: java.net.URLClassLoader.<init>(Ljava/lang/String;[Ljava/net/URL;Ljava/lang/ClassLoader;)V
1760-
// at org.astonbitecode.j4rs.api.deploy.J4rsClassLoader.<init>(J4rsClassLoader.java:22)
1761-
// at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
1762-
// at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
1763-
// at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
1764-
// at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
1765-
// at java.lang.SystemClassLoaderAction.run(ClassLoader.java:2204)
1766-
// at java.lang.SystemClassLoaderAction.run(ClassLoader.java:2188)
1767-
// at java.security.AccessController.doPrivileged(Native Method)
1768-
// at java.lang.ClassLoader.initSystemClassLoader(ClassLoader.java:1449)
1769-
// at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:1429)
1759+
/// > java.lang.NoSuchMethodError: java.net.URLClassLoader.`<init>`(Ljava/lang/String;[Ljava/net/URL;Ljava/lang/ClassLoader;)V
1760+
/// at org.astonbitecode.j4rs.api.deploy.J4rsClassLoader.`<init>`(J4rsClassLoader.java:22)
1761+
/// at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
1762+
/// at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
1763+
/// at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
1764+
/// at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
1765+
/// at java.lang.SystemClassLoaderAction.run(ClassLoader.java:2204)
1766+
/// at java.lang.SystemClassLoaderAction.run(ClassLoader.java:2188)
1767+
/// at java.security.AccessController.doPrivileged(Native Method)
1768+
/// at java.lang.ClassLoader.initSystemClassLoader(ClassLoader.java:1449)
1769+
/// at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:1429)
17701770
pub fn with_default_classloader(&'a mut self) -> &'a mut JvmBuilder {
17711771
self.default_classloader = true;
17721772
self

rust/src/async_api/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ impl Jvm {
4848
Self::do_return(self.jni_env, instance)?
4949
}
5050

51+
/// Invokes the method `method_name` of a created `Instance` asynchronously, passing an array of `InvocationArg`s.
52+
/// It returns an `Instance` as the result of the invocation.
53+
///
54+
///
55+
/// `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` is _not_ `Send`, even if it just contains an `Instance`. This is because the `Jvm` is being captured by the `async` call as well and the `Jvm` is __not__ `Send`.
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`.
5158
pub async fn invoke_into_sendable_async(
5259
instance: Instance,
5360
method_name: String,

rust/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#![doc = include_str!("../README.md")]
16+
1517
#[macro_use]
1618
extern crate lazy_static;
1719
extern crate libc;

0 commit comments

Comments
 (0)