Skip to content
Open
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
2 changes: 1 addition & 1 deletion buffa-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ fn generate_include_file(entries: &[(String, String)], relative: bool) -> String
#[derive(Default)]
struct ModNode {
files: Vec<String>,
children: BTreeMap<String, ModNode>,
children: BTreeMap<String, Self>,
}

let mut root = ModNode::default();
Expand Down
2 changes: 1 addition & 1 deletion buffa-codegen/src/impl_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ pub fn generate_message_impl(
fn default_instance() -> &'static Self {
static VALUE: ::buffa::__private::OnceBox<#name_ident>
= ::buffa::__private::OnceBox::new();
VALUE.get_or_init(|| ::buffa::alloc::boxed::Box::new(#name_ident::default()))
VALUE.get_or_init(|| ::buffa::alloc::boxed::Box::new(Self::default()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion buffa-codegen/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl ImportResolver {
blocked.insert(name.to_string());
}
}
ImportResolver { blocked }
Self { blocked }
}

/// Emit the `use` block for the top of a generated file.
Expand Down
2 changes: 1 addition & 1 deletion buffa-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub fn generate_module_tree(
#[derive(Default)]
struct ModNode {
files: Vec<String>,
children: BTreeMap<String, ModNode>,
children: BTreeMap<String, Self>,
}

let mut root = ModNode::default();
Expand Down
37 changes: 30 additions & 7 deletions buffa-codegen/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ fn custom_deser_oneof_group(
{
quote! { ::bytes::Bytes }
} else {
scalar_or_message_type(ctx, field, current_package, features, resolver)?
scalar_or_message_type(ctx, field, current_package, features, resolver, None)?
};

// Module-qualified path for the oneof enum (lives in the message's module).
Expand Down Expand Up @@ -937,6 +937,9 @@ fn classify_field(
// Check if this bytes field should use bytes::Bytes.
let field_name = field.name.as_deref().unwrap_or("");
let field_fqn = format!(".{}.{}", proto_fqn, field_name);
// Dotted FQN for the current message, matching FieldDescriptorProto's
// type_name convention. Used to emit `Self` for self-referential fields.
let self_fqn = format!(".{proto_fqn}");
let use_bytes = field_type == Type::TYPE_BYTES && ctx.use_bytes_type(&field_fqn);

let bytes_type = if use_bytes {
Expand All @@ -952,14 +955,21 @@ fn classify_field(
let elem = if field_type == Type::TYPE_BYTES {
bytes_type.clone()
} else {
scalar_or_message_type(ctx, field, current_package, features, resolver)?
scalar_or_message_type(
ctx,
field,
current_package,
features,
resolver,
Some(&self_fqn),
)?
};
{
let vec = resolver.vec();
quote! { #vec<#elem> }
}
} else if field_type == Type::TYPE_MESSAGE || field_type == Type::TYPE_GROUP {
let inner = resolve_message_type(ctx, field, current_package, 0)?;
let inner = resolve_message_type(ctx, field, current_package, 0, Some(&self_fqn))?;
{
let mf = resolver.message_field();
quote! { #mf<#inner> }
Expand Down Expand Up @@ -1158,8 +1168,10 @@ fn map_rust_type_from_entry(
.find(|f| f.number == Some(2))
.ok_or(CodeGenError::MissingField("map_entry.value"))?;

let key_type = scalar_or_message_type(ctx, key_field, current_package, features, resolver)?;
let value_type = scalar_or_message_type(ctx, value_field, current_package, features, resolver)?;
let key_type =
scalar_or_message_type(ctx, key_field, current_package, features, resolver, None)?;
let value_type =
scalar_or_message_type(ctx, value_field, current_package, features, resolver, None)?;

let hm = resolver.hashmap();
Ok(quote! { #hm<#key_type, #value_type> })
Expand All @@ -1177,8 +1189,9 @@ pub fn scalar_or_message_type(
current_package: &str,
features: &ResolvedFeatures,
resolver: &crate::imports::ImportResolver,
self_fqn: Option<&str>,
) -> Result<TokenStream, CodeGenError> {
scalar_or_message_type_nested(ctx, field, current_package, 0, features, resolver)
scalar_or_message_type_nested(ctx, field, current_package, 0, features, resolver, self_fqn)
}

/// Like [`scalar_or_message_type`] but with explicit module nesting depth.
Expand All @@ -1189,10 +1202,11 @@ pub fn scalar_or_message_type_nested(
nesting: usize,
features: &ResolvedFeatures,
resolver: &crate::imports::ImportResolver,
self_fqn: Option<&str>,
) -> Result<TokenStream, CodeGenError> {
match crate::impl_message::effective_type(ctx, field, features) {
Type::TYPE_MESSAGE | Type::TYPE_GROUP => {
resolve_message_type(ctx, field, current_package, nesting)
resolve_message_type(ctx, field, current_package, nesting, self_fqn)
}
Type::TYPE_ENUM => {
resolve_enum_type(ctx, field, current_package, nesting, features, resolver)
Expand All @@ -1206,11 +1220,20 @@ fn resolve_message_type(
field: &crate::generated::descriptor::FieldDescriptorProto,
current_package: &str,
nesting: usize,
self_fqn: Option<&str>,
) -> Result<TokenStream, CodeGenError> {
let type_name = field
.type_name
.as_deref()
.ok_or(CodeGenError::MissingField("field.type_name"))?;
// If the field's message type is the same message currently being
// generated, emit `Self` instead of the concrete type path. Only
// applies when the caller is building struct fields (passes Some);
// map entries, oneof variants, and deserialize arms pass None since
// `Self` would bind to the wrong type in those contexts.
if self_fqn == Some(type_name) {
return Ok(quote! { Self });
}
let path_str = ctx
.rust_type_relative(type_name, current_package, nesting)
.ok_or_else(|| {
Expand Down
25 changes: 16 additions & 9 deletions buffa-codegen/src/oneof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,20 @@ fn collect_variant_info(
// without codegen changes; only decode and JSON-deser need an
// explicit Vec<u8>→Bytes conversion (see oneof_merge_arm and
// oneof_variant_deser_arm).
let rust_type = if field_type == Type::TYPE_BYTES
&& field_uses_bytes(ctx, proto_fqn, proto_name)
{
quote! { ::bytes::Bytes }
} else {
scalar_or_message_type_nested(ctx, field, current_package, 1, features, resolver)?
};
let rust_type =
if field_type == Type::TYPE_BYTES && field_uses_bytes(ctx, proto_fqn, proto_name) {
quote! { ::bytes::Bytes }
} else {
scalar_or_message_type_nested(
ctx,
field,
current_package,
1,
features,
resolver,
None,
)?
};
Ok(VariantInfo {
variant_ident,
rust_type,
Expand Down Expand Up @@ -200,7 +207,7 @@ pub fn generate_oneof_enum(
let from_oneof = quote! {
impl From<#ty> for #rust_enum_ident {
fn from(v: #ty) -> Self {
#rust_enum_ident::#ident(::buffa::alloc::boxed::Box::new(v))
Self::#ident(::buffa::alloc::boxed::Box::new(v))
}
}
};
Expand All @@ -213,7 +220,7 @@ pub fn generate_oneof_enum(
quote! {
impl From<#ty> for ::core::option::Option<#rust_enum_ident> {
fn from(v: #ty) -> Self {
::core::option::Option::Some(#rust_enum_ident::from(v))
Self::Some(#rust_enum_ident::from(v))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion buffa-codegen/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ pub fn generate_view(
static VALUE: ::buffa::__private::OnceBox<#view_ident<'static>>
= ::buffa::__private::OnceBox::new();
VALUE.get_or_init(|| ::buffa::alloc::boxed::Box::new(
#view_ident::default(),
Self::default(),
))
}
}
Expand Down
14 changes: 4 additions & 10 deletions buffa-descriptor/src/generated/google.protobuf.compiler.plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Version {
unsafe impl ::buffa::DefaultInstance for Version {
fn default_instance() -> &'static Self {
static VALUE: ::buffa::__private::OnceBox<Version> = ::buffa::__private::OnceBox::new();
VALUE.get_or_init(|| ::buffa::alloc::boxed::Box::new(Version::default()))
VALUE.get_or_init(|| ::buffa::alloc::boxed::Box::new(Self::default()))
}
}
impl ::buffa::Message for Version {
Expand Down Expand Up @@ -256,10 +256,7 @@ impl CodeGeneratorRequest {
unsafe impl ::buffa::DefaultInstance for CodeGeneratorRequest {
fn default_instance() -> &'static Self {
static VALUE: ::buffa::__private::OnceBox<CodeGeneratorRequest> = ::buffa::__private::OnceBox::new();
VALUE
.get_or_init(|| ::buffa::alloc::boxed::Box::new(
CodeGeneratorRequest::default(),
))
VALUE.get_or_init(|| ::buffa::alloc::boxed::Box::new(Self::default()))
}
}
impl ::buffa::Message for CodeGeneratorRequest {
Expand Down Expand Up @@ -515,10 +512,7 @@ impl CodeGeneratorResponse {
unsafe impl ::buffa::DefaultInstance for CodeGeneratorResponse {
fn default_instance() -> &'static Self {
static VALUE: ::buffa::__private::OnceBox<CodeGeneratorResponse> = ::buffa::__private::OnceBox::new();
VALUE
.get_or_init(|| ::buffa::alloc::boxed::Box::new(
CodeGeneratorResponse::default(),
))
VALUE.get_or_init(|| ::buffa::alloc::boxed::Box::new(Self::default()))
}
}
impl ::buffa::Message for CodeGeneratorResponse {
Expand Down Expand Up @@ -831,7 +825,7 @@ pub mod code_generator_response {
unsafe impl ::buffa::DefaultInstance for File {
fn default_instance() -> &'static Self {
static VALUE: ::buffa::__private::OnceBox<File> = ::buffa::__private::OnceBox::new();
VALUE.get_or_init(|| ::buffa::alloc::boxed::Box::new(File::default()))
VALUE.get_or_init(|| ::buffa::alloc::boxed::Box::new(Self::default()))
}
}
impl ::buffa::Message for File {
Expand Down
Loading
Loading