Skip to content

Commit dac1797

Browse files
Adam Cozzettecopybara-github
authored andcommitted
Move the Parse trait out of gencode and into the runtime
As part of this, I also removed the inherent methods `parse` and `parse_dont_enforce_required` from generated messages. This changes the API in a way that requires fixes for code that's not using our prelude (requires adding `use protobuf::Parse`). The blanket implementation of the trait is the same for both the C++ and upb kernels, so I just put it directly in codegen_traits.rs. PiperOrigin-RevId: 804967515
1 parent 5e8700c commit dac1797

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

rust/codegen_traits.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ pub(crate) mod create {
7575
fn parse(serialized: &[u8]) -> Result<Self, crate::ParseError>;
7676
fn parse_dont_enforce_required(serialized: &[u8]) -> Result<Self, crate::ParseError>;
7777
}
78+
79+
impl<T> Parse for T
80+
where
81+
Self: Default + crate::ClearAndParse,
82+
{
83+
fn parse(serialized: &[u8]) -> Result<Self, crate::ParseError> {
84+
let mut msg = Self::default();
85+
crate::ClearAndParse::clear_and_parse(&mut msg, serialized).map(|_| msg)
86+
}
87+
88+
fn parse_dont_enforce_required(serialized: &[u8]) -> Result<Self, crate::ParseError> {
89+
let mut msg = Self::default();
90+
crate::ClearAndParse::clear_and_parse_dont_enforce_required(&mut msg, serialized)
91+
.map(|_| msg)
92+
}
93+
}
7894
}
7995

8096
/// Operations related to reading some aspect of a message (methods that would

src/google/protobuf/compiler/rust/message.cc

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -591,16 +591,6 @@ void GenerateRs(Context& ctx, const Descriptor& msg, const upb::DefPool& pool) {
591591
}
592592
}
593593
594-
impl $pb$::Parse for $Msg$ {
595-
fn parse(serialized: &[u8]) -> $Result$<Self, $pb$::ParseError> {
596-
Self::parse(serialized)
597-
}
598-
599-
fn parse_dont_enforce_required(serialized: &[u8]) -> $Result$<Self, $pb$::ParseError> {
600-
Self::parse_dont_enforce_required(serialized)
601-
}
602-
}
603-
604594
impl $std$::fmt::Debug for $Msg$ {
605595
fn fmt(&self, f: &mut $std$::fmt::Formatter<'_>) -> $std$::fmt::Result {
606596
$Msg::debug$
@@ -808,16 +798,6 @@ void GenerateRs(Context& ctx, const Descriptor& msg, const upb::DefPool& pool) {
808798
809799
$raw_arena_getter_for_message$
810800
811-
pub fn parse(data: &[u8]) -> $Result$<Self, $pb$::ParseError> {
812-
let mut msg = Self::new();
813-
$pb$::ClearAndParse::clear_and_parse(&mut msg, data).map(|_| msg)
814-
}
815-
816-
pub fn parse_dont_enforce_required(data: &[u8]) -> $Result$<Self, $pb$::ParseError> {
817-
let mut msg = Self::new();
818-
$pb$::ClearAndParse::clear_and_parse_dont_enforce_required(&mut msg, data).map(|_| msg)
819-
}
820-
821801
pub fn as_view(&self) -> $Msg$View<'_> {
822802
$pbr$::MessageViewInner::view_of_owned(&self.inner).into()
823803
}

0 commit comments

Comments
 (0)