瀏覽代碼

replace vec<u8> with Bytes

appflowy 3 年之前
父節點
當前提交
2ebe922507

+ 1 - 0
rust-lib/dart-ffi/Cargo.toml

@@ -24,6 +24,7 @@ tokio = { version = "1", features = ["rt", "rt-multi-thread"] }
 log = "0.4.14"
 serde = { version = "1.0", features = ["derive"] }
 serde_json = {version = "1.0"}
+bytes = { version = "1.0" }
 
 flowy-dispatch = {path = "../flowy-dispatch"}
 flowy-sdk = {path = "../flowy-sdk"}

+ 1 - 1
rust-lib/dart-ffi/src/lib.rs

@@ -67,7 +67,7 @@ async fn post_to_flutter(response: EventResponse, port: i64) {
     match isolate
         .catch_unwind(async {
             let ffi_resp = FFIResponse::from(response);
-            ffi_resp.into_bytes().unwrap()
+            ffi_resp.into_bytes().unwrap().to_vec()
         })
         .await
     {

+ 4 - 1
rust-lib/dart-ffi/src/model/ffi_request.rs

@@ -1,3 +1,4 @@
+use bytes::Bytes;
 use flowy_derive::ProtoBuf;
 use flowy_dispatch::prelude::ModuleRequest;
 use std::convert::TryFrom;
@@ -13,7 +14,9 @@ pub struct FFIRequest {
 
 impl FFIRequest {
     pub fn from_u8_pointer(pointer: *const u8, len: usize) -> Self {
-        let bytes = unsafe { std::slice::from_raw_parts(pointer, len) }.to_vec();
+        let buffer = unsafe { std::slice::from_raw_parts(pointer, len) }.to_vec();
+        let bytes = Bytes::from(buffer);
+
         let request: FFIRequest = FFIRequest::try_from(&bytes).unwrap();
         request
     }

+ 1 - 1
rust-lib/dart-ffi/src/model/ffi_response.rs

@@ -24,7 +24,7 @@ pub struct FFIResponse {
 impl std::convert::From<EventResponse> for FFIResponse {
     fn from(resp: EventResponse) -> Self {
         let payload = match resp.payload {
-            Payload::Bytes(bytes) => bytes,
+            Payload::Bytes(bytes) => bytes.to_vec(),
             Payload::None => vec![],
         };
 

+ 3 - 3
rust-lib/flowy-derive/src/proto_buf/deserialize.rs

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

+ 3 - 3
rust-lib/flowy-derive/src/proto_buf/serialize.rs

@@ -17,14 +17,14 @@ pub fn make_se_token_stream(ctxt: &Ctxt, ast: &ASTContainer) -> Option<TokenStre
 
     let se_token_stream: TokenStream = quote! {
 
-        impl std::convert::TryInto<Vec<u8>> for #struct_ident {
+        impl std::convert::TryInto<bytes::Bytes> for #struct_ident {
             type Error = String;
-            fn try_into(self) -> Result<Vec<u8>, Self::Error> {
+            fn try_into(self) -> Result<bytes::Bytes, Self::Error> {
                 use protobuf::Message;
                 let pb: crate::protobuf::#pb_ty = self.try_into()?;
                 let result: ::protobuf::ProtobufResult<Vec<u8>> = pb.write_to_bytes();
                 match result {
-                    Ok(bytes) => { Ok(bytes) },
+                    Ok(bytes) => { Ok(bytes::Bytes::from(bytes)) },
                     Err(e) => { Err(format!("{:?}", e)) }
                 }
             }

+ 1 - 1
rust-lib/flowy-dispatch/Cargo.toml

@@ -12,7 +12,7 @@ paste = "1"
 futures-channel = "0.3.15"
 futures = "0.3.15"
 futures-util = "0.3.15"
-bytes = "1.0"
+bytes = {version = "1.0", features = ["serde"]}
 tokio = { version = "1", features = ["full"] }
 uuid = { version = "0.8", features = ["serde", "v4"] }
 log = "0.4.14"

+ 13 - 10
rust-lib/flowy-dispatch/src/byte_trait.rs

@@ -1,14 +1,16 @@
+use bytes::Bytes;
+
 // To bytes
 pub trait ToBytes {
-    fn into_bytes(self) -> Result<Vec<u8>, String>;
+    fn into_bytes(self) -> Result<Bytes, String>;
 }
 
 #[cfg(feature = "use_protobuf")]
 impl<T> ToBytes for T
 where
-    T: std::convert::TryInto<Vec<u8>, Error = String>,
+    T: std::convert::TryInto<Bytes, Error = String>,
 {
-    fn into_bytes(self) -> Result<Vec<u8>, String> { self.try_into() }
+    fn into_bytes(self) -> Result<Bytes, String> { self.try_into() }
 }
 
 #[cfg(feature = "use_serde")]
@@ -16,9 +18,9 @@ impl<T> ToBytes for T
 where
     T: serde::Serialize,
 {
-    fn into_bytes(self) -> Result<Vec<u8>, String> {
+    fn into_bytes(self) -> Result<Bytes, String> {
         match serde_json::to_string(&self.0) {
-            Ok(s) => Ok(s.into_bytes()),
+            Ok(s) => Ok(Bytes::from(s)),
             Err(e) => Err(format!("{:?}", e)),
         }
     }
@@ -27,16 +29,16 @@ where
 // From bytes
 
 pub trait FromBytes: Sized {
-    fn parse_from_bytes(bytes: &Vec<u8>) -> Result<Self, String>;
+    fn parse_from_bytes(bytes: Bytes) -> Result<Self, String>;
 }
 
 #[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 Vec<u8>, Error = String>,
+    T: for<'a> std::convert::TryFrom<&'a Bytes, Error = String>,
 {
-    fn parse_from_bytes(bytes: &Vec<u8>) -> Result<Self, String> { T::try_from(bytes) }
+    fn parse_from_bytes(bytes: Bytes) -> Result<Self, String> { T::try_from(&bytes) }
 }
 
 #[cfg(feature = "use_serde")]
@@ -44,8 +46,9 @@ impl<T> FromBytes for T
 where
     T: serde::de::DeserializeOwned + 'static,
 {
-    fn parse_from_bytes(bytes: &Vec<u8>) -> Result<Self, String> {
-        let s = String::from_utf8_lossy(bytes);
+    fn parse_from_bytes(bytes: Bytes) -> Result<Self, String> {
+        let s = String::from_utf8_lossy(&bytes);
+
         match serde_json::from_str::<T>(s.as_ref()) {
             Ok(data) => Ok(data),
             Err(e) => Err(format!("{:?}", e)),

+ 5 - 4
rust-lib/flowy-dispatch/src/data.rs

@@ -5,6 +5,7 @@ use crate::{
     response::{EventResponse, Responder, ResponseBuilder},
     util::ready::{ready, Ready},
 };
+use bytes::Bytes;
 use std::ops;
 
 pub struct Data<T>(pub T);
@@ -34,7 +35,7 @@ where
     fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future {
         match payload {
             Payload::None => ready(Err(unexpected_none_payload(req))),
-            Payload::Bytes(bytes) => match T::parse_from_bytes(bytes) {
+            Payload::Bytes(bytes) => match T::parse_from_bytes(bytes.clone()) {
                 Ok(data) => ready(Ok(Data(data))),
                 Err(e) => ready(Err(InternalError::new(format!("{}", e)).into())),
             },
@@ -48,7 +49,7 @@ where
 {
     fn respond_to(self, _request: &EventRequest) -> EventResponse {
         match self.into_inner().into_bytes() {
-            Ok(bytes) => ResponseBuilder::Ok().data(bytes.to_vec()).build(),
+            Ok(bytes) => ResponseBuilder::Ok().data(bytes).build(),
             Err(e) => {
                 let system_err: DispatchError = InternalError::new(format!("{}", e)).into();
                 system_err.into()
@@ -79,7 +80,7 @@ where
 {
     match payload {
         Payload::None => Err(format!("Parse fail, expected payload")),
-        Payload::Bytes(bytes) => match T::parse_from_bytes(&bytes) {
+        Payload::Bytes(bytes) => match T::parse_from_bytes(bytes.clone()) {
             Ok(data) => Ok(Data(data)),
             Err(e) => Err(e),
         },
@@ -100,5 +101,5 @@ where
 }
 
 impl ToBytes for Data<String> {
-    fn into_bytes(self) -> Result<Vec<u8>, String> { Ok(self.0.into_bytes()) }
+    fn into_bytes(self) -> Result<Bytes, String> { Ok(Bytes::from(self.0)) }
 }

+ 2 - 1
rust-lib/flowy-dispatch/src/errors/errors.rs

@@ -3,6 +3,7 @@ use crate::{
     request::EventRequest,
     response::{EventResponse, ResponseBuilder},
 };
+use bytes::Bytes;
 use dyn_clone::DynClone;
 use serde::{Serialize, Serializer};
 use std::{fmt, option::NoneError};
@@ -68,7 +69,7 @@ impl From<String> for DispatchError {
 }
 
 impl FromBytes for DispatchError {
-    fn parse_from_bytes(bytes: &Vec<u8>) -> Result<Self, String> {
+    fn parse_from_bytes(bytes: Bytes) -> Result<Self, String> {
         let s = String::from_utf8(bytes.to_vec()).unwrap();
         Ok(InternalError { inner: s }.into())
     }

+ 5 - 8
rust-lib/flowy-dispatch/src/request/payload.rs

@@ -7,7 +7,7 @@ pub enum PayloadError {}
 #[derive(Clone, serde::Serialize)]
 pub enum Payload {
     None,
-    Bytes(Vec<u8>),
+    Bytes(Bytes),
 }
 
 impl std::fmt::Debug for Payload {
@@ -26,18 +26,15 @@ fn format_payload_print(payload: &Payload, f: &mut Formatter<'_>) -> fmt::Result
 }
 
 impl std::convert::Into<Payload> for String {
-    fn into(self) -> Payload { Payload::Bytes(self.into_bytes()) }
+    fn into(self) -> Payload { Payload::Bytes(Bytes::from(self)) }
 }
 
 impl std::convert::Into<Payload> for &'_ String {
-    fn into(self) -> Payload { Payload::Bytes(self.to_owned().into_bytes()) }
+    fn into(self) -> Payload { Payload::Bytes(Bytes::from(self.to_owned())) }
 }
 
 impl std::convert::Into<Payload> for Bytes {
-    fn into(self) -> Payload {
-        // Opti(nathan): do not copy the bytes?
-        Payload::Bytes(self.as_ref().to_vec())
-    }
+    fn into(self) -> Payload { Payload::Bytes(self) }
 }
 
 impl std::convert::Into<Payload> for () {
@@ -45,7 +42,7 @@ impl std::convert::Into<Payload> for () {
 }
 
 impl std::convert::Into<Payload> for Vec<u8> {
-    fn into(self) -> Payload { Payload::Bytes(self) }
+    fn into(self) -> Payload { Payload::Bytes(Bytes::from(self)) }
 }
 
 impl std::convert::Into<Payload> for &str {

+ 1 - 1
rust-lib/flowy-document/Cargo.toml

@@ -20,6 +20,6 @@ lazy_static = "1.4.0"
 log = "0.4.14"
 tokio = {version = "1.6.0", features = ["sync"]}
 tracing = { version = "0.1", features = ["log"] }
-
+bytes = { version = "1.0" }
 [dev-dependencies]
 flowy-test = { path = "../flowy-test" }

+ 2 - 1
rust-lib/flowy-document/src/errors.rs

@@ -1,4 +1,5 @@
 use crate::services::file_manager::FileError;
+use bytes::Bytes;
 use derive_more::Display;
 use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
 use flowy_dispatch::prelude::{EventResponse, ResponseBuilder};
@@ -74,7 +75,7 @@ impl std::convert::From<FileError> for DocError {
 
 impl flowy_dispatch::Error for DocError {
     fn as_response(&self) -> EventResponse {
-        let bytes: Vec<u8> = self.clone().try_into().unwrap();
+        let bytes: Bytes = self.clone().try_into().unwrap();
         ResponseBuilder::Err().data(bytes).build()
     }
 }

+ 2 - 1
rust-lib/flowy-infra/Cargo.toml

@@ -15,4 +15,5 @@ flowy-sqlite = { path = "../flowy-sqlite"}
 lazy_static = "1.4.0"
 protobuf = {version = "2.18.0"}
 log = "0.4.14"
-chrono = "0.4.19"
+chrono = "0.4.19"
+bytes = { version = "1.0" }

+ 1 - 0
rust-lib/flowy-observable/Cargo.toml

@@ -10,6 +10,7 @@ lazy_static = {version = "1.4.0"}
 protobuf = {version = "2.20.0"}
 allo-isolate = {version = "^0.1", features = ["catch-unwind",]}
 log = "0.4.14"
+bytes = { version = "1.0" }
 
 flowy-derive = {path = "../flowy-derive"}
 

+ 4 - 3
rust-lib/flowy-observable/src/dart/stream_sender.rs

@@ -1,4 +1,5 @@
 use crate::entities::ObservableSubject;
+use bytes::Bytes;
 use lazy_static::lazy_static;
 use std::{convert::TryInto, sync::RwLock};
 
@@ -21,8 +22,8 @@ impl RustStreamSender {
     fn inner_post(&self, observable_subject: ObservableSubject) -> Result<(), String> {
         match self.isolate {
             Some(ref isolate) => {
-                let bytes: Vec<u8> = observable_subject.try_into().unwrap();
-                isolate.post(bytes);
+                let bytes: Bytes = observable_subject.try_into().unwrap();
+                isolate.post(bytes.to_vec());
                 Ok(())
             },
             None => Err("Isolate is not set".to_owned()),
@@ -39,7 +40,7 @@ impl RustStreamSender {
         }
     }
 
-    pub fn post(_observable_subject: ObservableSubject) -> Result<(), String> {
+    pub fn post(observable_subject: ObservableSubject) -> Result<(), String> {
         #[cfg(feature = "dart")]
         match R2F_STREAM_SENDER.read() {
             Ok(stream) => stream.inner_post(observable_subject),

+ 1 - 0
rust-lib/flowy-sdk/Cargo.toml

@@ -18,6 +18,7 @@ tracing = { version = "0.1" }
 log = "0.4.14"
 futures-core = { version = "0.3", default-features = false }
 color-eyre = { version = "0.5", default-features = false }
+bytes = "1.0"
 
 [dev-dependencies]
 serde = { version = "1.0", features = ["derive"] }

+ 2 - 1
rust-lib/flowy-sdk/src/deps_resolve/user_deps_impl.rs

@@ -1,3 +1,4 @@
+use bytes::Bytes;
 use flowy_dispatch::prelude::{
     DispatchError,
     DispatchFuture,
@@ -23,7 +24,7 @@ impl WorkspaceAction for UserWorkspaceActionImpl {
         _user_id: &str,
     ) -> DispatchFuture<Result<String, UserError>> {
         log::info!("Create user workspace: {:?}", name);
-        let payload: Vec<u8> = CreateWorkspaceRequest {
+        let payload: Bytes = CreateWorkspaceRequest {
             name: name.to_string(),
             desc: desc.to_string(),
         }

+ 2 - 1
rust-lib/flowy-user/src/errors.rs

@@ -1,3 +1,4 @@
+use bytes::Bytes;
 use derive_more::Display;
 use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
 use flowy_dispatch::prelude::{EventResponse, ResponseBuilder};
@@ -114,7 +115,7 @@ impl std::convert::From<flowy_sqlite::Error> for UserError {
 
 impl flowy_dispatch::Error for UserError {
     fn as_response(&self) -> EventResponse {
-        let bytes: Vec<u8> = self.clone().try_into().unwrap();
+        let bytes: Bytes = self.clone().try_into().unwrap();
         ResponseBuilder::Err().data(bytes).build()
     }
 }

+ 2 - 1
rust-lib/flowy-user/src/services/user/user_session.rs

@@ -8,6 +8,7 @@ use crate::{
     },
     sql_tables::{UserTable, UserTableChangeset},
 };
+use bytes::Bytes;
 use flowy_database::{
     query_dsl::*,
     schema::{user_table, user_table::dsl},
@@ -168,7 +169,7 @@ impl UserSession {
 
     pub async fn set_current_workspace(&self, workspace_id: &str) -> Result<(), UserError> {
         let user_id = self.get_user_id()?;
-        let payload: Vec<u8> = UpdateUserRequest::new(&user_id)
+        let payload: Bytes = UpdateUserRequest::new(&user_id)
             .workspace(workspace_id)
             .into_bytes()
             .unwrap();

+ 1 - 1
rust-lib/flowy-workspace/Cargo.toml

@@ -26,7 +26,7 @@ derive_more = {version = "0.99", features = ["display"]}
 bincode = { version = "1.3"}
 unicode-segmentation = "1.7.1"
 tracing = { version = "0.1", features = ["log"] }
-
+bytes = { version = "1.0" }
 [dev-dependencies]
 flowy-test = { path = "../flowy-test" }
 serial_test = "0.5.1"

+ 2 - 1
rust-lib/flowy-workspace/src/errors.rs

@@ -1,3 +1,4 @@
+use bytes::Bytes;
 use derive_more::Display;
 use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
 use flowy_dispatch::prelude::{EventResponse, ResponseBuilder};
@@ -80,7 +81,7 @@ impl std::convert::From<flowy_database::result::Error> for WorkspaceError {
 
 impl flowy_dispatch::Error for WorkspaceError {
     fn as_response(&self) -> EventResponse {
-        let bytes: Vec<u8> = self.clone().try_into().unwrap();
+        let bytes: Bytes = self.clone().try_into().unwrap();
         ResponseBuilder::Err().data(bytes).build()
     }
 }

+ 9 - 2
rust-lib/flowy-workspace/src/observable/observable.rs

@@ -1,6 +1,8 @@
+use bytes::Bytes;
 use flowy_derive::ProtoBuf_Enum;
 use flowy_dispatch::prelude::ToBytes;
 use flowy_observable::{dart::RustStreamSender, entities::ObservableSubject};
+
 const OBSERVABLE_CATEGORY: &'static str = "Workspace";
 
 #[derive(ProtoBuf_Enum, Debug)]
@@ -28,7 +30,7 @@ impl std::default::Default for WorkspaceObservable {
 pub(crate) struct ObservableSender {
     ty: WorkspaceObservable,
     subject_id: String,
-    payload: Option<Vec<u8>>,
+    payload: Option<Bytes>,
 }
 
 impl ObservableSender {
@@ -57,11 +59,16 @@ impl ObservableSender {
             self.ty
         );
 
+        let subject_payload = match self.payload {
+            None => None,
+            Some(bytes) => Some(bytes.to_vec()),
+        };
+
         let subject = ObservableSubject {
             category: OBSERVABLE_CATEGORY.to_string(),
             ty: self.ty as i32,
             subject_id: self.subject_id,
-            subject_payload: self.payload,
+            subject_payload,
         };
         match RustStreamSender::post(subject) {
             Ok(_) => {},