From 1f10665c094993a02be54cbfcdb5d1d011162b35 Mon Sep 17 00:00:00 2001 From: Geoffry Song Date: Sun, 11 Feb 2024 20:13:47 -0800 Subject: [PATCH] Cleanup uses_sso --- pb-jelly-gen/codegen/codegen.py | 55 ++++++++++----------------------- 1 file changed, 16 insertions(+), 39 deletions(-) diff --git a/pb-jelly-gen/codegen/codegen.py b/pb-jelly-gen/codegen/codegen.py index 392f887..4e827cb 100755 --- a/pb-jelly-gen/codegen/codegen.py +++ b/pb-jelly-gen/codegen/codegen.py @@ -737,7 +737,6 @@ def __init__( self.indentation = 0 self.content = StringIO() self.is_proto3 = proto_file and proto_file.syntax == "proto3" - self.uses_sso = False if proto_file and proto_file.options.Extensions[extensions_pb2.serde_derive]: self.derive_serde = True else: @@ -787,13 +786,7 @@ def write_comments(self, sci_loc: Optional[SourceCodeInfo.Location]) -> None: def rust_type( self, msg_type: DescriptorProto, field: FieldDescriptorProto ) -> RustType: - rust_type = RustType(self.ctx, self.proto_file, msg_type, field) - - # checks if any of our types use a small string optimization - if not self.uses_sso and rust_type.is_small_string_optimization(): - self.uses_sso = True - - return rust_type + return RustType(self.ctx, self.proto_file, msg_type, field) def gen_closed_enum( self, @@ -1799,6 +1792,12 @@ def calc_impls( (impls_eq, impls_copy) = (False, False) self.extra_crates[crate].add("bytes") may_use_grpc_slices = True + elif ( + typ == FieldDescriptorProto.TYPE_STRING + and field.options.Extensions[extensions_pb2.sso] + ): + impls_copy = False + self.extra_crates[crate].add("compact_str") elif typ in PRIMITIVE_TYPES: if not PRIMITIVE_TYPES[typ][1]: impls_eq = False @@ -1813,12 +1812,9 @@ def calc_impls( ]: assert field_type.typ.options.Extensions[ extensions_pb2.preserve_unrecognized - ], ( - "%s preserves unrecognized but child message %s does not" - % ( - msg_type.proto_name(), - field.type, - ) + ], "%s preserves unrecognized but child message %s does not" % ( + msg_type.proto_name(), + field.type, ) if field.type_name not in types: field_impls = self.impls_by_msg[field.type_name] @@ -1944,9 +1940,7 @@ def mod_tree_dfs( content = RS_HEADER + LIB_RS_HEADER + librs.content.getvalue() yield filename, content - def get_spec_toml_file( - self, derive_serde: bool, include_sso: bool - ) -> Iterator[Tuple[Text, Text]]: + def get_spec_toml_file(self, derive_serde: bool) -> Iterator[Tuple[Text, Text]]: for crate, deps in self.deps_map.items(): all_deps = ( {"lazy_static", "pb-jelly"} | deps | self.extra_crates[crate] @@ -1955,13 +1949,9 @@ def get_spec_toml_file( "serde": 'features=["serde_derive"]', "compact_str": 'features=["bytes"]', } - if derive_serde: all_deps.update({"serde"}) - if include_sso: - all_deps.update({"compact_str"}) - if derive_serde: - features.update({"compact_str": 'features=["bytes", "serde"]'}) + features.update({"compact_str": 'features=["bytes", "serde"]'}) deps_str = "\n".join( "{dep} = {{{feat}}}".format(dep=dep, feat=features.get(dep, "")) @@ -1970,9 +1960,7 @@ def get_spec_toml_file( targets = SPEC_TOML_TEMPLATE.format(crate=crate, deps=deps_str) yield crate, targets - def get_cargo_toml_file( - self, derive_serde: bool, include_sso: bool - ) -> Iterator[Tuple[Text, Text]]: + def get_cargo_toml_file(self, derive_serde: bool) -> Iterator[Tuple[Text, Text]]: for crate, deps in self.deps_map.items(): all_deps = ( {"lazy_static", "pb-jelly"} | deps | self.extra_crates[crate] @@ -1981,13 +1969,9 @@ def get_cargo_toml_file( "serde": 'features=["serde_derive"]', "compact_str": 'features=["bytes"]', } - if derive_serde: all_deps.update({"serde"}) - if include_sso: - all_deps.update({"compact_str"}) - if derive_serde: - features.update({"compact_str": 'features=["bytes", "serde"]'}) + features.update({"compact_str": 'features=["bytes", "serde"]'}) versions = { "lazy_static": ' version = "1.4.0" ', @@ -2098,7 +2082,6 @@ def new_mod_tree() -> ModTree: # Set iteration order is nondeterministic, but this is ok, because we never iterate through this processed_files: Set[Text] = set() derive_serde = False - include_sso = False for proto_file_name in file_to_generate: # Detect packages which collide with filenames. The rust codegen does not support those due @@ -2150,10 +2133,6 @@ def add_mod(writer: CodeWriter) -> None: add_mod(writer=writer) - # check if the writer ever used a small string optimization - if writer.uses_sso: - include_sso = True - # Note that output filenames must use "/" even on windows. It is part of the # protoc plugin protocol. The plugin speaks os-independent in "/". Thus, we # should not use os.path.sep or os.path.join @@ -2170,15 +2149,13 @@ def add_mod(writer: CodeWriter) -> None: output.name = file_prefix + crate + "/BUILD.in-gen-proto~" output.content = "" elif "generate_spec_toml" in request.parameter: - for crate, spec_toml_file in ctx.get_spec_toml_file(derive_serde, include_sso): + for crate, spec_toml_file in ctx.get_spec_toml_file(derive_serde): output = response.file.add() output.name = file_prefix + crate + "/Spec.toml" output.content = spec_toml_file else: # Generate good ol Cargo.toml files - for crate, cargo_toml_file in ctx.get_cargo_toml_file( - derive_serde, include_sso - ): + for crate, cargo_toml_file in ctx.get_cargo_toml_file(derive_serde): output = response.file.add() output.name = file_prefix + crate + "/Cargo.toml" output.content = cargo_toml_file