Skip to content

Commit

Permalink
Migrate to the 2018 edition (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-tkach authored and dmitry-timofeev committed Aug 6, 2019
1 parent 6bc56b7 commit 8a859ca
Show file tree
Hide file tree
Showing 53 changed files with 196 additions and 265 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ name = "jni"
repository = "https://github.com/jni-rs/jni-rs"
# ¡When bumping version please also update it in examples and documentation!
version = "0.13.0"
edition = "2018"

[dependencies]
cesu8 = "1.1.0"
Expand Down
4 changes: 1 addition & 3 deletions benches/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

extern crate test;

extern crate jni;
#[macro_use]
extern crate lazy_static;
use lazy_static::lazy_static;

use jni::{
descriptors::Desc,
Expand Down
2 changes: 0 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
//! On Windows, we also need to find `jvm.lib` file which is used while linking
//! at build time. This file is typically placed in `$JAVA_HOME/lib` directory.

extern crate walkdir;

use std::{
env,
path::{Path, PathBuf},
Expand Down
1 change: 1 addition & 0 deletions example/mylib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "mylib"
version = "0.1.0"
authors = ["Josh Chase <josh@prevoty.com>"]
edition = "2018"

[dependencies]
jni = { path = "../../" }
Expand Down
86 changes: 46 additions & 40 deletions example/mylib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate jni;

// This is the interface to the JVM that we'll
// call the majority of our methods on.
use jni::JNIEnv;
Expand All @@ -12,44 +10,47 @@ use jni::objects::{GlobalRef, JClass, JObject, JString};
// This is just a pointer. We'll be returning it from our function.
// We can't return one of the objects with lifetime information because the
// lifetime checker won't let us.
use jni::sys::{jint, jlong, jstring, jbyteArray};
use jni::sys::{jbyteArray, jint, jlong, jstring};

use std::thread;
use std::time::Duration;
use std::sync::mpsc;
use std::{sync::mpsc, thread, time::Duration};

// This keeps rust from "mangling" the name and making it unique for this crate.
#[no_mangle]
// This turns off linter warnings because
// the name doesn't conform to conventions.
#[allow(non_snake_case)]
pub extern "system" fn Java_HelloWorld_hello(env: JNIEnv,
// this is the class that owns our
// static method. Not going to be
// used, but still needs to have
// an argument slot
_class: JClass,
input: JString)
-> jstring {
pub extern "system" fn Java_HelloWorld_hello(
env: JNIEnv,
// this is the class that owns our
// static method. Not going to be
// used, but still needs to have
// an argument slot
_class: JClass,
input: JString,
) -> jstring {
// First, we have to get the string out of java. Check out the `strings`
// module for more info on how this works.
let input: String =
env.get_string(input).expect("Couldn't get java string!").into();
let input: String = env
.get_string(input)
.expect("Couldn't get java string!")
.into();

// Then we have to create a new java string to return. Again, more info
// in the `strings` module.
let output = env.new_string(format!("Hello, {}!", input))
let output = env
.new_string(format!("Hello, {}!", input))
.expect("Couldn't create java string!");
// Finally, extract the raw pointer to return.
output.into_inner()
}

#[no_mangle]
#[allow(non_snake_case)]
pub extern "system" fn Java_HelloWorld_helloByte(env: JNIEnv,
_class: JClass,
input: jbyteArray)
-> jbyteArray {
pub extern "system" fn Java_HelloWorld_helloByte(
env: JNIEnv,
_class: JClass,
input: jbyteArray,
) -> jbyteArray {
// First, we have to get the byte[] out of java.
let _input = env.convert_byte_array(input).unwrap();

Expand All @@ -62,14 +63,17 @@ pub extern "system" fn Java_HelloWorld_helloByte(env: JNIEnv,

#[no_mangle]
#[allow(non_snake_case)]
pub extern "system" fn Java_HelloWorld_factAndCallMeBack(env: JNIEnv,
_class: JClass,
n: jint,
callback: JObject) {
pub extern "system" fn Java_HelloWorld_factAndCallMeBack(
env: JNIEnv,
_class: JClass,
n: jint,
callback: JObject,
) {
let i = n as i32;
let res: jint = (2..i + 1).product();

env.call_method(callback, "factCallback", "(I)V", &[res.into()]).unwrap();
env.call_method(callback, "factCallback", "(I)V", &[res.into()])
.unwrap();
}

struct Counter {
Expand All @@ -87,21 +91,23 @@ impl Counter {

pub fn increment(&mut self, env: JNIEnv) {
self.count = self.count + 1;
env.call_method(self.callback.as_obj(),
"counterCallback",
"(I)V",
&[self.count.into()])
.unwrap();
env.call_method(
self.callback.as_obj(),
"counterCallback",
"(I)V",
&[self.count.into()],
)
.unwrap();
}
}


#[no_mangle]
#[allow(non_snake_case)]
pub unsafe extern "system" fn Java_HelloWorld_counterNew(env: JNIEnv,
_class: JClass,
callback: JObject)
-> jlong {
pub unsafe extern "system" fn Java_HelloWorld_counterNew(
env: JNIEnv,
_class: JClass,
callback: JObject,
) -> jlong {
let global_ref = env.new_global_ref(callback).unwrap();
let counter = Counter::new(global_ref);

Expand All @@ -113,8 +119,8 @@ pub unsafe extern "system" fn Java_HelloWorld_counterNew(env: JNIEnv,
pub unsafe extern "system" fn Java_HelloWorld_counterIncrement(
env: JNIEnv,
_class: JClass,
counter_ptr: jlong
){
counter_ptr: jlong,
) {
let counter = &mut *(counter_ptr as *mut Counter);

counter.increment(env);
Expand All @@ -125,8 +131,8 @@ pub unsafe extern "system" fn Java_HelloWorld_counterIncrement(
pub unsafe extern "system" fn Java_HelloWorld_counterDestroy(
_env: JNIEnv,
_class: JClass,
counter_ptr: jlong
){
counter_ptr: jlong,
) {
let _boxed_counter = Box::from_raw(counter_ptr as *mut Counter);
}

Expand Down
14 changes: 0 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@
//! your crate's `src/lib.rs`:
//!
//! ```rust,ignore
//! extern crate jni;
//!
//! // This is the interface to the JVM that we'll call the majority of our
//! // methods on.
//! use jni::JNIEnv;
Expand Down Expand Up @@ -192,21 +190,9 @@
//! [graalvm-rust]: http://www.graalvm.org/docs/reference-manual/languages/llvm/#running-rust
//! [projects-panama]: https://jdk.java.net/panama/

/// Bindgen-generated definitions. Mirrors `jni.h` and `jni_md.h`.
extern crate jni_sys;
/// `jni-sys` re-exports
pub mod sys;

#[macro_use]
extern crate log;

#[macro_use]
extern crate error_chain;

extern crate combine;

extern crate cesu8;

mod wrapper {
mod version;
pub use self::version::*;
Expand Down
16 changes: 7 additions & 9 deletions src/wrapper/descriptors/class_desc.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use strings::JNIString;

use objects::{AutoLocal, GlobalRef, JClass, JObject};

use descriptors::Desc;

use JNIEnv;

use errors::*;
use crate::{
descriptors::Desc,
errors::*,
objects::{AutoLocal, GlobalRef, JClass, JObject},
strings::JNIString,
JNIEnv,
};

impl<'a, T> Desc<'a, JClass<'a>> for T
where
Expand Down
5 changes: 2 additions & 3 deletions src/wrapper/descriptors/desc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use errors::*;
use JNIEnv;
use crate::{errors::*, JNIEnv};

/// Trait for things that can be looked up through the JNI via a descriptor.
/// This will be something like the fully-qualified class name
Expand All @@ -8,7 +7,7 @@ use JNIEnv;
/// concrete types themselves in addition to their descriptors.
pub trait Desc<'a, T> {
/// Look up the concrete type from the JVM.
fn lookup(self, &JNIEnv<'a>) -> Result<T>;
fn lookup(self, _: &JNIEnv<'a>) -> Result<T>;
}

impl<'a, T> Desc<'a, T> for T {
Expand Down
16 changes: 7 additions & 9 deletions src/wrapper/descriptors/exception_desc.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use errors::*;

use descriptors::Desc;

use objects::{JClass, JObject, JThrowable, JValue};

use strings::JNIString;

use JNIEnv;
use crate::{
descriptors::Desc,
errors::*,
objects::{JClass, JObject, JThrowable, JValue},
strings::JNIString,
JNIEnv,
};

const DEFAULT_EXCEPTION_CLASS: &str = "java/lang/RuntimeException";

Expand Down
18 changes: 7 additions & 11 deletions src/wrapper/descriptors/field_desc.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use errors::*;

use descriptors::Desc;

use objects::JClass;
use objects::JFieldID;
use objects::JStaticFieldID;

use strings::JNIString;

use JNIEnv;
use crate::{
descriptors::Desc,
errors::*,
objects::{JClass, JFieldID, JStaticFieldID},
strings::JNIString,
JNIEnv,
};

impl<'a, 'c, T, U, V> Desc<'a, JFieldID<'a>> for (T, U, V)
where
Expand Down
18 changes: 7 additions & 11 deletions src/wrapper/descriptors/method_desc.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use errors::*;

use descriptors::Desc;

use objects::JClass;
use objects::JMethodID;
use objects::JStaticMethodID;

use strings::JNIString;

use JNIEnv;
use crate::{
descriptors::Desc,
errors::*,
objects::{JClass, JMethodID, JStaticMethodID},
strings::JNIString,
JNIEnv,
};

impl<'a, 'c, T, U, V> Desc<'a, JMethodID<'a>> for (T, U, V)
where
Expand Down
4 changes: 3 additions & 1 deletion src/wrapper/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![allow(missing_docs)]

use sys;
use error_chain::*;

use crate::sys;

error_chain! {
foreign_links {
Expand Down
4 changes: 2 additions & 2 deletions src/wrapper/executor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {errors::*, objects::JObject, JNIEnv, JavaVM};

use std::sync::Arc;

use crate::{errors::*, objects::JObject, JNIEnv, JavaVM};

/// The capacity of local frames, allocated for attached threads by default. Same as the default
/// value Hotspot uses when calling native Java methods.
pub const DEFAULT_LOCAL_FRAME_CAPACITY: i32 = 32;
Expand Down
11 changes: 6 additions & 5 deletions src/wrapper/java_vm/init_args.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::os::raw::c_void;
use std::{ffi::CString, os::raw::c_void};

use std::ffi::CString;
use error_chain::*;

use sys::{JavaVMInitArgs, JavaVMOption};

use JNIVersion;
use crate::{
sys::{JavaVMInitArgs, JavaVMOption},
JNIVersion,
};

error_chain! {
errors {
Expand Down
19 changes: 10 additions & 9 deletions src/wrapper/java_vm/vm.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use errors::*;
use JNIEnv;
use std::{
cell::RefCell,
ops::Deref,
ptr,
sync::atomic::{AtomicUsize, Ordering},
thread::current,
};

use sys;
use log::{debug, error};

use std::cell::RefCell;
use std::ops::Deref;
use std::ptr;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread::current;
use crate::{errors::*, sys, JNIEnv};

#[cfg(feature = "invocation")]
use InitArgs;
use crate::InitArgs;

/// The Java VM, providing [Invocation API][invocation-api] support.
///
Expand Down
Loading

0 comments on commit 8a859ca

Please sign in to comment.