This is an automated email from the ASF dual-hosted git repository. pnoltes pushed a commit to branch feature/599-add-rust-bindings in repository https://gitbox.apache.org/repos/asf/celix.git
commit 36b67ea93ba566f9d3f9a4d6a329fe2ce736d113 Author: Pepijn Noltes <pepijnnol...@gmail.com> AuthorDate: Tue Aug 1 23:30:44 2023 +0200 599: Update rust bundle to use celix_bindings --- .../rust/hello_world_activator/Cargo.toml | 6 +- .../rust/hello_world_activator/src/lib.rs | 87 ++++++++++++++++++++-- 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/misc/experimental/rust/hello_world_activator/Cargo.toml b/misc/experimental/rust/hello_world_activator/Cargo.toml index e3795ca0..4844acd2 100644 --- a/misc/experimental/rust/hello_world_activator/Cargo.toml +++ b/misc/experimental/rust/hello_world_activator/Cargo.toml @@ -17,8 +17,12 @@ [package] name = "rust_bundle_activator" -version = "0.1.0" +version = "0.0.1" + +[dependencies] +celix_bindings = { path = "../celix_bindings" } [lib] name = "rust_bundle_activator" +path = "src/lib.rs" crate-type = ["cdylib"] diff --git a/misc/experimental/rust/hello_world_activator/src/lib.rs b/misc/experimental/rust/hello_world_activator/src/lib.rs index 19e6d05a..17d835d4 100644 --- a/misc/experimental/rust/hello_world_activator/src/lib.rs +++ b/misc/experimental/rust/hello_world_activator/src/lib.rs @@ -17,16 +17,91 @@ * under the License. */ +extern crate celix_bindings; + +use std::error::Error; use std::os::raw::c_void; +use std::ffi::CString; +use std::ffi::NulError; +use celix_bindings::*; //Add all Apache Celix C bindings to the namespace (i.e. celix_bundleContext_log, etc.) + +struct RustBundle { + name: String, + ctx: *mut celix_bundle_context_t, +} + +impl RustBundle { + + unsafe fn new(name: String, ctx: *mut celix_bundle_context_t) -> Result<RustBundle, NulError> { + let result = RustBundle { + name, + ctx, + }; + result.log_lifecycle("created")?; + Ok(result) + } + + unsafe fn log_lifecycle(&self, event: &str) -> Result<(), NulError> { + let id = celix_bundleContext_getBundleId(self.ctx); + let c_string = CString::new(format!("Rust Bundle '{}' with id {} {}!", self.name, id, event))?; + celix_bundleContext_log(self.ctx, celix_log_level_CELIX_LOG_LEVEL_INFO, c_string.as_ptr()); + Ok(()) + } + + unsafe fn start(&self) -> Result<(), NulError> { + self.log_lifecycle("started") + } + + unsafe fn stop(&self) -> Result<(), NulError> { + self.log_lifecycle("stopped") + } +} + +impl Drop for RustBundle { + fn drop(&mut self) { + unsafe { + let result = self.log_lifecycle("destroyed"); + match result { + Ok(()) => (), + Err(e) => println!("Error while logging: {}", e), + } + } + } +} + +#[no_mangle] +pub unsafe extern "C" fn celix_bundleActivator_create(ctx: *mut celix_bundle_context_t, data: *mut *mut c_void) -> celix_status_t { + let rust_bundle = RustBundle::new("Hello World".to_string(), ctx); + if rust_bundle.is_err() { + return CELIX_BUNDLE_EXCEPTION; + } + *data = Box::into_raw(Box::new(rust_bundle.unwrap())) as *mut c_void; + CELIX_SUCCESS +} + +#[no_mangle] +pub unsafe extern "C" fn celix_bundleActivator_start(data: *mut c_void, _ctx: *mut celix_bundle_context_t) -> celix_status_t { + let rust_bundle = &*(data as *mut RustBundle); + let result = rust_bundle.start(); + match result { + Ok(()) => CELIX_SUCCESS, + Err(_) => CELIX_BUNDLE_EXCEPTION, + } +} #[no_mangle] -pub unsafe extern "C" fn celix_bundleActivator_start(_data: *mut c_void, _context: *mut c_void) -> i32 { - println!("Rust Bundle started!"); - 0 +pub unsafe extern "C" fn celix_bundleActivator_stop(data: *mut c_void, _ctx: *mut celix_bundle_context_t) -> celix_status_t { + let rust_bundle = &*(data as *mut RustBundle); + let result = rust_bundle.stop(); + match result { + Ok(()) => CELIX_SUCCESS, + Err(_) => CELIX_BUNDLE_EXCEPTION, + } } #[no_mangle] -pub unsafe extern "C" fn celix_bundleActivator_stop(_data: *mut c_void, _context: *mut c_void) -> i32 { - println!("Rust Bundle stopped!"); - 0 +pub unsafe extern "C" fn celix_bundleActivator_destroy(data: *mut c_void, _ctx: *mut celix_bundle_context_t) -> celix_status_t { + let rust_bundle = Box::from_raw(data as *mut RustBundle); + drop(rust_bundle); + CELIX_SUCCESS }