fix: &mut object references

it just works?? maybe it did all the time...
This commit is contained in:
əlemi 2024-09-24 03:35:42 +02:00
parent 678f8bd9ea
commit 0095511cc7
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 41 additions and 42 deletions

View file

@ -7,37 +7,37 @@ use super::null_check;
/// Get the name of the buffer. /// Get the name of the buffer.
#[jni(package = "mp.code", class = "BufferController")] #[jni(package = "mp.code", class = "BufferController")]
fn get_name(controller: crate::buffer::Controller) -> String { fn get_name(controller: &mut crate::buffer::Controller) -> String {
controller.path().to_string() //TODO: &str is built into the newer version controller.path().to_string() //TODO: &str is built into the newer version
} }
/// Get the contents of the buffers. /// Get the contents of the buffers.
#[jni(package = "mp.code", class = "BufferController")] #[jni(package = "mp.code", class = "BufferController")]
fn get_content(controller: crate::buffer::Controller) -> Result<String, ControllerError> { fn get_content(controller: &mut crate::buffer::Controller) -> Result<String, ControllerError> {
super::tokio().block_on(controller.content()) super::tokio().block_on(controller.content())
} }
/// Try to fetch a [TextChange], or return null if there's nothing. /// Try to fetch a [TextChange], or return null if there's nothing.
#[jni(package = "mp.code", class = "BufferController")] #[jni(package = "mp.code", class = "BufferController")]
fn try_recv(controller: crate::buffer::Controller) -> Result<Option<TextChange>, ControllerError> { fn try_recv(controller: &mut crate::buffer::Controller) -> Result<Option<TextChange>, ControllerError> {
super::tokio().block_on(controller.try_recv()) super::tokio().block_on(controller.try_recv())
} }
/// Block until it receives a [TextChange]. /// Block until it receives a [TextChange].
#[jni(package = "mp.code", class = "BufferController")] #[jni(package = "mp.code", class = "BufferController")]
fn recv(controller: crate::buffer::Controller) -> Result<TextChange, ControllerError> { fn recv(controller: &mut crate::buffer::Controller) -> Result<TextChange, ControllerError> {
super::tokio().block_on(controller.recv()) super::tokio().block_on(controller.recv())
} }
/// Send a [TextChange] to the server. /// Send a [TextChange] to the server.
#[jni(package = "mp.code", class = "BufferController")] #[jni(package = "mp.code", class = "BufferController")]
fn send(controller: crate::buffer::Controller, change: TextChange) -> Result<(), ControllerError> { fn send(controller: &mut crate::buffer::Controller, change: TextChange) -> Result<(), ControllerError> {
super::tokio().block_on(controller.send(change)) super::tokio().block_on(controller.send(change))
} }
/// Register a callback for buffer changes. /// Register a callback for buffer changes.
#[jni(package = "mp.code", class = "BufferController")] #[jni(package = "mp.code", class = "BufferController")]
fn callback<'local>(env: &mut JNIEnv<'local>, controller: crate::buffer::Controller, cb: JObject<'local>) { fn callback<'local>(env: &mut JNIEnv<'local>, controller: &mut crate::buffer::Controller, cb: JObject<'local>) {
null_check!(env, cb, {}); null_check!(env, cb, {});
let Ok(cb_ref) = env.new_global_ref(cb) else { let Ok(cb_ref) = env.new_global_ref(cb) else {
env.throw_new("mp/code/exceptions/JNIException", "Failed to pin callback reference!") env.throw_new("mp/code/exceptions/JNIException", "Failed to pin callback reference!")
@ -70,19 +70,19 @@ fn callback<'local>(env: &mut JNIEnv<'local>, controller: crate::buffer::Control
/// Clear the callback for buffer changes. /// Clear the callback for buffer changes.
#[jni(package = "mp.code", class = "BufferController")] #[jni(package = "mp.code", class = "BufferController")]
fn clear_callback(controller: crate::buffer::Controller) { fn clear_callback(controller: &mut crate::buffer::Controller) {
controller.clear_callback() controller.clear_callback()
} }
/// Block until there is a new value available. /// Block until there is a new value available.
#[jni(package = "mp.code", class = "BufferController")] #[jni(package = "mp.code", class = "BufferController")]
fn poll(controller: crate::buffer::Controller) -> Result<(), ControllerError> { fn poll(controller: &mut crate::buffer::Controller) -> Result<(), ControllerError> {
super::tokio().block_on(controller.poll()) super::tokio().block_on(controller.poll())
} }
/// Stop the controller. /// Stop the controller.
#[jni(package = "mp.code", class = "BufferController")] #[jni(package = "mp.code", class = "BufferController")]
fn stop(controller: crate::buffer::Controller) -> bool { fn stop(controller: &mut crate::buffer::Controller) -> bool {
controller.stop() controller.stop()
} }

View file

@ -9,61 +9,61 @@ fn connect(config: Config) -> Result<Client, ConnectionError> {
/// Gets the current [crate::api::User]. /// Gets the current [crate::api::User].
#[jni(package = "mp.code", class = "Client", ptr)] #[jni(package = "mp.code", class = "Client", ptr)]
fn get_user(client: Client) -> crate::api::User { fn get_user(client: &mut Client) -> crate::api::User {
client.user().clone() client.user().clone()
} }
/// Join a [Workspace] and return a pointer to it. /// Join a [Workspace] and return a pointer to it.
#[jni(package = "mp.code", class = "Client")] #[jni(package = "mp.code", class = "Client")]
fn join_workspace(client: Client, workspace: String) -> Result<Workspace, ConnectionError> { fn join_workspace(client: &mut Client, workspace: String) -> Result<Workspace, ConnectionError> {
super::tokio().block_on(client.join_workspace(workspace)) super::tokio().block_on(client.join_workspace(workspace))
} }
/// Create a workspace on server, if allowed to. /// Create a workspace on server, if allowed to.
#[jni(package = "mp.code", class = "Client")] #[jni(package = "mp.code", class = "Client")]
fn create_workspace(client: Client, workspace: String) -> Result<(), RemoteError> { fn create_workspace(client: &mut Client, workspace: String) -> Result<(), RemoteError> {
super::tokio().block_on(client.create_workspace(workspace)) super::tokio().block_on(client.create_workspace(workspace))
} }
/// Delete a workspace on server, if allowed to. /// Delete a workspace on server, if allowed to.
#[jni(package = "mp.code", class = "Client")] #[jni(package = "mp.code", class = "Client")]
fn delete_workspace(client: Client, workspace: String) -> Result<(), RemoteError> { fn delete_workspace(client: &mut Client, workspace: String) -> Result<(), RemoteError> {
super::tokio().block_on(client.delete_workspace(workspace)) super::tokio().block_on(client.delete_workspace(workspace))
} }
/// Invite another user to an owned workspace. /// Invite another user to an owned workspace.
#[jni(package = "mp.code", class = "Client")] #[jni(package = "mp.code", class = "Client")]
fn invite_to_workspace(client: Client, workspace: String, user: String) -> Result<(), RemoteError> { fn invite_to_workspace(client: &mut Client, workspace: String, user: String) -> Result<(), RemoteError> {
super::tokio().block_on(client.invite_to_workspace(workspace, user)) super::tokio().block_on(client.invite_to_workspace(workspace, user))
} }
/// List available workspaces. /// List available workspaces.
#[jni(package = "mp.code", class = "Client")] #[jni(package = "mp.code", class = "Client")]
fn list_workspaces(client: Client, owned: bool, invited: bool) -> Result<Vec<String>, RemoteError> { fn list_workspaces(client: &mut Client, owned: bool, invited: bool) -> Result<Vec<String>, RemoteError> {
super::tokio().block_on(client.list_workspaces(owned, invited)) super::tokio().block_on(client.list_workspaces(owned, invited))
} }
/// List available workspaces. /// List available workspaces.
#[jni(package = "mp.code", class = "Client")] #[jni(package = "mp.code", class = "Client")]
fn active_workspaces(client: Client) -> Vec<String> { fn active_workspaces(client: &mut Client) -> Vec<String> {
client.active_workspaces() client.active_workspaces()
} }
/// Leave a [Workspace] and return whether or not the client was in such workspace. /// Leave a [Workspace] and return whether or not the client was in such workspace.
#[jni(package = "mp.code", class = "Client")] #[jni(package = "mp.code", class = "Client")]
fn leave_workspace(client: Client, workspace: String) -> bool { fn leave_workspace(client: &mut Client, workspace: String) -> bool {
client.leave_workspace(&workspace) client.leave_workspace(&workspace)
} }
/// Get a [Workspace] by name and returns a pointer to it. /// Get a [Workspace] by name and returns a pointer to it.
#[jni(package = "mp.code", class = "Client")] #[jni(package = "mp.code", class = "Client")]
fn get_workspace(client: Client, workspace: String) -> Option<Workspace> { fn get_workspace(client: &mut Client, workspace: String) -> Option<Workspace> {
client.get_workspace(&workspace) client.get_workspace(&workspace)
} }
/// Refresh the client's session token. /// Refresh the client's session token.
#[jni(package = "mp.code", class = "Client")] #[jni(package = "mp.code", class = "Client")]
fn refresh(client: Client) -> Result<(), RemoteError> { fn refresh(client: &mut Client) -> Result<(), RemoteError> {
super::tokio().block_on(client.refresh()) super::tokio().block_on(client.refresh())
} }

View file

@ -6,25 +6,25 @@ use super::null_check;
/// Try to fetch a [Cursor], or returns null if there's nothing. /// Try to fetch a [Cursor], or returns null if there's nothing.
#[jni(package = "mp.code", class = "CursorController")] #[jni(package = "mp.code", class = "CursorController")]
fn try_recv(controller: crate::cursor::Controller) -> Result<Option<Cursor>, ControllerError> { fn try_recv(controller: &mut crate::cursor::Controller) -> Result<Option<Cursor>, ControllerError> {
super::tokio().block_on(controller.try_recv()) super::tokio().block_on(controller.try_recv())
} }
/// Block until it receives a [Cursor]. /// Block until it receives a [Cursor].
#[jni(package = "mp.code", class = "CursorController")] #[jni(package = "mp.code", class = "CursorController")]
fn recv(controller: crate::cursor::Controller) -> Result<Cursor, ControllerError> { fn recv(controller: &mut crate::cursor::Controller) -> Result<Cursor, ControllerError> {
super::tokio().block_on(controller.recv()) super::tokio().block_on(controller.recv())
} }
/// Receive from Java, converts and sends a [Cursor]. /// Receive from Java, converts and sends a [Cursor].
#[jni(package = "mp.code", class = "CursorController")] #[jni(package = "mp.code", class = "CursorController")]
fn send(controller: crate::cursor::Controller, cursor: Cursor) -> Result<(), ControllerError> { fn send(controller: &mut crate::cursor::Controller, cursor: Cursor) -> Result<(), ControllerError> {
super::tokio().block_on(controller.send(cursor)) super::tokio().block_on(controller.send(cursor))
} }
/// Register a callback for cursor changes. /// Register a callback for cursor changes.
#[jni(package = "mp.code", class = "CursorController")] #[jni(package = "mp.code", class = "CursorController")]
fn callback<'local>(env: &mut JNIEnv<'local>, controller: crate::cursor::Controller, cb: JObject<'local>) { fn callback<'local>(env: &mut JNIEnv<'local>, controller: &mut crate::cursor::Controller, cb: JObject<'local>) {
null_check!(env, cb, {}); null_check!(env, cb, {});
let Ok(cb_ref) = env.new_global_ref(cb) else { let Ok(cb_ref) = env.new_global_ref(cb) else {
env.throw_new("mp/code/exceptions/JNIException", "Failed to pin callback reference!") env.throw_new("mp/code/exceptions/JNIException", "Failed to pin callback reference!")
@ -57,19 +57,19 @@ fn callback<'local>(env: &mut JNIEnv<'local>, controller: crate::cursor::Control
/// Clear the callback for cursor changes. /// Clear the callback for cursor changes.
#[jni(package = "mp.code", class = "CursorController")] #[jni(package = "mp.code", class = "CursorController")]
fn clear_callback(controller: crate::cursor::Controller) { fn clear_callback(controller: &mut crate::cursor::Controller) {
controller.clear_callback() controller.clear_callback()
} }
/// Block until there is a new value available. /// Block until there is a new value available.
#[jni(package = "mp.code", class = "CursorController")] #[jni(package = "mp.code", class = "CursorController")]
fn poll(controller: crate::cursor::Controller) -> Result<(), ControllerError> { fn poll(controller: &mut crate::cursor::Controller) -> Result<(), ControllerError> {
super::tokio().block_on(controller.poll()) super::tokio().block_on(controller.poll())
} }
/// Stop the controller. /// Stop the controller.
#[jni(package = "mp.code", class = "CursorController")] #[jni(package = "mp.code", class = "CursorController")]
fn stop(controller: crate::cursor::Controller) -> bool { fn stop(controller: &mut crate::cursor::Controller) -> bool {
controller.stop() controller.stop()
} }

View file

@ -244,11 +244,10 @@ impl<'j> jni_toolbox::IntoJavaObject<'j> for crate::api::Cursor {
macro_rules! from_java_ptr { macro_rules! from_java_ptr {
($type: ty) => { ($type: ty) => {
impl<'j> jni_toolbox::FromJava<'j> for $type { impl<'j> jni_toolbox::FromJava<'j> for &mut $type {
type From = jni::sys::jobject; type From = jni::sys::jobject;
fn from_java(_env: &mut jni::JNIEnv<'j>, value: Self::From) -> Result<Self, jni::errors::Error> { fn from_java(_env: &mut jni::JNIEnv<'j>, value: Self::From) -> Result<Self, jni::errors::Error> {
let original_ptr = unsafe { Box::leak(Box::from_raw(value as *mut $type)) }; Ok(unsafe { Box::leak(Box::from_raw(value as *mut $type)) })
Ok(original_ptr.clone())
} }
} }
}; };

View file

@ -3,79 +3,79 @@ use crate::{errors::{ConnectionError, ControllerError, RemoteError}, Workspace};
/// Get the workspace id. /// Get the workspace id.
#[jni(package = "mp.code", class = "Workspace")] #[jni(package = "mp.code", class = "Workspace")]
fn get_workspace_id(workspace: Workspace) -> String { fn get_workspace_id(workspace: &mut Workspace) -> String {
workspace.id() workspace.id()
} }
/// Get a cursor controller by name and returns a pointer to it. /// Get a cursor controller by name and returns a pointer to it.
#[jni(package = "mp.code", class = "Workspace")] #[jni(package = "mp.code", class = "Workspace")]
fn get_cursor(workspace: Workspace) -> crate::cursor::Controller { fn get_cursor(workspace: &mut Workspace) -> crate::cursor::Controller {
workspace.cursor() workspace.cursor()
} }
/// Get a buffer controller by name and returns a pointer to it. /// Get a buffer controller by name and returns a pointer to it.
#[jni(package = "mp.code", class = "Workspace")] #[jni(package = "mp.code", class = "Workspace")]
fn get_buffer(workspace: Workspace, path: String) -> Option<crate::buffer::Controller> { fn get_buffer(workspace: &mut Workspace, path: String) -> Option<crate::buffer::Controller> {
workspace.buffer_by_name(&path) workspace.buffer_by_name(&path)
} }
/// Get the filetree. /// Get the filetree.
#[jni(package = "mp.code", class = "Workspace")] #[jni(package = "mp.code", class = "Workspace")]
fn get_file_tree(workspace: Workspace, filter: Option<String>, strict: bool) -> Vec<String> { fn get_file_tree(workspace: &mut Workspace, filter: Option<String>, strict: bool) -> Vec<String> {
workspace.filetree(filter.as_deref(), strict) workspace.filetree(filter.as_deref(), strict)
} }
/// Gets a list of the active buffers. /// Gets a list of the active buffers.
#[jni(package = "mp.code", class = "Workspace")] #[jni(package = "mp.code", class = "Workspace")]
fn active_buffers(workspace: Workspace) -> Vec<String> { fn active_buffers(workspace: &mut Workspace) -> Vec<String> {
workspace.buffer_list() workspace.buffer_list()
} }
/// Create a new buffer. /// Create a new buffer.
#[jni(package = "mp.code", class = "Workspace")] #[jni(package = "mp.code", class = "Workspace")]
fn create_buffer(workspace: Workspace, path: String) -> Result<(), RemoteError> { fn create_buffer(workspace: &mut Workspace, path: String) -> Result<(), RemoteError> {
super::tokio().block_on(workspace.create(&path)) super::tokio().block_on(workspace.create(&path))
} }
/// Attach to a buffer and return a pointer to its [crate::buffer::Controller]. /// Attach to a buffer and return a pointer to its [crate::buffer::Controller].
#[jni(package = "mp.code", class = "Workspace")] #[jni(package = "mp.code", class = "Workspace")]
fn attach_to_buffer(workspace: Workspace, path: String) -> Result<crate::buffer::Controller, ConnectionError> { fn attach_to_buffer(workspace: &mut Workspace, path: String) -> Result<crate::buffer::Controller, ConnectionError> {
super::tokio().block_on(workspace.attach(&path)) super::tokio().block_on(workspace.attach(&path))
} }
/// Detach from a buffer. /// Detach from a buffer.
#[jni(package = "mp.code", class = "Workspace")] #[jni(package = "mp.code", class = "Workspace")]
fn detach_from_buffer(workspace: Workspace, path: String) -> crate::workspace::DetachResult { fn detach_from_buffer(workspace: &mut Workspace, path: String) -> crate::workspace::DetachResult {
workspace.detach(&path) workspace.detach(&path)
} }
/// Update the local buffer list. /// Update the local buffer list.
#[jni(package = "mp.code", class = "Workspace")] #[jni(package = "mp.code", class = "Workspace")]
fn fetch_buffers(workspace: Workspace) -> Result<(), RemoteError> { fn fetch_buffers(workspace: &mut Workspace) -> Result<(), RemoteError> {
super::tokio().block_on(workspace.fetch_buffers()) super::tokio().block_on(workspace.fetch_buffers())
} }
/// Update the local user list. /// Update the local user list.
#[jni(package = "mp.code", class = "Workspace")] #[jni(package = "mp.code", class = "Workspace")]
fn fetch_users(workspace: Workspace) -> Result<(), RemoteError> { fn fetch_users(workspace: &mut Workspace) -> Result<(), RemoteError> {
super::tokio().block_on(workspace.fetch_users()) super::tokio().block_on(workspace.fetch_users())
} }
/// List users attached to a buffer. /// List users attached to a buffer.
#[jni(package = "mp.code", class = "Workspace")] #[jni(package = "mp.code", class = "Workspace")]
fn list_buffer_users(workspace: Workspace, path: String) -> Result<Vec<crate::api::User>, RemoteError> { fn list_buffer_users(workspace: &mut Workspace, path: String) -> Result<Vec<crate::api::User>, RemoteError> {
super::tokio().block_on(workspace.list_buffer_users(&path)) super::tokio().block_on(workspace.list_buffer_users(&path))
} }
/// Delete a buffer. /// Delete a buffer.
#[jni(package = "mp.code", class = "Workspace")] #[jni(package = "mp.code", class = "Workspace")]
fn delete_buffer(workspace: Workspace, path: String) -> Result<(), RemoteError> { fn delete_buffer(workspace: &mut Workspace, path: String) -> Result<(), RemoteError> {
super::tokio().block_on(workspace.delete(&path)) super::tokio().block_on(workspace.delete(&path))
} }
/// Receive a workspace event if present. /// Receive a workspace event if present.
#[jni(package = "mp.code", class = "Workspace")] #[jni(package = "mp.code", class = "Workspace")]
fn event(workspace: Workspace) -> Result<crate::api::Event, ControllerError> { fn event(workspace: &mut Workspace) -> Result<crate::api::Event, ControllerError> {
super::tokio().block_on(workspace.event()) super::tokio().block_on(workspace.event())
} }