Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: boxed string field #1210

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,21 +639,21 @@ impl CodeGenerator<'_> {

self.push_indent();
let ty_tag = self.field_type_tag(&field.descriptor);
self.buf.push_str(&format!(
"#[prost({}, tag=\"{}\")]\n",
ty_tag,
field.descriptor.number()
));
self.append_field_attributes(&oneof_name, field.descriptor.name());

self.push_indent();
let ty = self.resolve_type(&field.descriptor, fq_message_name);

let boxed = self.boxed(
&field.descriptor,
fq_message_name,
Some(oneof.descriptor.name()),
);
self.buf.push_str(&format!("#[prost({}", ty_tag));
if boxed {
self.buf.push_str(", boxed");
}
self.buf
.push_str(&format!(", tag=\"{}\")]\n", field.descriptor.number()));
self.append_field_attributes(&oneof_name, field.descriptor.name());

self.push_indent();
let ty = self.resolve_type(&field.descriptor, fq_message_name);

debug!(
" oneof: {:?}, type: {:?}, boxed: {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Container {
pub mod container {
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Data {
#[prost(message, tag="1")]
#[prost(message, boxed, tag="1")]
Foo(::prost::alloc::boxed::Box<super::Foo>),
#[prost(message, tag="2")]
Bar(super::Bar),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Container {
pub mod container {
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Data {
#[prost(message, tag = "1")]
#[prost(message, boxed, tag = "1")]
Foo(::prost::alloc::boxed::Box<super::Foo>),
#[prost(message, tag = "2")]
Bar(super::Bar),
Expand Down
1 change: 1 addition & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ fn main() {

prost_build::Config::new()
.boxed("Foo.bar")
.boxed("Foo.oneof_field.box_qux")
.compile_protos(&[src.join("boxed_field.proto")], includes)
.unwrap();

Expand Down
4 changes: 4 additions & 0 deletions tests/src/boxed_field.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ package boxed_field;

message Foo {
Bar bar = 1;
oneof oneof_field {
string baz = 2;
Bar box_qux = 3;
}
}

message Bar {
Expand Down
13 changes: 11 additions & 2 deletions tests/src/boxed_field.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
include!(concat!(env!("OUT_DIR"), "/boxed_field.rs"));

cfg_if! {
if #[cfg(feature = "edition-2015")] {
use boxed_field::foo::OneofField;
} else {
use foo::OneofField;
}
}

Comment on lines +3 to +10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could simplify this to:

Suggested change
cfg_if! {
if #[cfg(feature = "edition-2015")] {
use boxed_field::foo::OneofField;
} else {
use foo::OneofField;
}
}
use self::foo::OneofField;

#[test]
/// Confirm `Foo::bar` is boxed by creating an instance
fn test_bar_is_boxed() {
/// Confirm `Foo::bar` and `OneofField::BoxQux` is boxed by creating an instance
fn test_boxed_field() {
use alloc::boxed::Box;
let _ = Foo {
bar: Some(Box::new(Bar {})),
oneof_field: Some(OneofField::BoxQux(Box::new(Bar {}))),
};
}
Loading