Skip to content

Commit

Permalink
Remove TypeName_default lazy_statics, use OnceLock as needed instead
Browse files Browse the repository at this point in the history
  • Loading branch information
goffrie committed Dec 2, 2023
1 parent 25e97ca commit 388500d
Show file tree
Hide file tree
Showing 14 changed files with 9 additions and 216 deletions.
20 changes: 7 additions & 13 deletions pb-jelly-gen/codegen/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,11 @@ def get_method(self) -> Tuple[Text, Text]:
deref = "" if not self.is_boxed() else ".map(::std::ops::Deref::deref)"
return (
"&" + self.rust_type(),
"self.%s.as_ref()%s.unwrap_or(&%s_default)"
% (name, deref, self.rust_type()),
"self.{name}.as_ref(){deref}.unwrap_or({{ \
static DEFAULT: ::std::sync::OnceLock<{ty}> = ::std::sync::OnceLock::new(); \
DEFAULT.get_or_init(<{ty} as ::std::default::Default>::default) }})".format(
name=name, deref=deref, ty=self.rust_type()
),
)
raise AssertionError("Unexpected field type")

Expand Down Expand Up @@ -1063,12 +1066,6 @@ def gen_msg(
]:
self.write("_unrecognized: Vec::new(),")

with block(self, "lazy_static!"):
self.write(
"pub static ref %s_default: %s = %s::default();"
% (name, escaped_name, escaped_name)
)

