Skip to content

Commit 4ec7b02

Browse files
committed
chore(java): use prelude imports
1 parent 4b27119 commit 4ec7b02

File tree

5 files changed

+84
-53
lines changed

5 files changed

+84
-53
lines changed

src/ffi/java/buffer.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
use jni::{Env, objects::JObject};
21
use jni_toolbox::jni;
32

43
use crate::{
5-
api::{AsyncReceiver, AsyncSender, BufferUpdate, TextChange},
64
errors::ControllerError,
7-
proto::session::WorkspaceIdentifier
5+
prelude::{
6+
CodempAsyncReceiver as AsyncReceiver, CodempAsyncSender as AsyncSender,
7+
CodempBufferUpdate as BufferUpdate, CodempTextChange as TextChange,
8+
CodempWorkspaceIdentifier as WorkspaceIdentifier,
9+
},
810
};
911

1012
/// Get the name of the buffer.
@@ -51,16 +53,18 @@ fn send(
5153
/// Register a callback for buffer changes.
5254
#[jni(package = "mp.code", class = "BufferController")]
5355
fn callback<'local>(
54-
env: &mut Env<'local>,
56+
env: &mut jni::Env<'local>,
5557
controller: &mut crate::buffer::Controller,
56-
cb: JObject<'local>,
58+
cb: jni::objects::JObject<'local>,
5759
) -> Result<(), jni::errors::Error> {
5860
if cb.is_null() {
59-
return Err(jni::errors::Error::NullPtr("null pointer to buffer callback"));
61+
return Err(jni::errors::Error::NullPtr(
62+
"null pointer to buffer callback",
63+
));
6064
}
6165

6266
let cb_ref = env.new_global_ref(cb)?;
63-
let jvm = env.get_java_vm()?;
67+
let jvm = env.get_java_vm()?;
6468

6569
controller.callback(move |controller: crate::buffer::Controller| {
6670
let result: Result<(), jni::errors::Error> = jvm.attach_current_thread(|env| {

src/ffi/java/client.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
use jni_toolbox::jni;
2+
13
use crate::{
2-
Workspace,
3-
api::{AsyncReceiver, Config},
4-
client::Client,
54
errors::{ConnectionError, ControllerError, RemoteError},
6-
proto::{common::UserInfo, session::{SessionEvent, WorkspaceIdentifier}}
5+
prelude::{
6+
CodempAsyncReceiver as AsyncReceiver, CodempClient as Client, CodempConfig as Config,
7+
CodempSessionEvent as SessionEvent, CodempUserInfo as UserInfo,
8+
CodempWorkspace as Workspace, CodempWorkspaceIdentifier as WorkspaceIdentifier,
9+
},
710
};
8-
use jni_toolbox::jni;
911

1012
/// Connect using the given credentials to the default server, and return a [Client] to interact with it.
1113
#[jni(package = "mp.code", class = "Client")]
@@ -21,7 +23,11 @@ fn current_user(client: &mut Client) -> UserInfo {
2123

2224
/// Join a [Workspace] and return a pointer to it.
2325
#[jni(package = "mp.code", class = "Client")]
24-
fn attach_workspace(client: &mut Client, user: String, workspace: String) -> Result<Workspace, ConnectionError> {
26+
fn attach_workspace(
27+
client: &mut Client,
28+
user: String,
29+
workspace: String,
30+
) -> Result<Workspace, ConnectionError> {
2531
super::tokio().block_on(client.attach_workspace(user, workspace))
2632
}
2733

@@ -57,7 +63,11 @@ fn delete_workspace(client: &mut Client, workspace: String) -> Result<(), Remote
5763

5864
/// Invite another user to an owned workspace.
5965
#[jni(package = "mp.code", class = "Client")]
60-
fn invite_to_workspace(client: &mut Client, workspace: String, user: String) -> Result<(), RemoteError> {
66+
fn invite_to_workspace(
67+
client: &mut Client,
68+
workspace: String,
69+
user: String,
70+
) -> Result<(), RemoteError> {
6171
super::tokio().block_on(client.invite_to_workspace(workspace, user))
6272
}
6373

@@ -117,11 +127,13 @@ fn callback<'local>(
117127
cb: jni::objects::JObject<'local>,
118128
) -> Result<(), jni::errors::Error> {
119129
if cb.is_null() {
120-
return Err(jni::errors::Error::NullPtr("null pointer to buffer callback"));
130+
return Err(jni::errors::Error::NullPtr(
131+
"null pointer to buffer callback",
132+
));
121133
}
122134

123135
let cb_ref = env.new_global_ref(cb)?;
124-
let jvm = env.get_java_vm()?;
136+
let jvm = env.get_java_vm()?;
125137

126138
client.callback(move |controller: Client| {
127139
let result: Result<(), jni::errors::Error> = jvm.attach_current_thread(|env| {

src/ffi/java/cursor.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use jni_toolbox::jni;
2+
13
use crate::{
2-
api::{AsyncReceiver, AsyncSender},
34
errors::ControllerError,
4-
proto::{cursor::{CursorEvent, CursorUpdate}, session::WorkspaceIdentifier}
5+
prelude::{
6+
CodempAsyncReceiver as AsyncReceiver, CodempAsyncSender as AsyncSender,
7+
CodempCursorEvent as CursorEvent, CodempCursorUpdate as CursorUpdate,
8+
CodempWorkspaceIdentifier as WorkspaceIdentifier,
9+
},
510
};
6-
use jni::{Env, objects::JObject};
7-
use jni_toolbox::jni;
811

912
/// Get the [WorkspaceIdentifier] of the workspace that contains this buffer.
1013
#[jni(package = "mp.code", class = "CursorController")]
@@ -14,7 +17,9 @@ fn workspace_id(controller: &mut crate::cursor::Controller) -> WorkspaceIdentifi
1417

1518
/// Try to fetch a [Cursor], or returns null if there's nothing.
1619
#[jni(package = "mp.code", class = "CursorController")]
17-
fn try_recv(controller: &mut crate::cursor::Controller) -> Result<Option<CursorEvent>, ControllerError> {
20+
fn try_recv(
21+
controller: &mut crate::cursor::Controller,
22+
) -> Result<Option<CursorEvent>, ControllerError> {
1823
super::tokio().block_on(controller.try_recv())
1924
}
2025

@@ -26,23 +31,26 @@ fn recv(controller: &mut crate::cursor::Controller) -> Result<CursorEvent, Contr
2631

2732
/// Receive from Java, converts and sends a [Cursor].
2833
#[jni(package = "mp.code", class = "CursorController")]
29-
fn send(controller: &mut crate::cursor::Controller, sel: CursorUpdate) -> Result<(), ControllerError> {
34+
fn send(
35+
controller: &mut crate::cursor::Controller,
36+
sel: CursorUpdate,
37+
) -> Result<(), ControllerError> {
3038
controller.send(sel)
3139
}
3240

3341
/// Register a callback for cursor changes.
3442
#[jni(package = "mp.code", class = "CursorController")]
3543
fn callback<'local>(
36-
env: &mut Env<'local>,
44+
env: &mut jni::Env<'local>,
3745
controller: &mut crate::cursor::Controller,
38-
cb: JObject<'local>,
46+
cb: jni::objects::JObject<'local>,
3947
) -> Result<(), jni::errors::Error> {
4048
if cb.is_null() {
4149
return Err(jni::errors::Error::NullPtr("cursor callback is null"));
4250
}
4351

4452
let cb_ref = env.new_global_ref(cb)?;
45-
let jvm = env.get_java_vm()?;
53+
let jvm = env.get_java_vm()?;
4654

4755
controller.callback(move |controller: crate::cursor::Controller| {
4856
let res: Result<(), jni::errors::Error> = jvm.attach_current_thread(|env| {

src/ffi/java/mod.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ impl jni_toolbox::IntoException for crate::errors::ControllerError {
9090
}
9191
}
9292

93-
94-
macro_rules! from_java_ptr {
95-
($type: ty) => {
93+
/// Generates a [jni_toolbox::IntoJava] and [jni_toolbox::FromJava] implementations
94+
/// for a class that is just a holder for a pointer.
95+
macro_rules! java_ptr_class {
96+
($type: ty, $jclass: literal) => {
9697
impl<'j> jni_toolbox::FromJava<'j> for &mut $type {
9798
type From = jni::sys::jobject;
9899
#[allow(unsafe_code)]
@@ -110,17 +111,7 @@ macro_rules! from_java_ptr {
110111
Self::from_java(env, value.l()?.into_raw())
111112
}
112113
}
113-
};
114-
}
115-
116-
from_java_ptr!(crate::Client);
117-
from_java_ptr!(crate::Workspace);
118-
from_java_ptr!(crate::cursor::Controller);
119-
from_java_ptr!(crate::buffer::Controller);
120114

121-
/// Generates a [JObjectify] implementation for a class that is just a holder for a pointer.
122-
macro_rules! into_java_ptr_class {
123-
($type: ty, $jclass: literal) => {
124115
impl<'j> jni_toolbox::IntoJavaObject<'j> for $type {
125116
const CLASS: &'static str = $jclass;
126117
fn into_java_object(
@@ -140,7 +131,13 @@ macro_rules! into_java_ptr_class {
140131
};
141132
}
142133

143-
into_java_ptr_class!(crate::Client, "mp/code/Client");
144-
into_java_ptr_class!(crate::Workspace, "mp/code/Workspace");
145-
into_java_ptr_class!(crate::cursor::Controller, "mp/code/CursorController");
146-
into_java_ptr_class!(crate::buffer::Controller, "mp/code/BufferController");
134+
java_ptr_class!(crate::prelude::CodempClient, "mp/code/Client");
135+
java_ptr_class!(crate::prelude::CodempWorkspace, "mp/code/Workspace");
136+
java_ptr_class!(
137+
crate::prelude::CodempBufferController,
138+
"mp/code/BufferController"
139+
);
140+
java_ptr_class!(
141+
crate::prelude::CodempCursorController,
142+
"mp/code/CursorController"
143+
);

src/ffi/java/workspace.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
use jni_toolbox::jni;
2+
13
use crate::{
2-
Workspace,
3-
api::controller::AsyncReceiver,
44
errors::{ConnectionError, ControllerError, RemoteError},
5-
proto::{common::UserInfo, session::WorkspaceIdentifier, workspace::WorkspaceEvent}
5+
prelude::{
6+
CodempAsyncReceiver as AsyncReceiver, CodempBufferAttributes as BufferAttributes,
7+
CodempBufferNode as BufferNode, CodempUserInfo as UserInfo, CodempWorkspace as Workspace,
8+
CodempWorkspaceEvent as WorkspaceEvent, CodempWorkspaceIdentifier as WorkspaceIdentifier,
9+
},
610
};
7-
use codemp_proto::files::{BufferAttributes, BufferNode};
8-
use jni::{Env, objects::JObject};
9-
use jni_toolbox::jni;
1011

1112
/// Get the workspace id.
1213
#[jni(package = "mp.code", class = "Workspace")]
@@ -46,7 +47,11 @@ fn user_list(workspace: &mut Workspace) -> Vec<UserInfo> {
4647

4748
/// Create a new buffer.
4849
#[jni(package = "mp.code", class = "Workspace")]
49-
fn create_buffer(workspace: &mut Workspace, path: String, attributes: Option<BufferAttributes>) -> Result<(), RemoteError> {
50+
fn create_buffer(
51+
workspace: &mut Workspace,
52+
path: String,
53+
attributes: Option<BufferAttributes>,
54+
) -> Result<(), RemoteError> {
5055
super::tokio().block_on(workspace.create_buffer(path, attributes))
5156
}
5257

@@ -64,7 +69,10 @@ fn un_pin_buffer(workspace: &mut Workspace, path: String) -> Result<(), RemoteEr
6469

6570
/// Attach to a buffer and return a pointer to its [`crate::buffer::Controller`].
6671
#[jni(package = "mp.code", class = "Workspace")]
67-
fn attach_buffer(workspace: &mut Workspace, path: String) -> Result<crate::buffer::Controller, ConnectionError> {
72+
fn attach_buffer(
73+
workspace: &mut Workspace,
74+
path: String,
75+
) -> Result<crate::buffer::Controller, ConnectionError> {
6876
super::tokio().block_on(workspace.attach_buffer(&path))
6977
}
7078

@@ -131,12 +139,14 @@ fn clear_callback(workspace: &mut Workspace) {
131139
/// Register a callback for workspace events.
132140
#[jni(package = "mp.code", class = "Workspace")]
133141
fn callback<'local>(
134-
env: &mut Env<'local>,
142+
env: &mut jni::Env<'local>,
135143
controller: &mut crate::Workspace,
136-
cb: JObject<'local>,
144+
cb: jni::objects::JObject<'local>,
137145
) -> Result<(), jni::errors::Error> {
138146
if cb.is_null() {
139-
return Err(jni::errors::Error::NullPtr("null pointer to workspace callback"));
147+
return Err(jni::errors::Error::NullPtr(
148+
"null pointer to workspace callback",
149+
));
140150
}
141151

142152
let cb_ref = env.new_global_ref(cb)?;

0 commit comments

Comments
 (0)