Skip to content

Commit

Permalink
Const-ify the descriptor on Message
Browse files Browse the repository at this point in the history
  • Loading branch information
cyang1 committed Dec 17, 2021
1 parent 2eea3fa commit 95808a4
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 101 deletions.
4 changes: 3 additions & 1 deletion pb-jelly-gen/codegen/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,9 @@ def gen_msg(
with block(self, "impl ::pb_jelly::Message for " + name):
with block(
self,
"fn descriptor(&self) -> ::std::option::Option<::pb_jelly::MessageDescriptor>",
"const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = ",
start=" (",
end=");",
):
name = "_".join(path + [msg_type.name])
full_name = (
Expand Down
2 changes: 1 addition & 1 deletion pb-jelly/src/erased.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where
T: ConcreteMessage,
{
fn erased_descriptor(&self) -> Option<MessageDescriptor> {
self.descriptor()
T::DESCRIPTOR
}

fn erased_compute_size(&self) -> usize {
Expand Down
6 changes: 2 additions & 4 deletions pb-jelly/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ mod tests;
/// like string, bytes, etc. The exact details of this trait is implemented for messages
/// and base types can be found at - <https://developers.google.com/protocol-buffers/docs/encoding>
pub trait Message: PartialEq + Default + Debug + Any {
/// Returns the `MessageDescriptor` for this message, if this is not a primitive type.
fn descriptor(&self) -> Option<MessageDescriptor> {
None
}
/// The `MessageDescriptor` for this message, if this is not a primitive type.
const DESCRIPTOR: Option<MessageDescriptor> = None;

/// Computes the number of bytes a message will take when serialized. This does not
/// include number of bytes required for tag+wire_format or the bytes used to represent
Expand Down
4 changes: 2 additions & 2 deletions pb-test/gen/pb-jelly/proto_google/src/empty.rs.expected
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "Empty",
full_name: "google.protobuf.Empty",
Expand All @@ -30,7 +30,7 @@ impl ::pb_jelly::Message for Empty {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "NoPackage",
full_name: "NoPackage",
Expand All @@ -32,7 +32,7 @@ impl ::pb_jelly::Message for NoPackage {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut field_size = 0;
Expand Down
12 changes: 6 additions & 6 deletions pb-test/gen/pb-jelly/proto_pbtest/src/bench.rs.expected
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "BytesData",
full_name: "pbtest.BytesData",
Expand All @@ -44,7 +44,7 @@ impl ::pb_jelly::Message for BytesData {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut data_size = 0;
Expand Down Expand Up @@ -142,7 +142,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "VecData",
full_name: "pbtest.VecData",
Expand All @@ -160,7 +160,7 @@ impl ::pb_jelly::Message for VecData {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut data_size = 0;
Expand Down Expand Up @@ -257,7 +257,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "StringMessage",
full_name: "pbtest.StringMessage",
Expand All @@ -275,7 +275,7 @@ impl ::pb_jelly::Message for StringMessage {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut data_size = 0;
Expand Down
60 changes: 30 additions & 30 deletions pb-test/gen/pb-jelly/proto_pbtest/src/pbtest2.rs.expected
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "Option",
full_name: "pbtest.Option",
Expand All @@ -474,7 +474,7 @@ impl ::pb_jelly::Message for Option {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
0
}
Expand Down Expand Up @@ -527,7 +527,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "Vec",
full_name: "pbtest.Vec",
Expand All @@ -536,7 +536,7 @@ impl ::pb_jelly::Message for Vec {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
0
}
Expand Down Expand Up @@ -589,7 +589,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "Default",
full_name: "pbtest.Default",
Expand All @@ -598,7 +598,7 @@ impl ::pb_jelly::Message for Default {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
0
}
Expand Down Expand Up @@ -651,7 +651,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "String",
full_name: "pbtest.String",
Expand All @@ -660,7 +660,7 @@ impl ::pb_jelly::Message for String {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
0
}
Expand Down Expand Up @@ -719,7 +719,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "Version0OneOfNoneNullable",
full_name: "pbtest.Version0OneOfNoneNullable",
Expand All @@ -740,7 +740,7 @@ impl ::pb_jelly::Message for Version0OneOfNoneNullable {
},
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut string_one_of_size = 0;
Expand Down Expand Up @@ -844,7 +844,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "Version1OneOfNoneNullable",
full_name: "pbtest.Version1OneOfNoneNullable",
Expand Down Expand Up @@ -874,7 +874,7 @@ impl ::pb_jelly::Message for Version1OneOfNoneNullable {
},
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut string_one_of_size = 0;
Expand Down Expand Up @@ -1025,7 +1025,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "Version2OneOfNoneNullable",
full_name: "pbtest.Version2OneOfNoneNullable",
Expand Down Expand Up @@ -1064,7 +1064,7 @@ impl ::pb_jelly::Message for Version2OneOfNoneNullable {
},
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut string_one_of_size = 0;
Expand Down Expand Up @@ -1253,7 +1253,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "Version1Enum",
full_name: "pbtest.Version1Enum",
Expand All @@ -1271,7 +1271,7 @@ impl ::pb_jelly::Message for Version1Enum {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut enum_field_size = 0;
Expand Down Expand Up @@ -1360,7 +1360,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "Version2Enum",
full_name: "pbtest.Version2Enum",
Expand All @@ -1378,7 +1378,7 @@ impl ::pb_jelly::Message for Version2Enum {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut enum_field_size = 0;
Expand Down Expand Up @@ -1462,7 +1462,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "Version1OneOf",
full_name: "pbtest.Version1OneOf",
Expand All @@ -1483,7 +1483,7 @@ impl ::pb_jelly::Message for Version1OneOf {
},
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut string_one_of_size = 0;
Expand Down Expand Up @@ -1588,7 +1588,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "Version2OneOf",
full_name: "pbtest.Version2OneOf",
Expand Down Expand Up @@ -1618,7 +1618,7 @@ impl ::pb_jelly::Message for Version2OneOf {
},
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut string_one_of_size = 0;
Expand Down Expand Up @@ -1765,7 +1765,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "Version1",
full_name: "pbtest.Version1",
Expand All @@ -1783,7 +1783,7 @@ impl ::pb_jelly::Message for Version1 {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut required_string_size = 0;
Expand Down Expand Up @@ -2029,7 +2029,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "Version2",
full_name: "pbtest.Version2",
Expand Down Expand Up @@ -2164,7 +2164,7 @@ impl ::pb_jelly::Message for Version2 {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut required_string_size = 0;
Expand Down Expand Up @@ -2567,7 +2567,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "ForeignMessage",
full_name: "pbtest.ForeignMessage",
Expand All @@ -2585,7 +2585,7 @@ impl ::pb_jelly::Message for ForeignMessage {
oneofs: &[
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut c_size = 0;
Expand Down Expand Up @@ -3157,7 +3157,7 @@ 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> {
const DESCRIPTOR: ::std::option::Option<::pb_jelly::MessageDescriptor> = (
Some(::pb_jelly::MessageDescriptor {
name: "TestMessage",
full_name: "pbtest.TestMessage",
Expand Down Expand Up @@ -3658,7 +3658,7 @@ impl ::pb_jelly::Message for TestMessage {
},
],
})
}
);
fn compute_size(&self) -> usize {
let mut size = 0;
let mut optional_int32_size = 0;
Expand Down
Loading

0 comments on commit 95808a4

Please sign in to comment.