replace tryFrom &Bytes to Bytes

This commit is contained in:
appflowy 2021-08-21 12:34:11 +08:00
parent fed6117d04
commit 57b4a8e114
5 changed files with 26 additions and 20 deletions

View File

@ -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]

View File

@ -22,9 +22,9 @@ pub fn make_de_token_steam(ctxt: &Ctxt, ast: &ASTContainer) -> Option<TokenStrea
});
let de_token_stream: TokenStream = quote! {
impl std::convert::TryFrom<&bytes::Bytes> for #struct_ident {
impl std::convert::TryFrom<bytes::Bytes> for #struct_ident {
type Error = ::protobuf::ProtobufError;
fn try_from(bytes: &bytes::Bytes) -> Result<Self, Self::Error> {
fn try_from(bytes: bytes::Bytes) -> Result<Self, Self::Error> {
let mut pb: crate::protobuf::#pb_ty = ::protobuf::Message::parse_from_bytes(&bytes)?;
#struct_ident::try_from(&mut pb)
}

View File

@ -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<T> 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<Bytes, Error = protobuf::ProtobufError>,
{
fn parse_from_bytes(bytes: Bytes) -> Result<Self, DispatchError> {
let data = T::try_from(&bytes)?;
let data = T::try_from(bytes.clone())?;
Ok(data)
}
}

View File

@ -8,21 +8,22 @@ use std::{
};
use tokio::sync::{oneshot, oneshot::error::RecvError};
pub async fn http_post<T1, T2>(url: &str, data: T1) -> ResultFuture<T2, NetworkError>
where
T1: TryInto<Bytes, Error = ProtobufError> + Send + Sync + 'static,
T2: TryFrom<Bytes, Error = ProtobufError> + Send + Sync + 'static,
{
let url = url.to_owned();
ResultFuture::new(async move { post(url, data).await })
}
// pub async fn http_post<T1, T2>(url: &str, data: T1) -> ResultFuture<T2,
// NetworkError> where
// T1: TryInto<Bytes, Error = ProtobufError> + Send + Sync + 'static,
// T2: TryFrom<Bytes, Error = ProtobufError> + Send + Sync + 'static,
// {
// let url = url.to_owned();
// ResultFuture::new(async move { post(url, data).await })
// }
pub async fn post<T1, T2>(url: String, data: T1) -> Result<T2, NetworkError>
pub async fn http_post<T1, T2>(url: &str, data: T1) -> Result<T2, NetworkError>
where
T1: TryInto<Bytes, Error = ProtobufError>,
T2: TryFrom<Bytes, Error = ProtobufError>,
{
let request_bytes: Bytes = data.try_into()?;
let url = url.to_owned();
let (tx, rx) = oneshot::channel::<Result<Response, _>>();
tokio::spawn(async move {

View File

@ -27,8 +27,10 @@ impl UserServerImpl {}
impl UserServer for UserServerImpl {
fn sign_up(&self, params: SignUpParams) -> ResultFuture<SignUpResponse, UserError> {
// 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<SignInResponse, UserError> {