Skip to content

Commit f730afc

Browse files
committed
Step 1: Add FFI Function to Rust Code
Signed-off-by: currantw <taylor.curran@improving.com>
1 parent b1ae357 commit f730afc

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

rust/src/lib.rs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,10 +802,85 @@ pub unsafe extern "C-unwind" fn refresh_iam_token(
802802
);
803803
},
804804
};
805+
806+
async_panic_guard.panicked = false;
807+
});
808+
809+
panic_guard.panicked = false;
810+
}
811+
812+
/// Update connection password
813+
///
814+
/// # Arguments
815+
/// * `client_ptr` - Pointer to the client
816+
/// * `callback_index` - Callback index for async response
817+
/// * `password` - New password (null for password removal)
818+
/// * `immediate_auth` - Whether to authenticate immediately
819+
///
820+
/// # Safety
821+
/// * `client_ptr` must be a valid pointer to a Client
822+
/// * `password` must be a valid C string or null
823+
#[unsafe(no_mangle)]
824+
pub unsafe extern "C-unwind" fn update_connection_password(
825+
client_ptr: *const c_void,
826+
callback_index: usize,
827+
password: *const c_char,
828+
immediate_auth: bool,
829+
) {
830+
// Build client and add panic guard.
831+
let client = unsafe {
832+
Arc::increment_strong_count(client_ptr);
833+
Arc::from_raw(client_ptr as *mut Client)
834+
};
835+
let core = client.core.clone();
836+
837+
let mut panic_guard = PanicGuard {
838+
panicked: true,
839+
failure_callback: core.failure_callback,
840+
callback_index,
841+
};
842+
843+
let password_opt = if password.is_null() {
844+
None
845+
} else {
846+
Some(
847+
unsafe { CStr::from_ptr(password) }
848+
.to_str()
849+
.expect("Can not read password argument.")
850+
.to_owned(),
851+
)
852+
};
853+
854+
client.runtime.spawn(async move {
855+
let mut async_panic_guard = PanicGuard {
856+
panicked: true,
857+
failure_callback: core.failure_callback,
858+
callback_index,
859+
};
860+
861+
let result = core
862+
.client
863+
.clone()
864+
.update_connection_password(password_opt, immediate_auth)
865+
.await;
866+
match result {
867+
Ok(_) => {
868+
let response = ResponseValue::from_value(redis::Value::Okay);
869+
let ptr = Box::into_raw(Box::new(response));
870+
unsafe { (core.success_callback)(callback_index, ptr) };
871+
}
872+
Err(err) => unsafe {
873+
report_error(
874+
core.failure_callback,
875+
callback_index,
876+
error_message(&err),
877+
error_type(&err),
878+
);
879+
},
880+
};
881+
805882
async_panic_guard.panicked = false;
806-
drop(async_panic_guard);
807883
});
808884

809885
panic_guard.panicked = false;
810-
drop(panic_guard);
811886
}

0 commit comments

Comments
 (0)