This is an automated email from the ASF dual-hosted git repository. pnoltes pushed a commit to branch feature/599-provide-and-use-c-service-in-rust in repository https://gitbox.apache.org/repos/asf/celix.git
commit 8b3f9436ae5cf04559bb9b166dff765c25edb7f2 Author: Pepijn Noltes <pepijnnol...@gmail.com> AuthorDate: Mon Aug 28 23:51:45 2023 +0200 #599: Remove unused poc rust bundle example --- misc/experimental/rust/CMakeLists.txt | 8 +- misc/experimental/rust/Cargo.toml | 1 - misc/experimental/rust/rust_shell_api/src/lib.rs | 5 - misc/experimental/rust/rust_shell_tui/Cargo.toml | 28 ---- misc/experimental/rust/rust_shell_tui/src/lib.rs | 161 --------------------- .../rust/shell_command_bundle/src/lib.rs | 27 +--- 6 files changed, 3 insertions(+), 227 deletions(-) diff --git a/misc/experimental/rust/CMakeLists.txt b/misc/experimental/rust/CMakeLists.txt index 966f83c8..964ab62d 100644 --- a/misc/experimental/rust/CMakeLists.txt +++ b/misc/experimental/rust/CMakeLists.txt @@ -44,13 +44,9 @@ if (CELIX_RUST_EXPERIMENTAL AND TARGET Celix::shell_tui AND TARGET Celix::shell corrosion_link_libraries(rust_shell_command_activator Celix::framework ) + get_target_property(ACTUAL_LIB_TARGET rust_shell_command_activator INTERFACE_LINK_LIBRARIES) add_celix_bundle(rust_shell_command ACTIVATOR ${ACTUAL_LIB_TARGET}) - target_link_libraries(${ACTUAL_LIB_TARGET} INTERFACE - -Wl,--whole-archive - Celix::log_helper - -Wl,--no-whole-archive - ) add_dependencies(rust_shell_command rust_shell_command_activator) add_celix_container(rust_container NO_COPY @@ -62,9 +58,9 @@ if (CELIX_RUST_EXPERIMENTAL AND TARGET Celix::shell_tui AND TARGET Celix::shell add_celix_container(rust_shell_cnt NO_COPY BUNDLES - Celix::log_admin Celix::shell Celix::shell_tui + Celix::log_admin rust_shell_command ) diff --git a/misc/experimental/rust/Cargo.toml b/misc/experimental/rust/Cargo.toml index 9dc3d514..c8942eb6 100644 --- a/misc/experimental/rust/Cargo.toml +++ b/misc/experimental/rust/Cargo.toml @@ -22,5 +22,4 @@ members = [ "hello_world_activator", "rust_shell_api", "shell_command_bundle", - #"rust_shell_tui", ] diff --git a/misc/experimental/rust/rust_shell_api/src/lib.rs b/misc/experimental/rust/rust_shell_api/src/lib.rs index 0969f86c..0b439667 100644 --- a/misc/experimental/rust/rust_shell_api/src/lib.rs +++ b/misc/experimental/rust/rust_shell_api/src/lib.rs @@ -28,8 +28,3 @@ pub const COMMAND_DESCRIPTION: &str = "command.description"; pub trait RustShellCommandTrait { fn execute_command(&self, command_line: &str) -> Result<(), Error>; } - -#[doc = "A struct to register a Rust Shell Command"] -pub struct RustShellCommandStruct { - pub execute_command: Box<dyn Fn(&str) -> Result<(), Error>> -} diff --git a/misc/experimental/rust/rust_shell_tui/Cargo.toml b/misc/experimental/rust/rust_shell_tui/Cargo.toml deleted file mode 100644 index fb4dced0..00000000 --- a/misc/experimental/rust/rust_shell_tui/Cargo.toml +++ /dev/null @@ -1,28 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -[package] -name = "rust_shell_tui" -version = "0.0.1" - -[dependencies] -celix_bindings = { path = "../celix_bindings" } - -[lib] -name = "rust_shell_tui_activator" -path = "src/lib.rs" -crate-type = ["cdylib"] diff --git a/misc/experimental/rust/rust_shell_tui/src/lib.rs b/misc/experimental/rust/rust_shell_tui/src/lib.rs deleted file mode 100644 index 08727add..00000000 --- a/misc/experimental/rust/rust_shell_tui/src/lib.rs +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -extern crate celix_bindings; - -use std::os::raw::c_char; -use std::os::raw::c_void; -use std::ffi::CString; -use std::ffi::NulError; -use std::ptr::null_mut; -use celix_bindings::*; //Add all Apache Celix C bindings to the namespace (i.e. celix_bundleContext_log, etc.) - -// pub struct celix_shell_command { -// pub handle -// : *mut ::std ::os ::raw ::c_void, -// #[doc = -// " Calls the shell command.\n @param handle The shell command handle.\n @param commandLine The " -// "complete provided cmd line (e.g. for a 'stop' command -> 'stop 42')\n @param outStream The output " -// "stream, to use for printing normal flow info.\n @param errorStream The error stream, to use for " -// "printing error flow info.\n @return Whether a command is successfully executed."] pub -// executeCommand -// pub executeCommand : :: std :: option :: Option < unsafe extern "C" fn (handle : * mut :: std :: os :: raw :: c_void , commandLine : * const :: std :: os :: raw :: c_char , outStream : * mut FILE , errorStream : * mut FILE) -> bool > , } -// } - - -struct RustShellTui { - ctx: *mut celix_bundle_context_t, - svc_id: i64, - svc: celix_shell_command, -} - -unsafe extern "C" fn rust_shell_tui_execute_command(_handle: *mut c_void, command_line: *const c_char, out_stream: *mut FILE, error_stream: *mut FILE) -> bool { - let obj = Box::from_raw(_handle as *mut RustShellTui); - obj.execute_command(command_line, out_stream, error_stream) -} - -impl RustShellTui { - - unsafe fn new(ctx: *mut celix_bundle_context_t) -> Result<RustShellTui, NulError> { - let result = RustShellTui { - ctx, - svc_id: -1, - svc: celix_shell_command { - handle: null_mut(), - executeCommand: Some(rust_shell_tui_execute_command), - }, - }; - Ok(result) - } - - unsafe fn start(&mut self) -> Result<(), NulError> { - - // let mut input = String::new(); - // print!("-> "); - // loop { - // std::io::stdin().read_line(&mut input).unwrap(); - // println!("You typed: {}", input.trim()); - // input.clear(); - // print!("-> "); - // } - - // self.svc.executeCommand = Some(|handle: *mut c_void, commandLine: *const c_char, outStream: *mut FILE, errorStream: *mut FILE| -> bool { - // println!("RustShellTui::executeCommand called"); - // true - // }); - - //TODO let svc_name = CString::new(CELIX_SHELL_COMMAND_SERVICE_NAME as * const c_char).unwrap(); - let svc_name = CString::new("celix_shell_command").unwrap(); - - let command_name = CString::new("command.name").unwrap(); - let props = celix_properties_create(); - celix_properties_set(props, command_name.as_ptr(), CString::new("rust").unwrap().as_ptr()); - - self.svc.handle = Box::into_raw(Box::new(self.svc)) as *mut c_void; - - - self.svc_id = celix_bundleContext_registerServiceAsync( - self.ctx, - Box::into_raw(Box::new(self.svc)) as *mut c_void, - svc_name.as_ptr(), - props); - - Ok(()) - } - - unsafe fn stop(&mut self) -> Result<(), NulError> { - celix_bundleContext_unregisterService(self.ctx, self.svc_id); - self.svc_id = -1; - - // let to_drop = Box::from_raw(self.svc.handle); - // drop(to_drop); - - // TODO drop - // let to_drop = Box::from_raw(&self.svc); - // drop(to_drop); - Ok(()) - } - - fn execute_command(&self, _command_line: *const c_char, _out_stream: *mut FILE, _error_stream: *mut FILE) -> bool { - println!("RustShellTui::executeCommand called"); - true - } -} - -impl Drop for RustShellTui { - fn drop(&mut self) { () } -} - -#[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 obj = RustShellTui::new(ctx); - if obj.is_err() { - return CELIX_BUNDLE_EXCEPTION; - } - *data = Box::into_raw(Box::new(obj.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 obj = &mut *(data as *mut RustShellTui); - let result = obj.start(); - match result { - Ok(()) => CELIX_SUCCESS, - Err(_) => CELIX_BUNDLE_EXCEPTION, - } -} - -#[no_mangle] -pub unsafe extern "C" fn celix_bundleActivator_stop(data: *mut c_void, _ctx: *mut celix_bundle_context_t) -> celix_status_t { - let obj = &mut *(data as *mut RustShellTui); - let result = obj.stop(); - match result { - Ok(()) => CELIX_SUCCESS, - Err(_) => CELIX_BUNDLE_EXCEPTION, - } -} - -#[no_mangle] -pub unsafe extern "C" fn celix_bundleActivator_destroy(data: *mut c_void, _ctx: *mut celix_bundle_context_t) -> celix_status_t { - let obj = Box::from_raw(data as *mut RustShellTui); - drop(obj); - CELIX_SUCCESS -} - diff --git a/misc/experimental/rust/shell_command_bundle/src/lib.rs b/misc/experimental/rust/shell_command_bundle/src/lib.rs index bb097765..98d5501d 100644 --- a/misc/experimental/rust/shell_command_bundle/src/lib.rs +++ b/misc/experimental/rust/shell_command_bundle/src/lib.rs @@ -28,7 +28,7 @@ use std::sync::Arc; use celix::{BundleActivator, LogHelper}; use celix::BundleContext; use celix::Error; -use rust_shell_api::{RustShellCommandTrait, RustShellCommandStruct}; +use rust_shell_api::RustShellCommandTrait; use celix_bindings::celix_shell_command_t; use celix_bindings::FILE; @@ -123,21 +123,6 @@ impl ShellCommandActivator { self.log_helper.log_info("Rust trait Shell Command registered"); - //Register Rust struct service (with closures) as value - let cloned_ctx = self.ctx.clone(); - let rust_shell_command = RustShellCommandStruct { - execute_command: Box::new(move |command_line: &str| { - cloned_ctx.log_info(format!("Execute command: {}", command_line).as_str()); - Ok(()) - }), - }; - let registration = self.ctx.register_service() - .with_service(rust_shell_command) - .with_property("command.name", "exe_rust_command2") - .with_property("command.description", "Simple command written in a Rust struct using a Rust closure") - .build()?; - self.registrations.push(registration); - self.log_helper.log_info("Rust struct Shell Command registered"); Ok(()) } @@ -168,16 +153,6 @@ impl ShellCommandActivator { .build()?; self.log_helper.log_info(format!("Found {} RustShellCommandTrait services", count).as_str()); - self.log_helper.log_info("Use Rust struct service command service"); - let count = self.ctx.use_services() - .with_callback(Box::new( |svc: &RustShellCommandStruct| { - let exe_cmd = svc.execute_command.as_ref(); - let _ = exe_cmd("test rust struct"); - })) - .build()?; - self.log_helper.log_info(format!("Found {} RustShellCommandStruct services", count).as_str()); - - self.log_helper.log_info("Rust Shell Command started"); Ok(()) } }