Skip to content
Merged
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
40 changes: 27 additions & 13 deletions prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,25 @@ impl<'b> CodeGenerator<'_, 'b> {
));
self.append_field_attributes(fq_message_name, field.descriptor.name());
self.push_indent();
self.buf.push_str(&format!(
"pub {}: {}<{}, {}>,\n",
field.rust_name(),
map_type.rust_type(),
key_ty,
value_ty
));
match map_type {
crate::MapType::HashMap => {
self.buf.push_str(&format!(
"pub {}: ::std::collections::HashMap<{}, {}>,\n",
field.rust_name(),
key_ty,
value_ty
));
}
crate::MapType::BTreeMap => {
self.buf.push_str(&format!(
"pub {}: {}::alloc::collections::BTreeMap<{}, {}>,\n",
field.rust_name(),
self.context.prost_path(),
key_ty,
value_ty
));
}
}
}

fn append_oneof_field(
Expand Down Expand Up @@ -685,8 +697,9 @@ impl<'b> CodeGenerator<'_, 'b> {

if boxed {
self.buf.push_str(&format!(
"{}(::prost::alloc::boxed::Box<{}>),\n",
"{}({}::alloc::boxed::Box<{}>),\n",
to_upper_camel(field.descriptor.name()),
self.context.prost_path(),
ty
));
} else {
Expand Down Expand Up @@ -983,11 +996,12 @@ impl<'b> CodeGenerator<'_, 'b> {
Type::Int64 | Type::Sfixed64 | Type::Sint64 => String::from("i64"),
Type::Bool => String::from("bool"),
Type::String => format!("{}::alloc::string::String", self.context.prost_path()),
Type::Bytes => self
.context
.bytes_type(fq_message_name, field.name())
.rust_type()
.to_owned(),
Type::Bytes => match self.context.bytes_type(fq_message_name, field.name()) {
crate::BytesType::Vec => {
format!("{}::alloc::vec::Vec<u8>", self.context.prost_path())
}
crate::BytesType::Bytes => format!("{}::bytes::Bytes", self.context.prost_path()),
},
Type::Group | Type::Message => self.resolve_ident(field.type_name()),
}
}
Expand Down
16 changes: 0 additions & 16 deletions prost-build/src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ impl MapType {
MapType::BTreeMap => "btree_map",
}
}

/// The fully-qualified Rust type corresponding to the map type.
pub fn rust_type(&self) -> &'static str {
match self {
MapType::HashMap => "::std::collections::HashMap",
MapType::BTreeMap => "::prost::alloc::collections::BTreeMap",
}
}
}

impl BytesType {
Expand All @@ -46,12 +38,4 @@ impl BytesType {
BytesType::Bytes => "bytes",
}
}

/// The fully-qualified Rust type corresponding to the bytes type.
pub fn rust_type(&self) -> &'static str {
match self {
BytesType::Vec => "::prost::alloc::vec::Vec<u8>",
BytesType::Bytes => "::prost::bytes::Bytes",
}
}
}
Loading