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

Conversation

xxchan
Copy link

@xxchan xxchan commented Dec 20, 2024

fix #1159

Signed-off-by: xxchan [email protected]

Copy link
Collaborator

@caspermeijn caspermeijn left a comment

Choose a reason for hiding this comment

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

I think this test will pass without the other changes. CodeGenerator already makes the OneOf field a Box<T> if self.boxed() == true on line 667.

Can you explain why the other changes are needed?

tests/src/boxed_field.rs Outdated Show resolved Hide resolved
prost-derive/src/field/oneof.rs Outdated Show resolved Hide resolved
Signed-off-by: xxchan <[email protected]>
Signed-off-by: xxchan <[email protected]>
Signed-off-by: xxchan <[email protected]>
Signed-off-by: xxchan <[email protected]>
Copy link
Author

@xxchan xxchan left a comment

Choose a reason for hiding this comment

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

Hi @caspermeijn, thanks for the prompt review! Reverted unnecessary changes, and made CI Green.

I think this test will pass without the other changes.

I'm not sure what you mean by "other changes" here.

But anyway, here's how it works:

As mentioned in #1159, the type for the oneof field was correct. What was wrong is the generated merge function.

I noticed there are #[prost(boxed)] on normal messages fields, but not on the oneof field. After adding it, it works.

pub struct Foo {
    #[prost(message, optional, boxed, tag = "1")]
    pub bar: ::core::option::Option<::prost::alloc::boxed::Box<Bar>>,
    #[prost(oneof = "foo::OneofField", tags = "2, 3")]
    pub oneof_field: ::core::option::Option<foo::OneofField>,
}
/// Nested message and enum types in `Foo`.
    pub mod foo {
        pub enum OneofField {
            #[prost(string, tag = "2")]
            Baz(::prost::alloc::string::String),
// This PR added "boxed" here 
            #[prost(message, boxed, tag = "3")]  
            BoxQux(::prost::alloc::boxed::Box<super::Bar>),
        }
	}

prost-derive/src/field/oneof.rs Outdated Show resolved Hide resolved
@xxchan xxchan requested a review from caspermeijn December 23, 2024 05:04
@caspermeijn
Copy link
Collaborator

I think this test will pass without the other changes.

I'm not sure what you mean by "other changes" here.

When I only apply the changes in tests/, then the tests pass. See for example this commit: caspermeijn@06ee2ae

This means that either your tests are incomplete or that the code changes in code_generator are not necessary.

Comment on lines +3 to +10
cfg_if! {
if #[cfg(feature = "edition-2015")] {
use boxed_field::foo::OneofField;
} else {
use foo::OneofField;
}
}

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;

@xxchan
Copy link
Author

xxchan commented Jan 3, 2025

Yes you are right. I found the problem is only for string oneof variants, but not message variant.

::prost::encoding::message::merge accepts M: Message, and we have impl<M> Message for Box<M>. But ::prost::encoding::string::merge only accepts &mut String. 🤔

@xxchan
Copy link
Author

xxchan commented Jan 3, 2025

But it seems not useful at all to box a string.

@caspermeijn
Copy link
Collaborator

But it seems not useful at all to box a string.

I agree, a Box<String> doesn't make sense. Maybe the code generator should prevent a string from being boxed.

Perhaps it should just ignore boxed when the type is string. If it currently doesn't compile, then it is not a breaking change to change the field type.

To improve the tests, you could try to reuse TestAllTypesProto3 and add .boxed(".") to the build.rs. That way, we can verify that all field types actually compile.

@xxchan xxchan changed the title fix: boxed oneof field fix: boxed string field Jan 3, 2025
@xxchan xxchan marked this pull request as draft January 3, 2025 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

prost_config::Config::boxed generates code doesn't compile
2 participants