diff --git a/Cargo.lock b/Cargo.lock index 0c30d4a1..3861b26c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,6 +104,15 @@ dependencies = [ "instant", ] +[[package]] +name = "faststr" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c30e8b7deaa08b8ce5efe5d91bf00eee3d20e5e327a1cac209d4f9b2f39f503f" +dependencies = [ + "bytes", +] + [[package]] name = "fixedbitset" version = "0.4.2" @@ -176,18 +185,18 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.137" +version = "0.2.138" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" +checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8" [[package]] name = "linkedbytes" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e0703494cf80f30244b1a85c18fea20e4a4ba32d7d2799ffad1e7774303bf0c" +checksum = "1eb697040b79d318dd14b4219e6764c72ac84b5b07836551ce35b6847427db09" dependencies = [ "bytes", - "smol_str", + "faststr", "tokio", ] @@ -370,9 +379,9 @@ dependencies = [ "async-trait", "bytes", "derivative", + "faststr", "lazy_static", "linkedbytes", - "smol_str", "thiserror", "tokio", ] @@ -384,6 +393,7 @@ dependencies = [ "async-trait", "derivative", "diffy", + "faststr", "fxhash", "heck 0.4.0", "itertools", @@ -400,7 +410,6 @@ dependencies = [ "quote", "salsa", "scoped-tls", - "smol_str", "syn", "tempfile", "tokio", @@ -579,12 +588,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" -[[package]] -name = "serde" -version = "1.0.148" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e53f64bb4ba0191d6d0676e1b141ca55047d83b74f5607e6d8eb88126c52c2dc" - [[package]] name = "sharded-slab" version = "0.1.4" @@ -606,20 +609,11 @@ version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" -[[package]] -name = "smol_str" -version = "0.1.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7475118a28b7e3a2e157ce0131ba8c5526ea96e90ee601d9f6bb2e286a35ab44" -dependencies = [ - "serde", -] - [[package]] name = "syn" -version = "1.0.104" +version = "1.0.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ae548ec36cf198c0ef7710d3c230987c2d6d7bd98ad6edc0274462724c585ce" +checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908" dependencies = [ "proc-macro2", "quote", @@ -671,14 +665,15 @@ dependencies = [ [[package]] name = "tokio" -version = "1.22.0" +version = "1.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d76ce4a75fb488c605c54bf610f221cea8b0dafb53333c1a67e8ee199dcd2ae3" +checksum = "eab6d665857cc6ca78d6e80303a02cea7a7851e85dfbd77cbdc09bd129f1ef46" dependencies = [ "autocfg", "bytes", "memchr", "pin-project-lite", + "windows-sys", ] [[package]] @@ -793,3 +788,60 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" diff --git a/pilota-build/Cargo.toml b/pilota-build/Cargo.toml index 0163dd07..b5daa82f 100644 --- a/pilota-build/Cargo.toml +++ b/pilota-build/Cargo.toml @@ -20,7 +20,6 @@ maintenance = { status = "actively-developed" } pilota-thrift-parser = { path = "../pilota-thrift-parser", version = "0.4" } heck = "0.4" -smol_str = "0.1" syn = "1" normpath = "0.3" fxhash = "0.2" @@ -39,6 +38,7 @@ petgraph = "0.6" # We will switch to the official one when https://github.com/stepancheg/rust-protobuf/pull/646 is fixed. protobuf-parse = { package = "protobuf-parse2", version = "4.0.0-alpha.2" } protobuf = { package = "protobuf2", version = "4.0.0-alpha.2" } +faststr = "0.1" [build-dependencies] itertools = "0.10" diff --git a/pilota-build/src/codegen/mod.rs b/pilota-build/src/codegen/mod.rs index dbe95f8c..fff3433f 100644 --- a/pilota-build/src/codegen/mod.rs +++ b/pilota-build/src/codegen/mod.rs @@ -1,5 +1,6 @@ use std::{collections::HashMap, ops::Deref, sync::Arc}; +use faststr::FastStr; use fxhash::FxHashMap; use heck::ToShoutySnakeCase; use pkg_tree::PkgNode; @@ -30,7 +31,7 @@ pub struct Codegen { backend: B, zero_copy: bool, cx: Arc, - pkgs: FxHashMap, TokenStream>, + pkgs: FxHashMap, TokenStream>, } impl Deref for Codegen { @@ -339,9 +340,9 @@ where let s = &**s; quote! { #s.to_string() } } - (Literal::String(s), CodegenTy::SmolStr) => { + (Literal::String(s), CodegenTy::FastStr) => { let s = &**s; - quote! { ::pilota::SmolStr::new(#s) } + quote! { ::pilota::FastStr::new(#s) } } (Literal::Int(i), CodegenTy::I16) => { let i = *i as i16; @@ -392,7 +393,7 @@ where } } - pub(crate) fn write_mods(&mut self, mods: HashMap, Vec>) { + pub(crate) fn write_mods(&mut self, mods: HashMap, Vec>) { mods.iter().for_each(|(p, def_ids)| { let stream: &mut TokenStream = unsafe { std::mem::transmute(self.pkgs.entry(p.clone()).or_default()) }; @@ -405,7 +406,7 @@ where pub fn link(mut self, ns_name: &str) -> TokenStream { fn write_stream( - pkgs: &mut FxHashMap, TokenStream>, + pkgs: &mut FxHashMap, TokenStream>, stream: &mut TokenStream, nodes: &[PkgNode], ) { diff --git a/pilota-build/src/codegen/pkg_tree.rs b/pilota-build/src/codegen/pkg_tree.rs index 16d2756a..6a39b337 100644 --- a/pilota-build/src/codegen/pkg_tree.rs +++ b/pilota-build/src/codegen/pkg_tree.rs @@ -1,14 +1,15 @@ use std::sync::Arc; +use faststr::FastStr; use itertools::Itertools; #[derive(Debug)] pub struct PkgNode { - pub path: Arc<[smol_str::SmolStr]>, + pub path: Arc<[FastStr]>, pub children: Arc<[PkgNode]>, } -fn from_pkgs(base_path: &[smol_str::SmolStr], pkgs: &[Arc<[smol_str::SmolStr]>]) -> Arc<[PkgNode]> { +fn from_pkgs(base_path: &[FastStr], pkgs: &[Arc<[FastStr]>]) -> Arc<[PkgNode]> { let groups = pkgs.iter().into_group_map_by(|p| p.first().unwrap()); Arc::from_iter(groups.into_iter().map(|(k, v)| { @@ -33,11 +34,11 @@ fn from_pkgs(base_path: &[smol_str::SmolStr], pkgs: &[Arc<[smol_str::SmolStr]>]) } impl PkgNode { - pub fn from_pkgs(pkgs: &[Arc<[smol_str::SmolStr]>]) -> Arc<[PkgNode]> { + pub fn from_pkgs(pkgs: &[Arc<[FastStr]>]) -> Arc<[PkgNode]> { from_pkgs(&[], pkgs) } - pub fn ident(&self) -> smol_str::SmolStr { + pub fn ident(&self) -> FastStr { self.path.last().unwrap().clone() } } diff --git a/pilota-build/src/codegen/protobuf/mod.rs b/pilota-build/src/codegen/protobuf/mod.rs index 7c99ede8..6197f542 100644 --- a/pilota-build/src/codegen/protobuf/mod.rs +++ b/pilota-build/src/codegen/protobuf/mod.rs @@ -51,7 +51,7 @@ impl ProstPlugin { }; } match &ty.kind { - ty::String | ty::SmolStr => quote!(string), + ty::String | ty::FastStr => quote!(string), ty::Bool => quote!(bool), ty::BytesVec | ty::Bytes => quote!(bytes), ty::I32 => quote!(int32), diff --git a/pilota-build/src/codegen/thrift/decode_helper.rs b/pilota-build/src/codegen/thrift/decode_helper.rs index cfa11d9c..e2f18a2d 100644 --- a/pilota-build/src/codegen/thrift/decode_helper.rs +++ b/pilota-build/src/codegen/thrift/decode_helper.rs @@ -41,7 +41,7 @@ impl DecodeHelper { protocol_method!(read_bytes_vec); protocol_method!(read_byte); protocol_method!(read_string); - protocol_method!(read_smolstr); + protocol_method!(read_faststr); protocol_method!(read_list_begin); protocol_method!(read_list_end); protocol_method!(read_set_begin); diff --git a/pilota-build/src/codegen/thrift/ty.rs b/pilota-build/src/codegen/thrift/ty.rs index 848c4713..8a82112e 100644 --- a/pilota-build/src/codegen/thrift/ty.rs +++ b/pilota-build/src/codegen/thrift/ty.rs @@ -10,7 +10,7 @@ use crate::{ impl ThriftBackend { pub(crate) fn ttype(&self, ty: &Ty) -> TokenStream { match &ty.kind { - ty::String | ty::SmolStr => quote! {::pilota::thrift::TType::Binary}, + ty::String | ty::FastStr => quote! {::pilota::thrift::TType::Binary}, ty::Void => quote! {::pilota::thrift::TType::Void}, ty::U8 => quote! {::pilota::thrift::TType::I8}, ty::Bool => quote! {::pilota::thrift::TType::Bool}, @@ -42,8 +42,8 @@ impl ThriftBackend { pub(crate) fn codegen_encode_ty(&self, ty: &Ty, ident: &Ident) -> TokenStream { match &ty.kind { - ty::String => quote! { protocol.write_string(#ident)?; }, - ty::SmolStr => quote! { protocol.write_smolstr(#ident.clone())?; }, + ty::String => quote! { protocol.write_string(&#ident)?; }, + ty::FastStr => quote! { protocol.write_faststr(#ident.clone())?; }, ty::Void => quote! { protocol.write_struct_begin(&*::pilota::thrift::VOID_IDENT)?; protocol.write_struct_end()?; @@ -118,7 +118,7 @@ impl ThriftBackend { pub(crate) fn codegen_ty_size(&self, ty: &Ty, ident: &Ident) -> TokenStream { match &ty.kind { ty::String => quote! { protocol.write_string_len(&#ident) }, - ty::SmolStr => quote! { protocol.write_smolstr_len(#ident) }, + ty::FastStr => quote! { protocol.write_faststr_len(#ident) }, ty::Void => { quote! { protocol.write_struct_begin_len(&*::pilota::thrift::VOID_IDENT) + protocol.write_struct_end_len() } } @@ -206,7 +206,7 @@ impl ThriftBackend { pub(crate) fn codegen_decode_ty(&self, helper: &DecodeHelper, ty: &Ty) -> TokenStream { match &ty.kind { ty::String => helper.codegen_read_string(), - ty::SmolStr => helper.codegen_read_smolstr(), + ty::FastStr => helper.codegen_read_faststr(), ty::Void => { let read_struct_begin = helper.codegen_read_struct_begin(); let read_struct_end = helper.codegen_read_struct_end(); diff --git a/pilota-build/src/middle/context.rs b/pilota-build/src/middle/context.rs index bfdcddc6..4d68a98b 100644 --- a/pilota-build/src/middle/context.rs +++ b/pilota-build/src/middle/context.rs @@ -1,5 +1,6 @@ use std::{collections::HashMap, ops::Deref, path::PathBuf, sync::Arc}; +use faststr::FastStr; use fxhash::{FxHashMap, FxHashSet}; use itertools::Itertools; use normpath::PathExt; @@ -91,7 +92,7 @@ impl Context { item.symbol_name() } - pub fn rust_name(&self, def_id: DefId) -> smol_str::SmolStr { + pub fn rust_name(&self, def_id: DefId) -> FastStr { let node = self.node(def_id).unwrap(); if let Some(name) = self @@ -116,8 +117,8 @@ impl Context { } } - pub fn mod_path(&self, def_id: DefId) -> Arc<[smol_str::SmolStr]> { - fn calc_item_path(cx: &Context, def_id: DefId, segs: &mut Vec) { + pub fn mod_path(&self, def_id: DefId) -> Arc<[FastStr]> { + fn calc_item_path(cx: &Context, def_id: DefId, segs: &mut Vec) { let node = cx.node(def_id).unwrap(); if let Some(parent) = node.parent { calc_item_path(cx, parent, segs) @@ -141,8 +142,8 @@ impl Context { Arc::from(segs) } - pub fn item_path(&self, def_id: DefId) -> Arc<[smol_str::SmolStr]> { - fn calc_item_path(cx: &Context, def_id: DefId, segs: &mut Vec) { + pub fn item_path(&self, def_id: DefId) -> Arc<[FastStr]> { + fn calc_item_path(cx: &Context, def_id: DefId, segs: &mut Vec) { let node = cx.node(def_id).unwrap(); match node.kind { @@ -168,7 +169,7 @@ impl Context { Arc::from(segs) } - fn related_path(&self, p1: &[smol_str::SmolStr], p2: &[smol_str::SmolStr]) -> syn::Path { + fn related_path(&self, p1: &[FastStr], p2: &[FastStr]) -> syn::Path { if p1 == p2 { return syn::Path::from(format_ident!("{}", p2.last().unwrap().as_syn_ident())); } @@ -180,7 +181,7 @@ impl Context { enum Kind { Super, - Ident(smol_str::SmolStr), + Ident(FastStr), } let path = (0..p1.len() - i) .map(|_| Kind::Super) @@ -311,7 +312,7 @@ impl Context { .find(|def_id| &*self.item(**def_id).unwrap().symbol_name() == item_name) .cloned(); if let Some(def_id) = def_id { - collect(&self, def_id, &mut set); + collect(self, def_id, &mut set); } else { println!( "cargo:warning=item `{}` of `{}` not exists", @@ -323,13 +324,13 @@ impl Context { }); input.iter().for_each(|def_id| { - collect(&self, *def_id, &mut set); + collect(self, *def_id, &mut set); }); self.nodes().iter().for_each(|(def_id, node)| { if let NodeKind::Item(item) = &node.kind { if let rir::Item::Const(_) = &**item { - collect(&self, *def_id, &mut set); + collect(self, *def_id, &mut set); } } }); @@ -340,7 +341,7 @@ impl Context { pub(crate) fn collect_pkgs( &mut self, mode: CollectMode, - ) -> HashMap, Vec> { + ) -> HashMap, Vec> { match mode { CollectMode::All => { let files = self.files(); diff --git a/pilota-build/src/middle/ty.rs b/pilota-build/src/middle/ty.rs index c129219e..8a227d47 100644 --- a/pilota-build/src/middle/ty.rs +++ b/pilota-build/src/middle/ty.rs @@ -9,7 +9,7 @@ use crate::{db::RirDatabase, symbol::DefId, tags::TagId}; #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub enum TyKind { String, - SmolStr, + FastStr, Void, U8, Bool, @@ -51,9 +51,9 @@ pub enum AdtKind { #[derive(Hash, PartialEq, Eq, Clone, Debug)] pub enum CodegenTy { - String, - SmolStr, - Str, // static str, + FastStr, + String, // for protobuf + Str, // static str Void, U8, Bool, @@ -80,7 +80,7 @@ impl CodegenTy { pub fn should_lazy_static(&self) -> bool { match self { CodegenTy::String - | CodegenTy::SmolStr + | CodegenTy::FastStr | CodegenTy::LazyStaticRef(_) | CodegenTy::StaticRef(_) | CodegenTy::Vec(_) @@ -98,7 +98,7 @@ impl ToTokens for CodegenTy { fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { match self { CodegenTy::String => tokens.extend(quote! { ::std::string::String }), - CodegenTy::SmolStr => tokens.extend(quote! { ::pilota::SmolStr }), + CodegenTy::FastStr => tokens.extend(quote! { ::pilota::FastStr }), CodegenTy::Str => tokens.extend(quote! { &'static str }), CodegenTy::Void => tokens.extend(quote! { () }), CodegenTy::U8 => tokens.extend(quote! { u8 }), @@ -156,12 +156,6 @@ impl TyKind { } } -pub enum StringRepr { - SmolStr, - String, - Bytes, -} - pub trait TyTransformer { #[inline] fn string(&self) -> CodegenTy { @@ -169,8 +163,8 @@ pub trait TyTransformer { } #[inline] - fn smol_str(&self) -> CodegenTy { - CodegenTy::SmolStr + fn faststr(&self) -> CodegenTy { + CodegenTy::FastStr } #[inline] @@ -270,7 +264,7 @@ pub trait TyTransformer { fn codegen_item_ty(&self, ty: &TyKind) -> CodegenTy { match &ty { String => self.string(), - SmolStr => self.smol_str(), + FastStr => self.faststr(), Void => self.void(), U8 => self.u8(), Bool => self.bool(), @@ -293,6 +287,11 @@ pub trait TyTransformer { } } +pub enum StringRepr { + FastStr, + String, +} + pub(crate) struct DefaultTyTransformer; impl TyTransformer for DefaultTyTransformer {} @@ -306,7 +305,7 @@ impl TyTransformer for ConstTyTransformer { } #[inline] - fn smol_str(&self) -> CodegenTy { + fn faststr(&self) -> CodegenTy { CodegenTy::Str } @@ -359,7 +358,7 @@ pub(crate) trait Folder: Sized { pub(crate) fn fold_ty(f: &mut F, ty: &Ty) -> Ty { let kind = match &ty.kind { - String | SmolStr => TyKind::String, + String | FastStr => TyKind::String, Void => TyKind::Void, U8 => TyKind::U8, Bool => TyKind::Bool, @@ -370,14 +369,14 @@ pub(crate) fn fold_ty(f: &mut F, ty: &Ty) -> Ty { I32 => TyKind::I32, I64 => TyKind::I64, F64 => TyKind::F64, - Vec(ty) => TyKind::Vec(f.fold_ty(&ty).into()), - Set(ty) => TyKind::Set(f.fold_ty(&ty).into()), + Vec(ty) => TyKind::Vec(f.fold_ty(ty).into()), + Set(ty) => TyKind::Set(f.fold_ty(ty).into()), Map(k, v) => TyKind::Map(fold_ty(f, k).into(), fold_ty(f, v).into()), Path(path) => TyKind::Path(path.clone()), UInt32 => TyKind::UInt32, UInt64 => TyKind::UInt64, F32 => TyKind::F32, - Arc(ty) => TyKind::Arc(f.fold_ty(&ty).into()), + Arc(ty) => TyKind::Arc(f.fold_ty(ty).into()), }; Ty { diff --git a/pilota-build/src/parser/protobuf/mod.rs b/pilota-build/src/parser/protobuf/mod.rs index 7c04ee28..c4e7dede 100644 --- a/pilota-build/src/parser/protobuf/mod.rs +++ b/pilota-build/src/parser/protobuf/mod.rs @@ -185,7 +185,7 @@ impl Lower { let mut s = String::new(); parent_messages.iter().for_each(|m| { s.push_str(m); - s.push_str(".") + s.push('.') }); s }, @@ -326,10 +326,10 @@ impl Lower { if nested_items.is_empty() { item } else { - let name = item.name().clone(); + let name = item.name(); nested_items.push(Arc::new(item)); let mut tags = Tags::default(); - tags.insert(PilotaName(name.0.mod_ident().clone())); + tags.insert(PilotaName(name.0.mod_ident())); Item { related_items: Default::default(), tags: Arc::from(tags), @@ -404,7 +404,7 @@ impl Lower { let file_id = *self.files.get(f.name()).unwrap(); - let package = self.str2path(&f.package()); + let package = self.str2path(f.package()); let enums = f.enum_type.iter().map(|e| self.lower_enum(e)); let messages = f @@ -414,7 +414,7 @@ impl Lower { let services = f.service.iter().map(|s| self.lower_service(s)); let f = Arc::from(ir::File { - package: package.clone(), + package, uses: f .dependency .iter() diff --git a/pilota-build/src/parser/thrift/mod.rs b/pilota-build/src/parser/thrift/mod.rs index eeb632cb..c1466b10 100644 --- a/pilota-build/src/parser/thrift/mod.rs +++ b/pilota-build/src/parser/thrift/mod.rs @@ -382,9 +382,7 @@ impl ThriftLower { if let Some(rust_type) = rust_type { if let thrift_parser::Ty::String = &ty { let mut tags = Tags::default(); - if rust_type == "bytes" { - tags.insert(StringRepr::Bytes); - } else if rust_type == "string" { + if rust_type == "string" { tags.insert(StringRepr::String) } return ir::Ty { @@ -526,7 +524,7 @@ impl Lower> for ThriftLower { .unwrap() .trim_end_matches(".thrift") .split('.') - .map(|s| Ident::from(s)) + .map(Ident::from) .collect_vec(), this.lower_include(i), ) diff --git a/pilota-build/src/resolve.rs b/pilota-build/src/resolve.rs index 2c160258..ee5eeb89 100644 --- a/pilota-build/src/resolve.rs +++ b/pilota-build/src/resolve.rs @@ -357,17 +357,16 @@ impl Resolver { fn lower_type(&mut self, ty: &ir::Ty) -> Ty { let kind = match &ty.kind { - ir::TyKind::String => { - if let Some(repr) = ty.tags.get::() { - match repr { - StringRepr::String => ty::String, - StringRepr::Bytes => ty::Bytes, - StringRepr::SmolStr => ty::SmolStr, - } - } else { - ty::SmolStr - } + ir::TyKind::String + if ty + .tags + .get::() + .map(|repr| matches!(repr, StringRepr::String)) + .unwrap_or(false) => + { + ty::String } + ir::TyKind::String => ty::FastStr, ir::TyKind::Void => ty::Void, ir::TyKind::U8 => ty::U8, ir::TyKind::Bool => ty::Bool, diff --git a/pilota-build/src/symbol.rs b/pilota-build/src/symbol.rs index b50bf233..21353b02 100644 --- a/pilota-build/src/symbol.rs +++ b/pilota-build/src/symbol.rs @@ -1,5 +1,6 @@ use std::{fmt::Display, ops::Deref}; +use faststr::FastStr; use heck::{ToShoutySnakeCase, ToSnakeCase, ToUpperCamelCase}; use phf::phf_set; use quote::{format_ident, IdentFragment}; @@ -74,7 +75,7 @@ lazy_static::lazy_static! { } #[derive(Hash, PartialEq, Eq, Clone, Debug)] -pub struct Symbol(pub smol_str::SmolStr); +pub struct Symbol(pub FastStr); impl std::borrow::Borrow for Symbol { fn borrow(&self) -> &str { @@ -92,7 +93,7 @@ impl Deref for Symbol { impl From for Symbol where - T: Into, + T: Into, { fn from(t: T) -> Self { Symbol(t.into()) @@ -153,7 +154,7 @@ impl Display for Ident { impl From for Ident where - T: Into, + T: Into, { fn from(t: T) -> Self { Ident { @@ -163,42 +164,42 @@ where } pub trait IdentName { - fn struct_ident(&self) -> smol_str::SmolStr { + fn struct_ident(&self) -> FastStr { self.upper_camel_ident() } - fn enum_ident(&self) -> smol_str::SmolStr { + fn enum_ident(&self) -> FastStr { self.upper_camel_ident() } - fn mod_ident(&self) -> smol_str::SmolStr { + fn mod_ident(&self) -> FastStr { self.snake_ident() } - fn variant_ident(&self) -> smol_str::SmolStr { + fn variant_ident(&self) -> FastStr { self.upper_camel_ident() } - fn fn_ident(&self) -> smol_str::SmolStr { + fn fn_ident(&self) -> FastStr { self.snake_ident() } - fn field_ident(&self) -> smol_str::SmolStr { + fn field_ident(&self) -> FastStr { self.snake_ident() } - fn const_ident(&self) -> smol_str::SmolStr { + fn const_ident(&self) -> FastStr { self.shouty_snake_case() } - fn trait_ident(&self) -> smol_str::SmolStr { + fn trait_ident(&self) -> FastStr { self.upper_camel_ident() } - fn newtype_ident(&self) -> smol_str::SmolStr { + fn newtype_ident(&self) -> FastStr { self.upper_camel_ident() } - fn upper_camel_ident(&self) -> smol_str::SmolStr; - fn snake_ident(&self) -> smol_str::SmolStr; - fn shouty_snake_case(&self) -> smol_str::SmolStr; + fn upper_camel_ident(&self) -> FastStr; + fn snake_ident(&self) -> FastStr; + fn shouty_snake_case(&self) -> FastStr; fn as_syn_ident(&self) -> syn::Ident; } @@ -215,16 +216,16 @@ fn str2ident(s: &str) -> syn::Ident { } impl IdentName for &str { - fn upper_camel_ident(&self) -> smol_str::SmolStr { + fn upper_camel_ident(&self) -> FastStr { let s = self.to_upper_camel_case(); s.into() } - fn snake_ident(&self) -> smol_str::SmolStr { + fn snake_ident(&self) -> FastStr { self.to_snake_case().into() } - fn shouty_snake_case(&self) -> smol_str::SmolStr { + fn shouty_snake_case(&self) -> FastStr { self.to_shouty_snake_case().into() } @@ -233,16 +234,16 @@ impl IdentName for &str { } } -impl IdentName for smol_str::SmolStr { - fn upper_camel_ident(&self) -> smol_str::SmolStr { +impl IdentName for FastStr { + fn upper_camel_ident(&self) -> FastStr { (&**self).upper_camel_ident() } - fn snake_ident(&self) -> smol_str::SmolStr { + fn snake_ident(&self) -> FastStr { (&**self).snake_ident() } - fn shouty_snake_case(&self) -> smol_str::SmolStr { + fn shouty_snake_case(&self) -> FastStr { (&**self).shouty_snake_case() } diff --git a/pilota-build/src/tags.rs b/pilota-build/src/tags.rs index 70672292..5ae46d4a 100644 --- a/pilota-build/src/tags.rs +++ b/pilota-build/src/tags.rs @@ -7,6 +7,8 @@ use std::{ str::FromStr, }; +use faststr::FastStr; + #[derive(Default, Debug)] pub struct TypeMap(HashMap>); @@ -111,7 +113,7 @@ pub mod thrift { } #[derive(Clone)] -pub struct PilotaName(pub smol_str::SmolStr); +pub struct PilotaName(pub FastStr); pub trait Annotation: FromStr { const KEY: &'static str; @@ -121,7 +123,7 @@ impl FromStr for PilotaName { type Err = std::convert::Infallible; fn from_str(s: &str) -> Result { - Ok(Self(smol_str::SmolStr::new(s))) + Ok(Self(FastStr::new(s))) } } @@ -130,11 +132,11 @@ impl Annotation for PilotaName { } #[derive(Debug)] -pub struct RustType(pub smol_str::SmolStr); +pub struct RustType(pub FastStr); impl PartialEq for RustType { fn eq(&self, other: &str) -> bool { - &self.0 == other + self.0 == other } } @@ -142,7 +144,7 @@ impl FromStr for RustType { type Err = std::convert::Infallible; fn from_str(s: &str) -> Result { - Ok(Self(smol_str::SmolStr::new(s))) + Ok(Self(FastStr::new(s))) } } @@ -151,11 +153,11 @@ impl Annotation for RustType { } #[derive(Debug)] -pub struct RustWrapperArc(pub smol_str::SmolStr); +pub struct RustWrapperArc(pub FastStr); impl PartialEq for RustWrapperArc { fn eq(&self, other: &str) -> bool { - &self.0 == other + self.0 == other } } @@ -163,7 +165,7 @@ impl FromStr for RustWrapperArc { type Err = std::convert::Infallible; fn from_str(s: &str) -> Result { - Ok(Self(smol_str::SmolStr::new(s))) + Ok(Self(FastStr::new(s))) } } diff --git a/pilota-build/test_data/thrift/pilota_name.rs b/pilota-build/test_data/thrift/pilota_name.rs index 086d9518..fc487195 100644 --- a/pilota-build/test_data/thrift/pilota_name.rs +++ b/pilota-build/test_data/thrift/pilota_name.rs @@ -3,7 +3,7 @@ pub mod pilota_name { pub mod pilota_name { #[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)] pub struct Test2 { - pub id: ::pilota::SmolStr, + pub id: ::pilota::FastStr, } #[::async_trait::async_trait] impl ::pilota::thrift::Message for Test2 { @@ -16,7 +16,7 @@ pub mod pilota_name { { let value = &self.id; protocol.write_field_begin(::pilota::thrift::TType::Binary, 1i16)?; - protocol.write_smolstr(value.clone())?; + protocol.write_faststr(value.clone())?; protocol.write_field_end()?; } protocol.write_field_stop()?; @@ -36,7 +36,7 @@ pub mod pilota_name { let field_id = field_ident.id; match field_id { Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - id = Some(protocol.read_smolstr()?); + id = Some(protocol.read_faststr()?); } _ => { protocol.skip(field_ident.field_type)?; @@ -71,7 +71,7 @@ pub mod pilota_name { let field_id = field_ident.id; match field_id { Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - id = Some(protocol.read_smolstr().await?); + id = Some(protocol.read_faststr().await?); } _ => { protocol.skip(field_ident.field_type).await?; @@ -102,7 +102,7 @@ pub mod pilota_name { name: Some("ID"), field_type: ::pilota::thrift::TType::Binary, id: Some(1i16), - }) + protocol.write_smolstr_len(value) + }) + protocol.write_faststr_len(value) + protocol.write_field_end_len() } + protocol.write_field_stop_len() @@ -112,8 +112,8 @@ pub mod pilota_name { pub const LANG_ID: &'static str = "id"; #[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)] pub struct Test1 { - pub id: ::pilota::SmolStr, - pub hello: ::pilota::SmolStr, + pub id: ::pilota::FastStr, + pub hello: ::pilota::FastStr, } #[::async_trait::async_trait] impl ::pilota::thrift::Message for Test1 { @@ -126,13 +126,13 @@ pub mod pilota_name { { let value = &self.id; protocol.write_field_begin(::pilota::thrift::TType::Binary, 1i16)?; - protocol.write_smolstr(value.clone())?; + protocol.write_faststr(value.clone())?; protocol.write_field_end()?; } { let value = &self.hello; protocol.write_field_begin(::pilota::thrift::TType::Binary, 2i16)?; - protocol.write_smolstr(value.clone())?; + protocol.write_faststr(value.clone())?; protocol.write_field_end()?; } protocol.write_field_stop()?; @@ -153,10 +153,10 @@ pub mod pilota_name { let field_id = field_ident.id; match field_id { Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - id = Some(protocol.read_smolstr()?); + id = Some(protocol.read_faststr()?); } Some(2i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - hello = Some(protocol.read_smolstr()?); + hello = Some(protocol.read_faststr()?); } _ => { protocol.skip(field_ident.field_type)?; @@ -205,10 +205,10 @@ pub mod pilota_name { let field_id = field_ident.id; match field_id { Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - id = Some(protocol.read_smolstr().await?); + id = Some(protocol.read_faststr().await?); } Some(2i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - hello = Some(protocol.read_smolstr().await?); + hello = Some(protocol.read_faststr().await?); } _ => { protocol.skip(field_ident.field_type).await?; @@ -252,7 +252,7 @@ pub mod pilota_name { name: Some("ID"), field_type: ::pilota::thrift::TType::Binary, id: Some(1i16), - }) + protocol.write_smolstr_len(value) + }) + protocol.write_faststr_len(value) + protocol.write_field_end_len() } + { @@ -261,7 +261,7 @@ pub mod pilota_name { name: Some("Id"), field_type: ::pilota::thrift::TType::Binary, id: Some(2i16), - }) + protocol.write_smolstr_len(value) + }) + protocol.write_faststr_len(value) + protocol.write_field_end_len() } + protocol.write_field_stop_len() diff --git a/pilota-build/test_data/thrift/self_kw.rs b/pilota-build/test_data/thrift/self_kw.rs index 41276d55..4995d994 100644 --- a/pilota-build/test_data/thrift/self_kw.rs +++ b/pilota-build/test_data/thrift/self_kw.rs @@ -68,7 +68,7 @@ pub mod self_kw { } #[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)] pub struct A { - pub r#type: ::pilota::SmolStr, + pub r#type: ::pilota::FastStr, } #[::async_trait::async_trait] impl ::pilota::thrift::Message for A { @@ -81,7 +81,7 @@ pub mod self_kw { { let value = &self.r#type; protocol.write_field_begin(::pilota::thrift::TType::Binary, 1i16)?; - protocol.write_smolstr(value.clone())?; + protocol.write_faststr(value.clone())?; protocol.write_field_end()?; } protocol.write_field_stop()?; @@ -101,7 +101,7 @@ pub mod self_kw { let field_id = field_ident.id; match field_id { Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - r#type = Some(protocol.read_smolstr()?); + r#type = Some(protocol.read_faststr()?); } _ => { protocol.skip(field_ident.field_type)?; @@ -136,7 +136,7 @@ pub mod self_kw { let field_id = field_ident.id; match field_id { Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - r#type = Some(protocol.read_smolstr().await?); + r#type = Some(protocol.read_faststr().await?); } _ => { protocol.skip(field_ident.field_type).await?; @@ -166,7 +166,7 @@ pub mod self_kw { name: Some("type"), field_type: ::pilota::thrift::TType::Binary, id: Some(1i16), - }) + protocol.write_smolstr_len(value) + }) + protocol.write_faststr_len(value) + protocol.write_field_end_len() } + protocol.write_field_stop_len() diff --git a/pilota-build/test_data/thrift/string_bytes.rs b/pilota-build/test_data/thrift/string.rs similarity index 66% rename from pilota-build/test_data/thrift/string_bytes.rs rename to pilota-build/test_data/thrift/string.rs index cf46e29d..bccf53d9 100644 --- a/pilota-build/test_data/thrift/string_bytes.rs +++ b/pilota-build/test_data/thrift/string.rs @@ -1,10 +1,9 @@ -pub mod string_bytes { +pub mod string { #![allow(warnings, clippy::all)] - pub mod string_bytes { + pub mod string { #[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)] pub struct A { - pub smol: ::pilota::SmolStr, - pub bytes: ::pilota::Bytes, + pub faststr: ::pilota::FastStr, pub string: ::std::string::String, } #[::async_trait::async_trait] @@ -16,21 +15,15 @@ pub mod string_bytes { let struct_ident = ::pilota::thrift::TStructIdentifier { name: "A" }; protocol.write_struct_begin(&struct_ident)?; { - let value = &self.smol; + let value = &self.faststr; protocol.write_field_begin(::pilota::thrift::TType::Binary, 1i16)?; - protocol.write_smolstr(value.clone())?; - protocol.write_field_end()?; - } - { - let value = &self.bytes; - protocol.write_field_begin(::pilota::thrift::TType::Binary, 2i16)?; - protocol.write_bytes(value.clone())?; + protocol.write_faststr(value.clone())?; protocol.write_field_end()?; } { let value = &self.string; - protocol.write_field_begin(::pilota::thrift::TType::Binary, 3i16)?; - protocol.write_string(value)?; + protocol.write_field_begin(::pilota::thrift::TType::Binary, 2i16)?; + protocol.write_string(&value)?; protocol.write_field_end()?; } protocol.write_field_stop()?; @@ -40,8 +33,7 @@ pub mod string_bytes { fn decode( protocol: &mut T, ) -> ::std::result::Result { - let mut smol = None; - let mut bytes = None; + let mut faststr = None; let mut string = None; protocol.read_struct_begin()?; loop { @@ -52,12 +44,9 @@ pub mod string_bytes { let field_id = field_ident.id; match field_id { Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - smol = Some(protocol.read_smolstr()?); + faststr = Some(protocol.read_faststr()?); } Some(2i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - bytes = Some(protocol.read_bytes()?); - } - Some(3i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { string = Some(protocol.read_string()?); } _ => { @@ -67,23 +56,13 @@ pub mod string_bytes { protocol.read_field_end()?; } protocol.read_struct_end()?; - let smol = if let Some(smol) = smol { - smol - } else { - return Err(::pilota::thrift::Error::Protocol( - ::pilota::thrift::ProtocolError::new( - ::pilota::thrift::ProtocolErrorKind::InvalidData, - "field smol is required".to_string(), - ), - )); - }; - let bytes = if let Some(bytes) = bytes { - bytes + let faststr = if let Some(faststr) = faststr { + faststr } else { return Err(::pilota::thrift::Error::Protocol( ::pilota::thrift::ProtocolError::new( ::pilota::thrift::ProtocolErrorKind::InvalidData, - "field bytes is required".to_string(), + "field faststr is required".to_string(), ), )); }; @@ -98,8 +77,7 @@ pub mod string_bytes { )); }; let data = Self { - smol: smol, - bytes: bytes, + faststr: faststr, string: string, }; Ok(data) @@ -107,8 +85,7 @@ pub mod string_bytes { async fn decode_async( protocol: &mut ::pilota::thrift::TAsyncBinaryProtocol, ) -> ::std::result::Result { - let mut smol = None; - let mut bytes = None; + let mut faststr = None; let mut string = None; protocol.read_struct_begin().await?; loop { @@ -119,12 +96,9 @@ pub mod string_bytes { let field_id = field_ident.id; match field_id { Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - smol = Some(protocol.read_smolstr().await?); + faststr = Some(protocol.read_faststr().await?); } Some(2i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - bytes = Some(protocol.read_bytes().await?); - } - Some(3i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { string = Some(protocol.read_string().await?); } _ => { @@ -134,23 +108,13 @@ pub mod string_bytes { protocol.read_field_end().await?; } protocol.read_struct_end().await?; - let smol = if let Some(smol) = smol { - smol + let faststr = if let Some(faststr) = faststr { + faststr } else { return Err(::pilota::thrift::Error::Protocol( ::pilota::thrift::ProtocolError::new( ::pilota::thrift::ProtocolErrorKind::InvalidData, - "field smol is required".to_string(), - ), - )); - }; - let bytes = if let Some(bytes) = bytes { - bytes - } else { - return Err(::pilota::thrift::Error::Protocol( - ::pilota::thrift::ProtocolError::new( - ::pilota::thrift::ProtocolErrorKind::InvalidData, - "field bytes is required".to_string(), + "field faststr is required".to_string(), ), )); }; @@ -165,8 +129,7 @@ pub mod string_bytes { )); }; let data = Self { - smol: smol, - bytes: bytes, + faststr: faststr, string: string, }; Ok(data) @@ -174,21 +137,12 @@ pub mod string_bytes { fn size(&self, protocol: &mut T) -> usize { protocol.write_struct_begin_len(&::pilota::thrift::TStructIdentifier { name: "A" }) + { - let value = &self.smol; + let value = &self.faststr; protocol.write_field_begin_len(&::pilota::thrift::TFieldIdentifier { - name: Some("smol"), + name: Some("faststr"), field_type: ::pilota::thrift::TType::Binary, id: Some(1i16), - }) + protocol.write_smolstr_len(value) - + protocol.write_field_end_len() - } - + { - let value = &self.bytes; - protocol.write_field_begin_len(&::pilota::thrift::TFieldIdentifier { - name: Some("bytes"), - field_type: ::pilota::thrift::TType::Binary, - id: Some(2i16), - }) + protocol.write_bytes_len(value) + }) + protocol.write_faststr_len(value) + protocol.write_field_end_len() } + { @@ -196,7 +150,7 @@ pub mod string_bytes { protocol.write_field_begin_len(&::pilota::thrift::TFieldIdentifier { name: Some("string"), field_type: ::pilota::thrift::TType::Binary, - id: Some(3i16), + id: Some(2i16), }) + protocol.write_string_len(&value) + protocol.write_field_end_len() } diff --git a/pilota-build/test_data/thrift/string.thrift b/pilota-build/test_data/thrift/string.thrift new file mode 100644 index 00000000..8894e584 --- /dev/null +++ b/pilota-build/test_data/thrift/string.thrift @@ -0,0 +1,4 @@ +struct A { + 1: required string faststr, + 2: required string string(pilota.rust_type = "string"), +} \ No newline at end of file diff --git a/pilota-build/test_data/thrift/string_bytes.thrift b/pilota-build/test_data/thrift/string_bytes.thrift deleted file mode 100644 index 6ef1a044..00000000 --- a/pilota-build/test_data/thrift/string_bytes.thrift +++ /dev/null @@ -1,5 +0,0 @@ -struct A { - 1: required string smol, - 2: required string bytes(pilota.rust_type = "bytes"), - 3: required string string(pilota.rust_type = "string"), -} \ No newline at end of file diff --git a/pilota-build/test_data/thrift/wrapper_arc.rs b/pilota-build/test_data/thrift/wrapper_arc.rs index 692d700f..2c93a76d 100644 --- a/pilota-build/test_data/thrift/wrapper_arc.rs +++ b/pilota-build/test_data/thrift/wrapper_arc.rs @@ -3,7 +3,7 @@ pub mod wrapper_arc { pub mod wrapper_arc { #[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)] pub struct Test { - pub id: ::std::sync::Arc<::pilota::SmolStr>, + pub id: ::std::sync::Arc<::pilota::FastStr>, } #[::async_trait::async_trait] impl ::pilota::thrift::Message for Test { @@ -16,7 +16,7 @@ pub mod wrapper_arc { { let value = &self.id; protocol.write_field_begin(::pilota::thrift::TType::Binary, 1i16)?; - protocol.write_smolstr(value.clone())?; + protocol.write_faststr(value.clone())?; protocol.write_field_end()?; } protocol.write_field_stop()?; @@ -36,7 +36,7 @@ pub mod wrapper_arc { let field_id = field_ident.id; match field_id { Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - id = Some(protocol.read_smolstr()?); + id = Some(protocol.read_faststr()?); } _ => { protocol.skip(field_ident.field_type)?; @@ -71,7 +71,7 @@ pub mod wrapper_arc { let field_id = field_ident.id; match field_id { Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Binary => { - id = Some(protocol.read_smolstr().await?); + id = Some(protocol.read_faststr().await?); } _ => { protocol.skip(field_ident.field_type).await?; @@ -102,7 +102,7 @@ pub mod wrapper_arc { name: Some("ID"), field_type: ::pilota::thrift::TType::Binary, id: Some(1i16), - }) + protocol.write_smolstr_len(value) + }) + protocol.write_faststr_len(value) + protocol.write_field_end_len() } + protocol.write_field_stop_len() @@ -114,7 +114,7 @@ pub mod wrapper_arc { #[derive(Clone, PartialEq)] pub enum TestServiceTestResult { #[derivative(Default)] - Ok(::pilota::SmolStr), + Ok(::pilota::FastStr), } #[::async_trait::async_trait] impl ::pilota::thrift::Message for TestServiceTestResult { @@ -128,7 +128,7 @@ pub mod wrapper_arc { match self { TestServiceTestResult::Ok(ref value) => { protocol.write_field_begin(::pilota::thrift::TType::Binary, 0i16)?; - protocol.write_smolstr(value.clone())?; + protocol.write_faststr(value.clone())?; protocol.write_field_end()?; } } @@ -150,7 +150,7 @@ pub mod wrapper_arc { match field_id { Some(0i16) => { if ret.is_none() { - ret = Some(TestServiceTestResult::Ok(protocol.read_smolstr()?)); + ret = Some(TestServiceTestResult::Ok(protocol.read_faststr()?)); } else { return Err(::pilota::thrift::new_protocol_error( ::pilota::thrift::ProtocolErrorKind::InvalidData, @@ -189,7 +189,7 @@ pub mod wrapper_arc { Some(0i16) => { if ret.is_none() { ret = - Some(TestServiceTestResult::Ok(protocol.read_smolstr().await?)); + Some(TestServiceTestResult::Ok(protocol.read_faststr().await?)); } else { return Err(::pilota::thrift::new_protocol_error( ::pilota::thrift::ProtocolErrorKind::InvalidData, @@ -222,7 +222,7 @@ pub mod wrapper_arc { name: Some("Ok"), field_type: ::pilota::thrift::TType::Binary, id: Some(0i16), - }) + protocol.write_smolstr_len(value) + }) + protocol.write_faststr_len(value) + protocol.write_field_end_len() } } + protocol.write_field_stop_len() diff --git a/pilota/Cargo.toml b/pilota/Cargo.toml index 7725f3a9..6e9d5eea 100644 --- a/pilota/Cargo.toml +++ b/pilota/Cargo.toml @@ -23,8 +23,8 @@ async-trait = "0.1" async-recursion = "1" tokio = { version = "1", features = ["io-util"] } lazy_static = "1" -linkedbytes = { version = "0.1", features = ["smolstr"] } +linkedbytes = { version = "0.1", features = ["faststr"] } derivative = "2" anyhow = "1" thiserror = "1" -smol_str = "0.1" +faststr = "0.1" diff --git a/pilota/src/lib.rs b/pilota/src/lib.rs index 45fce523..7d097432 100644 --- a/pilota/src/lib.rs +++ b/pilota/src/lib.rs @@ -10,8 +10,8 @@ pub use async_recursion; pub use async_trait; pub use bytes::Bytes; pub use derivative; +pub use faststr::FastStr; pub use lazy_static; -pub use smol_str::SmolStr; pub use thiserror::Error as ThisError; pub use tokio::io::AsyncRead; diff --git a/pilota/src/thrift/binary.rs b/pilota/src/thrift/binary.rs index c9a0368a..8eb49307 100644 --- a/pilota/src/thrift/binary.rs +++ b/pilota/src/thrift/binary.rs @@ -1,9 +1,9 @@ use std::{convert::TryInto, str}; use bytes::{Bytes, BytesMut}; +use faststr::FastStr; use lazy_static::__Deref; use linkedbytes::LinkedBytes; -use smol_str::SmolStr; use tokio::io::{AsyncRead, AsyncReadExt}; use super::{ @@ -53,7 +53,7 @@ fn field_type_from_u8(ttype: u8) -> Result { impl TLengthProtocol for TBinaryProtocol { #[inline] fn write_message_begin_len(&mut self, identifier: &TMessageIdentifier) -> usize { - self.write_i32_len(0) + self.write_string_len(&identifier.name) + self.write_i32_len(0) + self.write_i32_len(0) + self.write_faststr_len(&identifier.name) + self.write_i32_len(0) } #[inline] @@ -134,13 +134,12 @@ impl TLengthProtocol for TBinaryProtocol { 8 } - #[inline] fn write_string_len(&mut self, s: &str) -> usize { self.write_i32_len(0) + s.len() } #[inline] - fn write_smolstr_len(&mut self, s: &SmolStr) -> usize { + fn write_faststr_len(&mut self, s: &FastStr) -> usize { if self.zero_copy && s.len() >= ZERO_COPY_THRESHOLD { self.zero_copy_len += s.len(); } @@ -201,7 +200,7 @@ impl TOutputProtocol for TBinaryProtocol<&mut BytesMut> { let msg_type_u8: u8 = identifier.message_type.into(); let version = (VERSION_1 | msg_type_u8 as u32) as i32; self.write_i32(version)?; - self.write_string(&identifier.name)?; + self.write_faststr(identifier.name.clone())?; self.write_i32(identifier.sequence_number)?; Ok(()) } @@ -308,9 +307,9 @@ impl TOutputProtocol for TBinaryProtocol<&mut BytesMut> { } #[inline] - fn write_smolstr(&mut self, s: SmolStr) -> Result<(), Error> { + fn write_faststr(&mut self, s: FastStr) -> Result<(), Error> { self.write_i32(s.len() as i32)?; - self.trans.write_slice(s.as_bytes())?; + self.trans.write_slice(s.as_ref())?; Ok(()) } @@ -381,7 +380,7 @@ impl TOutputProtocol for TBinaryProtocol<&mut LinkedBytes> { let msg_type_u8: u8 = identifier.message_type.into(); let version = (VERSION_1 | msg_type_u8 as u32) as i32; self.write_i32(version)?; - self.write_string(&identifier.name)?; + self.write_faststr(identifier.name.clone())?; self.write_i32(identifier.sequence_number)?; Ok(()) } @@ -490,15 +489,14 @@ impl TOutputProtocol for TBinaryProtocol<&mut LinkedBytes> { self.trans.bytes_mut().write_slice(s.as_bytes())?; Ok(()) } - #[inline] - fn write_smolstr(&mut self, s: SmolStr) -> Result<(), Error> { + fn write_faststr(&mut self, s: FastStr) -> Result<(), Error> { self.write_i32(s.len() as i32)?; if self.zero_copy && s.len() >= ZERO_COPY_THRESHOLD { - self.trans.insert_smolstr(s); + self.trans.insert_faststr(s); return Ok(()); } - self.trans.bytes_mut().write_slice(s.as_bytes())?; + self.trans.bytes_mut().write_slice(s.as_ref())?; Ok(()) } @@ -599,7 +597,7 @@ where )); } - let name = self.read_smolstr().await?; + let name = self.read_faststr().await?; let sequence_number = self.read_i32().await?; Ok(TMessageIdentifier::new(name, message_type, sequence_number)) @@ -620,8 +618,8 @@ where } #[inline] - pub async fn read_smolstr(&mut self) -> Result { - self.read_string().await.map(smol_str::SmolStr::new) + pub async fn read_faststr(&mut self) -> Result { + self.read_string().await.map(FastStr::from) } #[inline] @@ -855,7 +853,7 @@ impl TInputProtocol for TBinaryProtocol<&mut BytesMut> { )); } - let name = self.read_smolstr()?; + let name = self.read_faststr()?; let sequence_number = self.read_i32()?; Ok(TMessageIdentifier::new(name, message_type, sequence_number)) @@ -954,10 +952,13 @@ impl TInputProtocol for TBinaryProtocol<&mut BytesMut> { } #[inline] - fn read_smolstr(&mut self) -> Result { - let len = self.trans.read_i32()?; - let bytes = self.trans.split_to(len as usize); - unsafe { Ok(SmolStr::new(str::from_utf8_unchecked(bytes.deref()))) } + fn read_faststr(&mut self) -> Result { + let len = self.trans.read_i32()? as usize; + let bytes = self.trans.split_to(len); + if len >= ZERO_COPY_THRESHOLD { + unsafe { return Ok(FastStr::from_bytes_mut_unchecked(bytes)) }; + } + unsafe { Ok(FastStr::new(str::from_utf8_unchecked(bytes.deref()))) } } #[inline] diff --git a/pilota/src/thrift/mod.rs b/pilota/src/thrift/mod.rs index 2e78713b..e89f9efd 100644 --- a/pilota/src/thrift/mod.rs +++ b/pilota/src/thrift/mod.rs @@ -6,7 +6,7 @@ use std::{ops::Deref, sync::Arc}; use bytes::{Buf, BufMut, Bytes}; pub use error::*; -use smol_str::SmolStr; +use faststr::FastStr; use tokio::io::AsyncRead; pub use self::binary::TAsyncBinaryProtocol; @@ -107,8 +107,8 @@ pub trait TInputProtocol { fn read_double(&mut self) -> Result; /// Read a fixed-length string (not null terminated). fn read_string(&mut self) -> Result; - /// Read a smolstr. - fn read_smolstr(&mut self) -> Result; + /// Read a faststr. + fn read_faststr(&mut self) -> Result; /// Read the beginning of a list. fn read_list_begin(&mut self) -> Result; /// Read the end of a list. @@ -239,7 +239,7 @@ pub trait TLengthProtocol { fn write_string_len(&mut self, s: &str) -> usize; - fn write_smolstr_len(&mut self, s: &SmolStr) -> usize; + fn write_faststr_len(&mut self, s: &FastStr) -> usize; fn write_list_begin_len(&mut self, identifier: &TListIdentifier) -> usize; @@ -300,8 +300,8 @@ pub trait TOutputProtocol { fn write_double(&mut self, d: f64) -> Result<(), Error>; /// Write a fixed-length string. fn write_string(&mut self, s: &str) -> Result<(), Error>; - /// Write a fixed-length smolstr. - fn write_smolstr(&mut self, s: SmolStr) -> Result<(), Error>; + /// Write a fixed-length faststr. + fn write_faststr(&mut self, s: FastStr) -> Result<(), Error>; /// Write the beginning of a list. fn write_list_begin(&mut self, identifier: &TListIdentifier) -> Result<(), Error>; /// Write the end of a list. @@ -432,7 +432,7 @@ impl From for u8 { #[derive(Clone, Debug, Eq, PartialEq)] pub struct TMessageIdentifier { /// Service call the message is associated with. - pub name: smol_str::SmolStr, + pub name: FastStr, /// Message type. pub message_type: TMessageType, /// Ordered sequence number identifying the message. @@ -443,7 +443,7 @@ impl TMessageIdentifier { /// Create a `TMessageIdentifier` for a Thrift service-call named `name` /// with message type `message_type` and sequence number `sequence_number`. pub fn new( - name: smol_str::SmolStr, + name: FastStr, message_type: TMessageType, sequence_number: i32, ) -> TMessageIdentifier {