|
|
@ -65,15 +65,15 @@ void MessageStructFields(Context<Descriptor> msg) { |
|
|
|
void MessageNew(Context<Descriptor> msg) { |
|
|
|
void MessageNew(Context<Descriptor> msg) { |
|
|
|
switch (msg.opts().kernel) { |
|
|
|
switch (msg.opts().kernel) { |
|
|
|
case Kernel::kCpp: |
|
|
|
case Kernel::kCpp: |
|
|
|
msg.Emit(R"rs( |
|
|
|
msg.Emit({{"new_thunk", Thunk(msg, "new")}}, R"rs( |
|
|
|
Self { msg: unsafe { __rust_proto_thunk__$pkg_Msg$__new() } } |
|
|
|
Self { msg: unsafe { $new_thunk$() } } |
|
|
|
)rs"); |
|
|
|
)rs"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
case Kernel::kUpb: |
|
|
|
case Kernel::kUpb: |
|
|
|
msg.Emit(R"rs( |
|
|
|
msg.Emit({{"new_thunk", Thunk(msg, "new")}}, R"rs( |
|
|
|
let arena = unsafe { $pb$::Arena::new() }; |
|
|
|
let arena = unsafe { $pb$::Arena::new() }; |
|
|
|
let msg = unsafe { $pkg_Msg$_new(arena) }; |
|
|
|
let msg = unsafe { $new_thunk$(arena) }; |
|
|
|
$Msg$ { msg, arena } |
|
|
|
$Msg$ { msg, arena } |
|
|
|
)rs"); |
|
|
|
)rs"); |
|
|
|
return; |
|
|
|
return; |
|
|
@ -85,17 +85,17 @@ void MessageNew(Context<Descriptor> msg) { |
|
|
|
void MessageSerialize(Context<Descriptor> msg) { |
|
|
|
void MessageSerialize(Context<Descriptor> msg) { |
|
|
|
switch (msg.opts().kernel) { |
|
|
|
switch (msg.opts().kernel) { |
|
|
|
case Kernel::kCpp: |
|
|
|
case Kernel::kCpp: |
|
|
|
msg.Emit(R"rs( |
|
|
|
msg.Emit({{"serialize_thunk", Thunk(msg, "serialize")}}, R"rs( |
|
|
|
unsafe { __rust_proto_thunk__$pkg_Msg$__serialize(self.msg) } |
|
|
|
unsafe { $serialize_thunk$(self.msg) } |
|
|
|
)rs"); |
|
|
|
)rs"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
case Kernel::kUpb: |
|
|
|
case Kernel::kUpb: |
|
|
|
msg.Emit(R"rs( |
|
|
|
msg.Emit({{"serialize_thunk", Thunk(msg, "serialize")}}, R"rs( |
|
|
|
let arena = unsafe { $pb$::__runtime::upb_Arena_New() }; |
|
|
|
let arena = unsafe { $pb$::__runtime::upb_Arena_New() }; |
|
|
|
let mut len = 0; |
|
|
|
let mut len = 0; |
|
|
|
unsafe { |
|
|
|
unsafe { |
|
|
|
let data = $pkg_Msg$_serialize(self.msg, arena, &mut len); |
|
|
|
let data = $serialize_thunk$(self.msg, arena, &mut len); |
|
|
|
$pb$::SerializedData::from_raw_parts(arena, data, len) |
|
|
|
$pb$::SerializedData::from_raw_parts(arena, data, len) |
|
|
|
} |
|
|
|
} |
|
|
|
)rs"); |
|
|
|
)rs"); |
|
|
@ -108,14 +108,18 @@ void MessageSerialize(Context<Descriptor> msg) { |
|
|
|
void MessageDeserialize(Context<Descriptor> msg) { |
|
|
|
void MessageDeserialize(Context<Descriptor> msg) { |
|
|
|
switch (msg.opts().kernel) { |
|
|
|
switch (msg.opts().kernel) { |
|
|
|
case Kernel::kCpp: |
|
|
|
case Kernel::kCpp: |
|
|
|
msg.Emit(R"rs( |
|
|
|
msg.Emit( |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
{"deserialize_thunk", Thunk(msg, "deserialize")}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
R"rs( |
|
|
|
let success = unsafe { |
|
|
|
let success = unsafe { |
|
|
|
let data = $pb$::SerializedData::from_raw_parts( |
|
|
|
let data = $pb$::SerializedData::from_raw_parts( |
|
|
|
$NonNull$::new(data.as_ptr() as *mut _).unwrap(), |
|
|
|
$NonNull$::new(data.as_ptr() as *mut _).unwrap(), |
|
|
|
data.len(), |
|
|
|
data.len(), |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
__rust_proto_thunk__$pkg_Msg$__deserialize(self.msg, data) |
|
|
|
$deserialize_thunk$(self.msg, data) |
|
|
|
}; |
|
|
|
}; |
|
|
|
success.then_some(()).ok_or($pb$::ParseError) |
|
|
|
success.then_some(()).ok_or($pb$::ParseError) |
|
|
|
)rs"); |
|
|
|
)rs"); |
|
|
@ -135,30 +139,52 @@ void MessageDeserialize(Context<Descriptor> msg) { |
|
|
|
void MessageExterns(Context<Descriptor> msg) { |
|
|
|
void MessageExterns(Context<Descriptor> msg) { |
|
|
|
switch (msg.opts().kernel) { |
|
|
|
switch (msg.opts().kernel) { |
|
|
|
case Kernel::kCpp: |
|
|
|
case Kernel::kCpp: |
|
|
|
msg.Emit(R"rs( |
|
|
|
msg.Emit( |
|
|
|
fn __rust_proto_thunk__$pkg_Msg$__new() -> $NonNull$<u8>; |
|
|
|
{ |
|
|
|
fn __rust_proto_thunk__$pkg_Msg$__serialize(raw_msg: $NonNull$<u8>) |
|
|
|
{"new_thunk", Thunk(msg, "new")}, |
|
|
|
-> $pb$::SerializedData; |
|
|
|
{"delete_thunk", Thunk(msg, "delete")}, |
|
|
|
fn __rust_proto_thunk__$pkg_Msg$__deserialize( |
|
|
|
{"serialize_thunk", Thunk(msg, "serialize")}, |
|
|
|
raw_msg: $NonNull$<u8>, |
|
|
|
{"deserialize_thunk", Thunk(msg, "deserialize")}, |
|
|
|
data: $pb$::SerializedData, |
|
|
|
}, |
|
|
|
) -> bool; |
|
|
|
R"rs( |
|
|
|
|
|
|
|
fn $new_thunk$() -> $NonNull$<u8>; |
|
|
|
|
|
|
|
fn $delete_thunk$(raw_msg: $NonNull$<u8>); |
|
|
|
|
|
|
|
fn $serialize_thunk$(raw_msg: $NonNull$<u8>) -> $pb$::SerializedData; |
|
|
|
|
|
|
|
fn $deserialize_thunk$( raw_msg: $NonNull$<u8>, data: $pb$::SerializedData) -> bool; |
|
|
|
)rs"); |
|
|
|
)rs"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
case Kernel::kUpb: |
|
|
|
case Kernel::kUpb: |
|
|
|
msg.Emit(R"rs( |
|
|
|
msg.Emit( |
|
|
|
fn $pkg_Msg$_new(arena: *mut $pb$::Arena) -> $NonNull$<u8>; |
|
|
|
{ |
|
|
|
fn $pkg_Msg$_serialize( |
|
|
|
{"new_thunk", Thunk(msg, "new")}, |
|
|
|
|
|
|
|
{"serialize_thunk", Thunk(msg, "serialize")}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
R"rs( |
|
|
|
|
|
|
|
fn $new_thunk$(arena: *mut $pb$::Arena) -> $NonNull$<u8>; |
|
|
|
|
|
|
|
fn $serialize_thunk$( |
|
|
|
msg: $NonNull$<u8>, |
|
|
|
msg: $NonNull$<u8>, |
|
|
|
arena: *mut $pb$::Arena, |
|
|
|
arena: *mut $pb$::Arena, |
|
|
|
len: &mut usize) -> $NonNull$<u8>; |
|
|
|
len: &mut usize, |
|
|
|
|
|
|
|
) -> $NonNull$<u8>; |
|
|
|
)rs"); |
|
|
|
)rs"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ABSL_LOG(FATAL) << "unreachable"; |
|
|
|
ABSL_LOG(FATAL) << "unreachable"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MessageDrop(Context<Descriptor> msg) { |
|
|
|
|
|
|
|
if (msg.is_upb()) { |
|
|
|
|
|
|
|
// Nothing to do here; drop glue (which will run drop(self.arena)
|
|
|
|
|
|
|
|
// automatically) is sufficient.
|
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msg.Emit({{"delete_thunk", Thunk(msg, "delete")}}, R"rs( |
|
|
|
|
|
|
|
unsafe { $delete_thunk$(self.msg); } |
|
|
|
|
|
|
|
)rs"); |
|
|
|
|
|
|
|
} |
|
|
|
} // namespace
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
MessageGenerator::MessageGenerator(Context<Descriptor> msg) { |
|
|
|
MessageGenerator::MessageGenerator(Context<Descriptor> msg) { |
|
|
@ -176,11 +202,11 @@ void MessageGenerator::GenerateRs(Context<Descriptor> msg) { |
|
|
|
msg.Emit( |
|
|
|
msg.Emit( |
|
|
|
{ |
|
|
|
{ |
|
|
|
{"Msg", msg.desc().name()}, |
|
|
|
{"Msg", msg.desc().name()}, |
|
|
|
{"pkg_Msg", GetUnderscoreDelimitedFullName(msg)}, |
|
|
|
|
|
|
|
{"Msg.fields", [&] { MessageStructFields(msg); }}, |
|
|
|
{"Msg.fields", [&] { MessageStructFields(msg); }}, |
|
|
|
{"Msg::new", [&] { MessageNew(msg); }}, |
|
|
|
{"Msg::new", [&] { MessageNew(msg); }}, |
|
|
|
{"Msg::serialize", [&] { MessageSerialize(msg); }}, |
|
|
|
{"Msg::serialize", [&] { MessageSerialize(msg); }}, |
|
|
|
{"Msg::deserialize", [&] { MessageDeserialize(msg); }}, |
|
|
|
{"Msg::deserialize", [&] { MessageDeserialize(msg); }}, |
|
|
|
|
|
|
|
{"Msg::drop", [&] { MessageDrop(msg); }}, |
|
|
|
{"Msg_externs", [&] { MessageExterns(msg); }}, |
|
|
|
{"Msg_externs", [&] { MessageExterns(msg); }}, |
|
|
|
{"accessor_fns", |
|
|
|
{"accessor_fns", |
|
|
|
[&] { |
|
|
|
[&] { |
|
|
@ -235,6 +261,14 @@ void MessageGenerator::GenerateRs(Context<Descriptor> msg) { |
|
|
|
$accessor_fns$ |
|
|
|
$accessor_fns$ |
|
|
|
} // impl $Msg$
|
|
|
|
} // impl $Msg$
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//~ We implement drop unconditionally, so that `$Msg$: Drop` regardless
|
|
|
|
|
|
|
|
//~ of kernel.
|
|
|
|
|
|
|
|
impl $std$::ops::Drop for $Msg$ { |
|
|
|
|
|
|
|
fn drop(&mut self) { |
|
|
|
|
|
|
|
$Msg::drop$ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
extern "C" { |
|
|
|
extern "C" { |
|
|
|
$Msg_externs$ |
|
|
|
$Msg_externs$ |
|
|
|
|
|
|
|
|
|
|
@ -262,8 +296,11 @@ void MessageGenerator::GenerateThunkCc(Context<Descriptor> msg) { |
|
|
|
{ |
|
|
|
{ |
|
|
|
{"abi", "\"C\""}, // Workaround for syntax highlight bug in VSCode.
|
|
|
|
{"abi", "\"C\""}, // Workaround for syntax highlight bug in VSCode.
|
|
|
|
{"Msg", msg.desc().name()}, |
|
|
|
{"Msg", msg.desc().name()}, |
|
|
|
{"pkg_Msg", GetUnderscoreDelimitedFullName(msg)}, |
|
|
|
{"pkg", cpp::Namespace(&msg.desc())}, |
|
|
|
{"namespace", cpp::Namespace(&msg.desc())}, |
|
|
|
{"new_thunk", Thunk(msg, "new")}, |
|
|
|
|
|
|
|
{"delete_thunk", Thunk(msg, "delete")}, |
|
|
|
|
|
|
|
{"serialize_thunk", Thunk(msg, "serialize")}, |
|
|
|
|
|
|
|
{"deserialize_thunk", Thunk(msg, "deserialize")}, |
|
|
|
{"accessor_thunks", |
|
|
|
{"accessor_thunks", |
|
|
|
[&] { |
|
|
|
[&] { |
|
|
|
for (int i = 0; i < msg.desc().field_count(); ++i) { |
|
|
|
for (int i = 0; i < msg.desc().field_count(); ++i) { |
|
|
@ -276,17 +313,12 @@ void MessageGenerator::GenerateThunkCc(Context<Descriptor> msg) { |
|
|
|
}, |
|
|
|
}, |
|
|
|
R"cc( |
|
|
|
R"cc( |
|
|
|
extern $abi$ { |
|
|
|
extern $abi$ { |
|
|
|
void* __rust_proto_thunk__$pkg_Msg$__new() { |
|
|
|
void* $new_thunk$() { return new $pkg$::$Msg$(); } |
|
|
|
return new $namespace$::$Msg$(); |
|
|
|
void $delete_thunk$(void* ptr) { delete static_cast<$pkg$::$Msg$*>(ptr); } |
|
|
|
} |
|
|
|
google::protobuf::rust_internal::SerializedData $serialize_thunk$($pkg$::$Msg$ * msg) { |
|
|
|
|
|
|
|
|
|
|
|
google::protobuf::rust_internal::SerializedData |
|
|
|
|
|
|
|
__rust_proto_thunk__$pkg_Msg$__serialize($namespace$::$Msg$ * msg) { |
|
|
|
|
|
|
|
return google::protobuf::rust_internal::SerializeMsg(msg); |
|
|
|
return google::protobuf::rust_internal::SerializeMsg(msg); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
bool $deserialize_thunk$($pkg$::$Msg$ * msg, |
|
|
|
bool __rust_proto_thunk__$pkg_Msg$__deserialize( |
|
|
|
|
|
|
|
$namespace$::$Msg$ * msg, |
|
|
|
|
|
|
|
google::protobuf::rust_internal::SerializedData data) { |
|
|
|
google::protobuf::rust_internal::SerializedData data) { |
|
|
|
return msg->ParseFromArray(data.data, data.len); |
|
|
|
return msg->ParseFromArray(data.data, data.len); |
|
|
|
} |
|
|
|
} |
|
|
|