Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
replace vec<u8> with Bytes
appflowy2ebe92250721
diff --git a/rust-lib/dart-ffi/Cargo.toml b/rust-lib/dart-ffi/Cargo.tomlindex af0257627e..3132ea54aa 100644--- a/rust-lib/dart-ffi/Cargo.toml+++ b/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"}diff --git a/rust-lib/dart-ffi/src/lib.rs b/rust-lib/dart-ffi/src/lib.rsindex c522c0443a..03122f7519 100644--- a/rust-lib/dart-ffi/src/lib.rs+++ b/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{diff --git a/rust-lib/dart-ffi/src/model/ffi_request.rs b/rust-lib/dart-ffi/src/model/ffi_request.rsindex e9c2295d47..57a53bdd3b 100644--- a/rust-lib/dart-ffi/src/model/ffi_request.rs+++ b/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}diff --git a/rust-lib/dart-ffi/src/model/ffi_response.rs b/rust-lib/dart-ffi/src/model/ffi_response.rsindex 0a16bc4a83..8630d7e84f 100644--- a/rust-lib/dart-ffi/src/model/ffi_response.rs+++ b/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![],};diff --git a/rust-lib/flowy-derive/src/proto_buf/deserialize.rs b/rust-lib/flowy-derive/src/proto_buf/deserialize.rsindex 9a40b30cd7..5bcc19b80d 100644--- a/rust-lib/flowy-derive/src/proto_buf/deserialize.rs+++ b/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)diff --git a/rust-lib/flowy-derive/src/proto_buf/serialize.rs b/rust-lib/flowy-derive/src/proto_buf/serialize.rsindex 6ebba93212..ca85b480c8 100644--- a/rust-lib/flowy-derive/src/proto_buf/serialize.rs+++ b/rust-lib/flowy-derive/src/proto_buf/serialize.rs@@ -17,14 +17,14 @@ pub fn make_se_token_stream(ctxt: &Ctxt, ast: &ASTContainer) -> Option<TokenStrelet 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)) }}}diff --git a/rust-lib/flowy-dispatch/Cargo.toml b/rust-lib/flowy-dispatch/Cargo.tomlindex ed459fe902..e0cba0a5b7 100644--- a/rust-lib/flowy-dispatch/Cargo.toml+++ b/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"diff --git a/rust-lib/flowy-dispatch/src/byte_trait.rs b/rust-lib/flowy-dispatch/src/byte_trait.rsindex 6198cbef0f..084290f5c2 100644--- a/rust-lib/flowy-dispatch/src/byte_trait.rs+++ b/rust-lib/flowy-dispatch/src/byte_trait.rs@@ -1,14 +1,16 @@+use bytes::Bytes;+// To bytespub 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 Twhere- 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 TwhereT: 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 bytespub 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 Twhere// 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 TwhereT: 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)),diff --git a/rust-lib/flowy-dispatch/src/data.rs b/rust-lib/flowy-dispatch/src/data.rsindex 6539a52769..4265c56ba9 100644--- a/rust-lib/flowy-dispatch/src/data.rs+++ b/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 @@ wherefn 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)) }}diff --git a/rust-lib/flowy-dispatch/src/errors/errors.rs b/rust-lib/flowy-dispatch/src/errors/errors.rsindex 2335bde4b9..b0ad881153 100644--- a/rust-lib/flowy-dispatch/src/errors/errors.rs+++ b/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())}diff --git a/rust-lib/flowy-dispatch/src/request/payload.rs b/rust-lib/flowy-dispatch/src/request/payload.rsindex 374d3af999..34560c7346 100644--- a/rust-lib/flowy-dispatch/src/request/payload.rs+++ b/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 {diff --git a/rust-lib/flowy-document/Cargo.toml b/rust-lib/flowy-document/Cargo.tomlindex 17199ba162..7a89ee7176 100644--- a/rust-lib/flowy-document/Cargo.toml+++ b/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" }\ No newline at end of filediff --git a/rust-lib/flowy-document/src/errors.rs b/rust-lib/flowy-document/src/errors.rsindex 3e1ef74860..37cd1ff797 100644--- a/rust-lib/flowy-document/src/errors.rs+++ b/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()}}diff --git a/rust-lib/flowy-infra/Cargo.toml b/rust-lib/flowy-infra/Cargo.tomlindex 74d36e0f37..0173f3863b 100644--- a/rust-lib/flowy-infra/Cargo.toml+++ b/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"\ No newline at end of file+chrono = "0.4.19"+bytes = { version = "1.0" }\ No newline at end of filediff --git a/rust-lib/flowy-observable/Cargo.toml b/rust-lib/flowy-observable/Cargo.tomlindex 0fca6d8391..0ee0c9ff37 100644--- a/rust-lib/flowy-observable/Cargo.toml+++ b/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"}diff --git a/rust-lib/flowy-observable/src/dart/stream_sender.rs b/rust-lib/flowy-observable/src/dart/stream_sender.rsindex 05f14cb06d..3a8d019ff4 100644--- a/rust-lib/flowy-observable/src/dart/stream_sender.rs+++ b/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),diff --git a/rust-lib/flowy-sdk/Cargo.toml b/rust-lib/flowy-sdk/Cargo.tomlindex 333cca27d9..25c4e8e187 100644--- a/rust-lib/flowy-sdk/Cargo.toml+++ b/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"] }diff --git a/rust-lib/flowy-sdk/src/deps_resolve/user_deps_impl.rs b/rust-lib/flowy-sdk/src/deps_resolve/user_deps_impl.rsindex 248e18ccf1..11dc0f58b0 100644--- a/rust-lib/flowy-sdk/src/deps_resolve/user_deps_impl.rs+++ b/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(),}diff --git a/rust-lib/flowy-user/src/errors.rs b/rust-lib/flowy-user/src/errors.rsindex 793aeedd97..7e971721c4 100644--- a/rust-lib/flowy-user/src/errors.rs+++ b/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()}}diff --git a/rust-lib/flowy-user/src/services/user/user_session.rs b/rust-lib/flowy-user/src/services/user/user_session.rsindex 62561a3b9f..cec404cfce 100644--- a/rust-lib/flowy-user/src/services/user/user_session.rs+++ b/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();diff --git a/rust-lib/flowy-workspace/Cargo.toml b/rust-lib/flowy-workspace/Cargo.tomlindex e548bb000f..b457b5ad49 100644--- a/rust-lib/flowy-workspace/Cargo.toml+++ b/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"\ No newline at end of filediff --git a/rust-lib/flowy-workspace/src/errors.rs b/rust-lib/flowy-workspace/src/errors.rsindex de1f22222a..c7560d364d 100644--- a/rust-lib/flowy-workspace/src/errors.rs+++ b/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()}}diff --git a/rust-lib/flowy-workspace/src/observable/observable.rs b/rust-lib/flowy-workspace/src/observable/observable.rsindex 82db0f5f99..a556884403 100644--- a/rust-lib/flowy-workspace/src/observable/observable.rs+++ b/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(_) => {},