From 57b4a8e11473143d1177f41ab88fc65f22abf219 Mon Sep 17 00:00:00 2001 From: appflowy Date: Sat, 21 Aug 2021 12:34:11 +0800 Subject: [PATCH] replace tryFrom &Bytes to Bytes --- rust-lib/dart-ffi/Cargo.toml | 8 ++++---- .../flowy-derive/src/proto_buf/deserialize.rs | 4 ++-- rust-lib/flowy-dispatch/src/byte_trait.rs | 9 ++++++--- rust-lib/flowy-net/src/request/request.rs | 19 ++++++++++--------- .../src/services/user/user_server.rs | 6 ++++-- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/rust-lib/dart-ffi/Cargo.toml b/rust-lib/dart-ffi/Cargo.toml index 3132ea54aa..b29f4558cf 100644 --- a/rust-lib/dart-ffi/Cargo.toml +++ b/rust-lib/dart-ffi/Cargo.toml @@ -7,11 +7,11 @@ edition = "2018" [lib] name = "dart_ffi" # this value will change depending on the target os -# for iOS it would be `rlib` -# for Macos it would be `rlib` +# for iOS it would be `cdylib` +# for Macos it would be `cdylib` # for android it would be `c-dylib` -# default rlib -crate-type = ["rlib"] +# default cdylib +crate-type = ["cdylib"] [dependencies] diff --git a/rust-lib/flowy-derive/src/proto_buf/deserialize.rs b/rust-lib/flowy-derive/src/proto_buf/deserialize.rs index a490da261d..d84b90a2a5 100644 --- a/rust-lib/flowy-derive/src/proto_buf/deserialize.rs +++ b/rust-lib/flowy-derive/src/proto_buf/deserialize.rs @@ -22,9 +22,9 @@ pub fn make_de_token_steam(ctxt: &Ctxt, ast: &ASTContainer) -> Option for #struct_ident { + impl std::convert::TryFrom for #struct_ident { type Error = ::protobuf::ProtobufError; - fn try_from(bytes: &bytes::Bytes) -> Result { + fn try_from(bytes: bytes::Bytes) -> Result { let mut pb: crate::protobuf::#pb_ty = ::protobuf::Message::parse_from_bytes(&bytes)?; #struct_ident::try_from(&mut pb) } diff --git a/rust-lib/flowy-dispatch/src/byte_trait.rs b/rust-lib/flowy-dispatch/src/byte_trait.rs index 1c427f846e..531d4aab7a 100644 --- a/rust-lib/flowy-dispatch/src/byte_trait.rs +++ b/rust-lib/flowy-dispatch/src/byte_trait.rs @@ -1,6 +1,7 @@ use crate::errors::{DispatchError, InternalError}; use bytes::Bytes; use protobuf::ProtobufError; +use std::convert::TryFrom; // To bytes pub trait ToBytes { @@ -48,11 +49,13 @@ pub trait FromBytes: Sized { #[cfg(feature = "use_protobuf")] impl FromBytes for T where - // https://stackoverflow.com/questions/62871045/tryfromu8-trait-bound-in-trait - T: for<'a> std::convert::TryFrom<&'a Bytes, Error = protobuf::ProtobufError>, + // // https://stackoverflow.com/questions/62871045/tryfromu8-trait-bound-in-trait + // T: for<'a> std::convert::TryFrom<&'a Bytes, Error = + // protobuf::ProtobufError>, + T: std::convert::TryFrom, { fn parse_from_bytes(bytes: Bytes) -> Result { - let data = T::try_from(&bytes)?; + let data = T::try_from(bytes.clone())?; Ok(data) } } diff --git a/rust-lib/flowy-net/src/request/request.rs b/rust-lib/flowy-net/src/request/request.rs index e4959062c3..85ac9b65d2 100644 --- a/rust-lib/flowy-net/src/request/request.rs +++ b/rust-lib/flowy-net/src/request/request.rs @@ -8,21 +8,22 @@ use std::{ }; use tokio::sync::{oneshot, oneshot::error::RecvError}; -pub async fn http_post(url: &str, data: T1) -> ResultFuture -where - T1: TryInto + Send + Sync + 'static, - T2: TryFrom + Send + Sync + 'static, -{ - let url = url.to_owned(); - ResultFuture::new(async move { post(url, data).await }) -} +// pub async fn http_post(url: &str, data: T1) -> ResultFuture where +// T1: TryInto + Send + Sync + 'static, +// T2: TryFrom + Send + Sync + 'static, +// { +// let url = url.to_owned(); +// ResultFuture::new(async move { post(url, data).await }) +// } -pub async fn post(url: String, data: T1) -> Result +pub async fn http_post(url: &str, data: T1) -> Result where T1: TryInto, T2: TryFrom, { let request_bytes: Bytes = data.try_into()?; + let url = url.to_owned(); let (tx, rx) = oneshot::channel::>(); tokio::spawn(async move { diff --git a/rust-lib/flowy-user/src/services/user/user_server.rs b/rust-lib/flowy-user/src/services/user/user_server.rs index 22eb701799..446b19ee73 100644 --- a/rust-lib/flowy-user/src/services/user/user_server.rs +++ b/rust-lib/flowy-user/src/services/user/user_server.rs @@ -27,8 +27,10 @@ impl UserServerImpl {} impl UserServer for UserServerImpl { fn sign_up(&self, params: SignUpParams) -> ResultFuture { - // http_post(SIGN_UP_URL.as_ref(), params) - unimplemented!() + ResultFuture::new(async move { + let a = http_post(SIGN_UP_URL.as_ref(), params).await?; + Ok(a) + }) } fn sign_in(&self, _params: SignInParams) -> ResultFuture {