with block(self, "impl ::pb_jelly::Message for " + escaped_name):
with block(
self,
Expand Down Expand Up @@ -1795,8 +1792,6 @@ def get_lib_and_mod_rs(
) -> Iterator[Tuple[Text, Text]]:
for crate, deps in self.deps_map.items():
librs = CodeWriter(None, None, None, None) # type: ignore
librs.write("#[macro_use]")
librs.write("extern crate lazy_static;")
if derive_serde:
librs.write("#[macro_use]")
librs.write("extern crate serde;")
Expand Down Expand Up @@ -1846,7 +1841,7 @@ def get_spec_toml_file(
) -> Iterator[Tuple[Text, Text]]:
for crate, deps in self.deps_map.items():
all_deps = (
{"lazy_static", "pb-jelly"} | deps | self.extra_crates[crate]
{"pb-jelly"} | deps | self.extra_crates[crate]
) - {"std"}
features = {
"serde": 'features=["serde_derive"]',
Expand All @@ -1872,7 +1867,7 @@ def get_cargo_toml_file(
) -> Iterator[Tuple[Text, Text]]:
for crate, deps in self.deps_map.items():
all_deps = (
{"lazy_static", "pb-jelly"} | deps | self.extra_crates[crate]
{"pb-jelly"} | deps | self.extra_crates[crate]
) - {"std"}
features = {
"serde": 'features=["serde_derive"]',
Expand All @@ -1887,7 +1882,6 @@ def get_cargo_toml_file(
features.update({"compact_str": 'features=["bytes", "serde"]'})

versions = {
"lazy_static": ' version = "1.4.0" ',
"pb-jelly": ' version = "0.0.14" ',
"serde": ' version = "1.0" ',
"bytes": ' version = "1.0" ',
Expand Down
1 change: 0 additions & 1 deletion pb-test/gen/pb-jelly/proto_google/Cargo.toml.expected
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ edition = "2018"

[dependencies]
compact_str = {features=["bytes"], version = "0.5" }
lazy_static = { version = "1.4.0" }
pb-jelly = { version = "0.0.14" }
2 changes: 0 additions & 2 deletions pb-test/gen/pb-jelly/proto_google/src/lib.rs.expected
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@
// TODO: Ideally we don't allow this
#![allow(clippy::match_single_binding)]

#[macro_use]
extern crate lazy_static;

pub mod protobuf;
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ impl ::std::default::Default for Empty {
}
}
}
lazy_static! {
pub static ref Empty_default: Empty = Empty::default();
}
impl ::pb_jelly::Message for Empty {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down
1 change: 0 additions & 1 deletion pb-test/gen/pb-jelly/proto_nopackage/Cargo.toml.expected
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ edition = "2018"

[dependencies]
compact_str = {features=["bytes"], version = "0.5" }
lazy_static = { version = "1.4.0" }
pb-jelly = { version = "0.0.14" }
2 changes: 0 additions & 2 deletions pb-test/gen/pb-jelly/proto_nopackage/src/lib.rs.expected
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@
// TODO: Ideally we don't allow this
#![allow(clippy::match_single_binding)]

#[macro_use]
extern crate lazy_static;

pub mod proto_nopackage;
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ impl ::std::default::Default for NoPackage {
}
}
}
lazy_static! {
pub static ref NoPackage_default: NoPackage = NoPackage::default();
}
impl ::pb_jelly::Message for NoPackage {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down
1 change: 0 additions & 1 deletion pb-test/gen/pb-jelly/proto_pbtest/Cargo.toml.expected
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ edition = "2018"
[dependencies]
bytes = { version = "1.0" }
compact_str = {features=["bytes"], version = "0.5" }
lazy_static = { version = "1.4.0" }
pb-jelly = { version = "0.0.14" }
proto_google = {path = "../proto_google"}
24 changes: 0 additions & 24 deletions pb-test/gen/pb-jelly/proto_pbtest/src/bench.rs.expected
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ impl ::std::default::Default for BytesData {
}
}
}
lazy_static! {
pub static ref BytesData_default: BytesData = BytesData::default();
}
impl ::pb_jelly::Message for BytesData {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -138,9 +135,6 @@ impl ::std::default::Default for VecData {
}
}
}
lazy_static! {
pub static ref VecData_default: VecData = VecData::default();
}
impl ::pb_jelly::Message for VecData {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -253,9 +247,6 @@ impl ::std::default::Default for StringMessage {
}
}
}
lazy_static! {
pub static ref StringMessage_default: StringMessage = StringMessage::default();
}
impl ::pb_jelly::Message for StringMessage {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -368,9 +359,6 @@ impl ::std::default::Default for StringMessageSSO {
}
}
}
lazy_static! {
pub static ref StringMessageSSO_default: StringMessageSSO = StringMessageSSO::default();
}
impl ::pb_jelly::Message for StringMessageSSO {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -483,9 +471,6 @@ impl ::std::default::Default for Cities {
}
}
}
lazy_static! {
pub static ref Cities_default: Cities = Cities::default();
}
impl ::pb_jelly::Message for Cities {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -676,9 +661,6 @@ impl ::std::default::Default for City {
}
}
}
lazy_static! {
pub static ref City_default: City = City::default();
}
impl ::pb_jelly::Message for City {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -1003,9 +985,6 @@ impl ::std::default::Default for CitiesSSO {
}
}
}
lazy_static! {
pub static ref CitiesSSO_default: CitiesSSO = CitiesSSO::default();
}
impl ::pb_jelly::Message for CitiesSSO {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -1196,9 +1175,6 @@ impl ::std::default::Default for CitySSO {
}
}
}
lazy_static! {
pub static ref CitySSO_default: CitySSO = CitySSO::default();
}
impl ::pb_jelly::Message for CitySSO {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down
2 changes: 0 additions & 2 deletions pb-test/gen/pb-jelly/proto_pbtest/src/lib.rs.expected
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
// TODO: Ideally we don't allow this
#![allow(clippy::match_single_binding)]

#[macro_use]
extern crate lazy_static;

pub mod bench;
pub mod r#mod;
Expand Down
3 changes: 0 additions & 3 deletions pb-test/gen/pb-jelly/proto_pbtest/src/mod/struct.rs.expected
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ impl ::std::default::Default for Message {
}
}
}
lazy_static! {
pub static ref Message_default: Message = Message::default();
}
impl ::pb_jelly::Message for Message {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down
49 changes: 2 additions & 47 deletions pb-test/gen/pb-jelly/proto_pbtest/src/pbtest2.rs.expected
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,6 @@ impl ::std::default::Default for Option {
}
}
}
lazy_static! {
pub static ref Option_default: Option = Option::default();
}
impl ::pb_jelly::Message for Option {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -523,9 +520,6 @@ impl ::std::default::Default for Vec {
}
}
}
lazy_static! {
pub static ref Vec_default: Vec = Vec::default();
}
impl ::pb_jelly::Message for Vec {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -585,9 +579,6 @@ impl ::std::default::Default for Default {
}
}
}
lazy_static! {
pub static ref Default_default: Default = Default::default();
}
impl ::pb_jelly::Message for Default {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -647,9 +638,6 @@ impl ::std::default::Default for String {
}
}
}
lazy_static! {
pub static ref String_default: String = String::default();
}
impl ::pb_jelly::Message for String {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -715,9 +703,6 @@ impl ::std::default::Default for Version0OneOfNoneNullable {
}
}
}
lazy_static! {
pub static ref Version0OneOfNoneNullable_default: Version0OneOfNoneNullable = Version0OneOfNoneNullable::default();
}
impl ::pb_jelly::Message for Version0OneOfNoneNullable {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -840,9 +825,6 @@ impl ::std::default::Default for Version1OneOfNoneNullable {
}
}
}
lazy_static! {
pub static ref Version1OneOfNoneNullable_default: Version1OneOfNoneNullable = Version1OneOfNoneNullable::default();
}
impl ::pb_jelly::Message for Version1OneOfNoneNullable {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -1021,9 +1003,6 @@ impl ::std::default::Default for Version2OneOfNoneNullable {
}
}
}
lazy_static! {
pub static ref Version2OneOfNoneNullable_default: Version2OneOfNoneNullable = Version2OneOfNoneNullable::default();
}
impl ::pb_jelly::Message for Version2OneOfNoneNullable {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -1249,9 +1228,6 @@ impl ::std::default::Default for Version1Enum {
}
}
}
lazy_static! {
pub static ref Version1Enum_default: Version1Enum = Version1Enum::default();
}
impl ::pb_jelly::Message for Version1Enum {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -1356,9 +1332,6 @@ impl ::std::default::Default for Version2Enum {
}
}
}
lazy_static! {
pub static ref Version2Enum_default: Version2Enum = Version2Enum::default();
}
impl ::pb_jelly::Message for Version2Enum {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -1458,9 +1431,6 @@ impl ::std::default::Default for Version1OneOf {
}
}
}
lazy_static! {
pub static ref Version1OneOf_default: Version1OneOf = Version1OneOf::default();
}
impl ::pb_jelly::Message for Version1OneOf {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -1584,9 +1554,6 @@ impl ::std::default::Default for Version2OneOf {
}
}
}
lazy_static! {
pub static ref Version2OneOf_default: Version2OneOf = Version2OneOf::default();
}
impl ::pb_jelly::Message for Version2OneOf {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -1761,9 +1728,6 @@ impl ::std::default::Default for Version1 {
}
}
}
lazy_static! {
pub static ref Version1_default: Version1 = Version1::default();
}
impl ::pb_jelly::Message for Version1 {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -2025,9 +1989,6 @@ impl ::std::default::Default for Version2 {
}
}
}
lazy_static! {
pub static ref Version2_default: Version2 = Version2::default();
}
impl ::pb_jelly::Message for Version2 {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -2563,9 +2524,6 @@ impl ::std::default::Default for ForeignMessage {
}
}
}
lazy_static! {
pub static ref ForeignMessage_default: ForeignMessage = ForeignMessage::default();
}
impl ::pb_jelly::Message for ForeignMessage {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down Expand Up @@ -2870,7 +2828,7 @@ impl TestMessage {
self.optional_foreign_message.take().unwrap_or_default()
}
pub fn get_optional_foreign_message(&self) -> &ForeignMessage {
self.optional_foreign_message.as_ref().unwrap_or(&ForeignMessage_default)
self.optional_foreign_message.as_ref().unwrap_or({ static DEFAULT: ::std::sync::OnceLock<ForeignMessage> = ::std::sync::OnceLock::new(); DEFAULT.get_or_init(<ForeignMessage as ::std::default::Default>::default) })
}
pub fn has_optional_nested_enum(&self) -> bool {
self.optional_nested_enum.is_some()
Expand Down Expand Up @@ -3092,7 +3050,7 @@ impl TestMessage {
self.optional_foreign_message_boxed.take().unwrap_or_default()
}
pub fn get_optional_foreign_message_boxed(&self) -> &ForeignMessage {
self.optional_foreign_message_boxed.as_ref().map(::std::ops::Deref::deref).unwrap_or(&ForeignMessage_default)
self.optional_foreign_message_boxed.as_ref().map(::std::ops::Deref::deref).unwrap_or({ static DEFAULT: ::std::sync::OnceLock<ForeignMessage> = ::std::sync::OnceLock::new(); DEFAULT.get_or_init(<ForeignMessage as ::std::default::Default>::default) })
}
pub fn has_type(&self) -> bool {
self.r#type.is_some()
Expand Down Expand Up @@ -3153,9 +3111,6 @@ impl ::std::default::Default for TestMessage {
}
}
}
lazy_static! {
pub static ref TestMessage_default: TestMessage = TestMessage::default();
}
impl ::pb_jelly::Message for TestMessage {
fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor> {
Some(::pb_jelly::MessageDescriptor {
Expand Down
Loading

0 comments on commit 388500d

Please sign in to comment.