@ -5,9 +5,13 @@
// license that can be found in the LICENSE file or at
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// https://developers.google.com/open-source/licenses/bsd
# include "absl/strings/match.h"
# include "absl/strings/string_view.h"
# include "absl/strings/string_view.h"
# include "absl/strings/strip.h"
# include "google/protobuf/compiler/cpp/helpers.h"
# include "google/protobuf/compiler/rust/accessors/accessor_generator.h"
# include "google/protobuf/compiler/rust/accessors/accessor_generator.h"
# include "google/protobuf/compiler/rust/context.h"
# include "google/protobuf/compiler/rust/context.h"
# include "google/protobuf/compiler/rust/naming.h"
# include "google/protobuf/descriptor.h"
# include "google/protobuf/descriptor.h"
namespace google {
namespace google {
@ -16,29 +20,46 @@ namespace compiler {
namespace rust {
namespace rust {
void SingularMessage : : InMsgImpl ( Context < FieldDescriptor > field ) const {
void SingularMessage : : InMsgImpl ( Context < FieldDescriptor > field ) const {
Context < Descriptor > d = field . WithDesc ( field . desc ( ) . message_type ( ) ) ;
auto prefix = " crate:: " + GetCrateRelativeQualifiedPath ( d ) ;
// here we defer unit tests with messages that have import inside their
// pkg name e.g. unittest_import.proto
if ( absl : : StrContains ( prefix , " import " ) ) {
// TODO(b/285309454): Handle imports correctly, default to $Msg$View for now
prefix = field . desc ( ) . containing_type ( ) - > name ( ) ;
}
field . Emit (
field . Emit (
{
{
{ " prefix " , prefix } ,
{ " field " , field . desc ( ) . name ( ) } ,
{ " field " , field . desc ( ) . name ( ) } ,
{ " getter_thunk " , Thunk ( field , " get " ) } ,
} ,
} ,
R " rs(
R " rs(
// inMsgImpl
pub fn r # $ field $ ( & self ) - > $ prefix $ View {
pub fn r # $ field $ ( & self ) - > $ Msg $ View {
$ prefix $ View : : new ( $ pbi $ : : Private , unsafe { $ getter_thunk $ ( self . inner . msg ) } )
$ Msg $ View { msg : self . inner . msg , _phantom : std : : marker : : PhantomData }
}
}
) rs " );
) rs " );
}
}
void SingularMessage : : InExternC ( Context < FieldDescriptor > field ) const {
void SingularMessage : : InExternC ( Context < FieldDescriptor > field ) const {
field . Emit ( { } ,
field . Emit (
R " rs(
{
// inExternC
{ " getter_thunk " , Thunk ( field , " get " ) } ,
} ,
R " rs(
fn $ getter_thunk $ ( raw_msg : $ pbi $ : : RawMessage ) - > $ pbi $ : : RawMessage ;
) rs " );
) rs " );
}
}
void SingularMessage : : InThunkCc ( Context < FieldDescriptor > field ) const {
void SingularMessage : : InThunkCc ( Context < FieldDescriptor > field ) const {
field . Emit ( { } ,
field . Emit ( { { " QualifiedMsg " ,
cpp : : QualifiedClassName ( field . desc ( ) . containing_type ( ) ) } ,
{ " getter_thunk " , Thunk ( field , " get " ) } ,
{ " field " , cpp : : FieldName ( & field . desc ( ) ) } } ,
R " cc(
R " cc(
// inThunkCC
const void * $ getter_thunk $ ( $ QualifiedMsg $ * msg ) {
return static_cast < const void * > ( & msg - > $ field $ ( ) ) ;
}
) cc " );
) cc " );
}
}