Skip to content

Commit

Permalink
Fix fmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
youyuanwu committed Nov 19, 2023
1 parent e24bab9 commit 0d9d472
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 64 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ add_custom_target(build_fabric_rust_pal ALL
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

# only format not generated code
add_custom_target(format ALL
COMMAND ${cargo_exe} fmt -p fabric_ext
COMMAND ${cargo_exe} fmt -p pal
COMMAND ${cargo_exe} fmt -p samples_client
COMMAND ${cargo_exe} fmt -p samples_echomain
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

set(_pkg_root ${CMAKE_BINARY_DIR}/echoapp_root)
set(_pkg_src ${CMAKE_CURRENT_SOURCE_DIR}/crates/samples/echomain)
if(WIN32)
Expand Down
2 changes: 1 addition & 1 deletion crates/fabric/base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ pub mod Microsoft;
#[cfg(feature = "ServiceFabric")]
pub use Microsoft::ServiceFabric::*;

pub use fabric_metadata;
pub use fabric_metadata;
36 changes: 27 additions & 9 deletions crates/fabric/ext/src/fasync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,20 @@ pub struct AwaitableCallback {
shared_state: Arc<Mutex<SharedState>>,
}

impl Default for AwaitableCallback {
fn default() -> Self {
Self::new()
}
}

impl AwaitableCallback {
pub fn new() -> AwaitableCallback {
return AwaitableCallback {
AwaitableCallback {
shared_state: Arc::new(Mutex::new(SharedState {
completed: false,
waker: None,
})),
};
}
}
}

Expand All @@ -84,7 +90,7 @@ impl IFabricAsyncOperationCallback_Impl for AwaitableCallback {

impl IFabricAwaitableCallback_Impl for AwaitableCallback {
unsafe fn get_token(&self) -> AwaitableToken {
return AwaitableToken::new(self.shared_state.clone());
AwaitableToken::new(self.shared_state.clone())
}
}

Expand All @@ -95,9 +101,9 @@ pub struct AwaitableToken {

impl AwaitableToken {
pub fn new(state: Arc<Mutex<SharedState>>) -> AwaitableToken {
return AwaitableToken {
AwaitableToken {
shared_state: state,
};
}
}
}

Expand Down Expand Up @@ -139,6 +145,12 @@ macro_rules! beginmyclient {
unsafe impl Send for $name {}
unsafe impl Sync for $name {}

impl Default for $name {
fn default() -> Self {
Self::new()
}
}

impl $name {
pub fn new() -> $name {
return $name {
Expand All @@ -153,7 +165,7 @@ macro_rules! beginmyclient {
};
}
} // impl
}
};
}

// macros for impl async fn
Expand Down Expand Up @@ -215,7 +227,7 @@ unsafe impl<T> Send for SBox<T> {}

impl<T> SBox<T> {
pub fn new(x: T) -> SBox<T> {
return SBox { b: Box::new(x) };
SBox { b: Box::new(x) }
}
}

Expand Down Expand Up @@ -261,16 +273,22 @@ pub struct FabricQueryClient {
unsafe impl Send for FabricQueryClient {}
unsafe impl Sync for FabricQueryClient {}

impl Default for FabricQueryClient {
fn default() -> Self {
Self::new()
}
}

impl FabricQueryClient {
pub fn new() -> FabricQueryClient {
return FabricQueryClient {
FabricQueryClient {
c_: SBox::new(unsafe {
IFabricQueryClient::from_raw(
FabricCreateLocalClient(&IFabricQueryClient::IID)
.expect("cannot get localclient"),
)
}),
};
}
}

myasyncfunc!(
Expand Down
37 changes: 18 additions & 19 deletions crates/fabric/ext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ pub mod fasync;
//use std::os::windows::prelude::OsStrExt;
use std::sync::{Arc, Condvar, Mutex};

use log::info;
use fabric_base::FabricCommon::{
IFabricAsyncOperationCallback, IFabricAsyncOperationCallback_Impl,
IFabricAsyncOperationCallback_Vtbl, IFabricAsyncOperationContext,
IFabricAsyncOperationContext_Impl, IFabricStringResult, IFabricStringResult_Impl,
};
use log::info;
use windows::core::implement;
use windows_core::{HSTRING, PCWSTR};

Expand All @@ -35,11 +35,17 @@ pub struct WaitableCallback {
pair_: Arc<(Mutex<bool>, Condvar)>,
}

impl Default for WaitableCallback {
fn default() -> Self {
Self::new()
}
}

impl WaitableCallback {
pub fn new() -> WaitableCallback {
return WaitableCallback {
WaitableCallback {
pair_: Arc::new((Mutex::new(false), Condvar::new())),
};
}
}
}

Expand Down Expand Up @@ -81,7 +87,7 @@ pub fn pwstr_to_string(p: PCWSTR) -> String {
let ret: String = unsafe {
String::from_utf16_lossy(std::slice::from_raw_parts(p.0, end.offset_from(p.0) as _))
};
return ret;
ret
}

// The basic implementation of async context
Expand All @@ -95,33 +101,26 @@ pub struct AsyncContext {
impl AsyncContext {
// construct ctx. Note: caller needs to invoke callback.
// This is different from cpp impl.
pub fn new(
callback: core::option::Option<
&IFabricAsyncOperationCallback,
>,
) -> AsyncContext {
pub fn new(callback: core::option::Option<&IFabricAsyncOperationCallback>) -> AsyncContext {
info!("AsyncContext::new");
let callback_copy: IFabricAsyncOperationCallback = callback.expect("msg").clone();

let ctx = AsyncContext {
AsyncContext {
callback_: callback_copy,
};
return ctx;
}
}
}

impl IFabricAsyncOperationContext_Impl for AsyncContext {
fn IsCompleted(&self) -> windows::Win32::Foundation::BOOLEAN {
return windows::Win32::Foundation::BOOLEAN::from(true);
windows::Win32::Foundation::BOOLEAN::from(true)
}

fn CompletedSynchronously(&self) -> windows::Win32::Foundation::BOOLEAN {
return windows::Win32::Foundation::BOOLEAN::from(true);
windows::Win32::Foundation::BOOLEAN::from(true)
}

fn Callback(
&self,
) -> windows::core::Result<IFabricAsyncOperationCallback> {
fn Callback(&self) -> windows::core::Result<IFabricAsyncOperationCallback> {
info!("AsyncContext::Callback");
// get a view of the callback
let callback_copy: IFabricAsyncOperationCallback = self.callback_.clone();
Expand All @@ -147,14 +146,14 @@ impl StringResult {
let ret = StringResult {
vec_: data.as_wide().to_vec(),
};
return ret;
ret
}
}

impl IFabricStringResult_Impl for StringResult {
fn get_String(&self) -> windows::core::PCWSTR {
// This is some hack to get the raw pointer out.
let ptr: *mut u16 = self.vec_.as_ptr() as *mut u16;
return windows::core::PCWSTR::from_raw(ptr);
windows::core::PCWSTR::from_raw(ptr)
}
}
2 changes: 1 addition & 1 deletion crates/samples/client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// license information.
// ------------------------------------------------------------

use fabric_ext::{IFabricWaitableCallback, WaitableCallback};
use fabric_base::FabricCommon::{FabricClient::*, IFabricAsyncOperationCallback};
use fabric_base::{FABRIC_NODE_QUERY_DESCRIPTION, FABRIC_NODE_QUERY_RESULT_ITEM};
use fabric_ext::{IFabricWaitableCallback, WaitableCallback};
use windows_core::{ComInterface, Interface};

fn main() -> windows::core::Result<()> {
Expand Down
51 changes: 23 additions & 28 deletions crates/samples/echomain/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
#![allow(non_snake_case)]

use std::cell::Cell;
use std::convert::TryInto;
use std::io::Error;
use std::thread::JoinHandle;
use std::{convert::TryInto, ptr::null};

use fabric_ext::{AsyncContext, StringResult};
use log::info;
use fabric_base::FabricCommon::FabricRuntime::{
IFabricRuntime, IFabricStatelessServiceFactory, IFabricStatelessServiceFactory_Impl,
IFabricStatelessServiceInstance, IFabricStatelessServiceInstance_Impl, IFabricStatelessServicePartition,
IFabricStatelessServiceInstance, IFabricStatelessServiceInstance_Impl,
IFabricStatelessServicePartition,
};
use fabric_base::FabricCommon::{IFabricAsyncOperationContext, IFabricStringResult, IFabricAsyncOperationCallback};
use fabric_base::FabricCommon::{
IFabricAsyncOperationCallback, IFabricAsyncOperationContext, IFabricStringResult,
};
use fabric_ext::{AsyncContext, StringResult};
use log::info;
use tokio::sync::oneshot::{self, Sender};
use windows::core::implement;
use windows::core::w;
Expand Down Expand Up @@ -50,6 +53,7 @@ impl ServiceFactory {
}

impl IFabricStatelessServiceFactory_Impl for ServiceFactory {
#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn CreateInstance(
&self,
servicetypename: &::windows::core::PCWSTR,
Expand All @@ -60,7 +64,7 @@ impl IFabricStatelessServiceFactory_Impl for ServiceFactory {
instanceid: i64,
) -> ::windows::core::Result<IFabricStatelessServiceInstance> {
let mut init_data: String = "".to_string();
if initializationdata != null() && initializationdatalength != 0 {
if initializationdata.is_null() && initializationdatalength != 0 {
init_data = unsafe {
String::from_utf8_lossy(std::slice::from_raw_parts(
initializationdata,
Expand All @@ -69,17 +73,18 @@ impl IFabricStatelessServiceFactory_Impl for ServiceFactory {
.to_string()
};
}
info!("servicetypename: {}, servicename: {:?}, initdata: {}, partitionid: {:?}, instanceid {}",
unsafe{servicetypename.display()},
info!(
"servicetypename: {}, servicename: {:?}, initdata: {}, partitionid: {:?}, instanceid {}",
unsafe { servicetypename.display() },
servicename,
init_data,
partitionid,
instanceid
);
let port_copy = self.port_.clone();
let port_copy = self.port_;
let hostname_copy = self.hostname_.clone();
let instance = AppInstance::new(port_copy, hostname_copy);
return Ok(instance.into());
Ok(instance.into())
}
}

Expand All @@ -95,24 +100,20 @@ pub struct AppInstance {

impl AppInstance {
pub fn new(port: u32, hostname: HSTRING) -> AppInstance {
return AppInstance {
AppInstance {
port_: port,
hostname_: hostname,
tx_: Cell::from(None),
th_: Cell::from(None),
};
}
}
}

impl IFabricStatelessServiceInstance_Impl for AppInstance {
fn BeginOpen(
&self,
partition: core::option::Option<
&IFabricStatelessServicePartition,
>,
callback: core::option::Option<
&IFabricAsyncOperationCallback,
>,
partition: core::option::Option<&IFabricStatelessServicePartition>,
callback: core::option::Option<&IFabricAsyncOperationCallback>,
) -> windows::core::Result<IFabricAsyncOperationContext> {
let p = partition.as_ref().expect("get partition failed");
let info = unsafe { p.GetPartitionInfo() }.expect("getpartition info failed");
Expand All @@ -124,7 +125,7 @@ impl IFabricStatelessServiceInstance_Impl for AppInstance {

// TODO: emplement stop thread.

let port_copy = self.port_.clone();
let port_copy = self.port_;
let hostname_copy = self.hostname_.clone();

let (tx, rx) = oneshot::channel::<()>();
Expand All @@ -139,9 +140,7 @@ impl IFabricStatelessServiceInstance_Impl for AppInstance {

fn EndOpen(
&self,
context: core::option::Option<
&IFabricAsyncOperationContext,
>,
context: core::option::Option<&IFabricAsyncOperationContext>,
) -> windows::core::Result<IFabricStringResult> {
info!("AppInstance::EndOpen");
let completed = unsafe {
Expand All @@ -163,9 +162,7 @@ impl IFabricStatelessServiceInstance_Impl for AppInstance {

fn BeginClose(
&self,
callback: core::option::Option<
&IFabricAsyncOperationCallback,
>,
callback: core::option::Option<&IFabricAsyncOperationCallback>,
) -> windows::core::Result<IFabricAsyncOperationContext> {
info!("AppInstance::BeginClose");

Expand Down Expand Up @@ -203,9 +200,7 @@ impl IFabricStatelessServiceInstance_Impl for AppInstance {

fn EndClose(
&self,
context: core::option::Option<
&IFabricAsyncOperationContext,
>,
context: core::option::Option<&IFabricAsyncOperationContext>,
) -> windows::core::Result<()> {
info!("AppInstance::EndClose");
let completed = unsafe {
Expand Down
2 changes: 1 addition & 1 deletion crates/samples/echomain/src/app/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn get_addr(port: u32, hostname: HSTRING) -> String {
addr.push_str(&hostname.to_string());
addr.push(':');
addr.push_str(&port.to_string());
return addr;
addr
}

async fn echo_loop(listener: TcpListener) -> Result<(), Error> {
Expand Down
Loading

0 comments on commit 0d9d472

Please sign in to comment.