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

chore: improve no_std maintainability: #1047

Draft
wants to merge 4 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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,21 +332,21 @@ and the generated Rust code (`tutorial.rs`):
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Person {
#[prost(string, tag="1")]
pub name: ::prost::alloc::string::String,
pub name: String,
/// Unique ID number for this person.
#[prost(int32, tag="2")]
pub id: i32,
#[prost(string, tag="3")]
pub email: ::prost::alloc::string::String,
pub email: String,
#[prost(message, repeated, tag="4")]
pub phones: ::prost::alloc::vec::Vec<person::PhoneNumber>,
pub phones: Vec<person::PhoneNumber>,
}
/// Nested message and enum types in `Person`.
pub mod person {
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct PhoneNumber {
#[prost(string, tag="1")]
pub number: ::prost::alloc::string::String,
pub number: String,
#[prost(enumeration="PhoneType", tag="2")]
pub r#type: i32,
}
Expand All @@ -362,7 +362,7 @@ pub mod person {
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AddressBook {
#[prost(message, repeated, tag="1")]
pub people: ::prost::alloc::vec::Vec<Person>,
pub people: Vec<Person>,
}
```

Expand Down
3 changes: 1 addition & 2 deletions conformance/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::io::{self, Read, Write};

use bytes::{Buf, BufMut};
use prost::facade::*;
use prost::Message;

use protobuf::conformance::{
Expand Down
40 changes: 17 additions & 23 deletions prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use std::ascii;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::iter;

use itertools::{Either, Itertools};
use log::debug;
use multimap::MultiMap;
use prost::facade::*;
use prost_types::field_descriptor_proto::{Label, Type};
use prost_types::source_code_info::Location;
use prost_types::{
Expand Down Expand Up @@ -88,6 +84,8 @@ impl<'a> CodeGenerator<'a> {
buf,
};

code_gen.add_prelude();

debug!(
"file: {:?}, package: {:?}",
file.name.as_ref().unwrap(),
Expand Down Expand Up @@ -286,10 +284,6 @@ impl<'a> CodeGenerator<'a> {
"const PACKAGE: &'static str = \"{}\";\n",
self.package,
));

let prost_path = self.config.prost_path.as_deref().unwrap_or("::prost");
let string_path = format!("{prost_path}::alloc::string::String");

let full_name = format!(
"{}{}{}{}{message_name}",
self.package.trim_matches('.'),
Expand All @@ -304,11 +298,11 @@ impl<'a> CodeGenerator<'a> {
.map_or("", |name| name.as_str());

self.buf.push_str(&format!(
r#"fn full_name() -> {string_path} {{ "{full_name}".into() }}"#,
r#"fn full_name() -> String {{ "{full_name}".into() }}"#,
));

self.buf.push_str(&format!(
r#"fn type_url() -> {string_path} {{ "{domain_name}/{full_name}".into() }}"#,
r#"fn type_url() -> String {{ "{domain_name}/{full_name}".into() }}"#,
));

self.depth -= 1;
Expand Down Expand Up @@ -479,17 +473,13 @@ impl<'a> CodeGenerator<'a> {
self.buf.push_str(&to_snake(field.name()));
self.buf.push_str(": ");

let prost_path = prost_path(self.config);

if repeated {
self.buf
.push_str(&format!("{}::alloc::vec::Vec<", prost_path));
self.buf.push_str("Vec<");
} else if optional {
self.buf.push_str("::core::option::Option<");
}
if boxed {
self.buf
.push_str(&format!("{}::alloc::boxed::Box<", prost_path));
self.buf.push_str("Box<");
}
self.buf.push_str(&ty);
if boxed {
Expand Down Expand Up @@ -644,11 +634,8 @@ impl<'a> CodeGenerator<'a> {
);

if boxed {
self.buf.push_str(&format!(
"{}(::prost::alloc::boxed::Box<{}>),\n",
to_upper_camel(field.name()),
ty
));
self.buf
.push_str(&format!("{}(Box<{}>),\n", to_upper_camel(field.name()), ty));
} else {
self.buf
.push_str(&format!("{}({}),\n", to_upper_camel(field.name()), ty));
Expand Down Expand Up @@ -911,6 +898,7 @@ impl<'a> CodeGenerator<'a> {
self.buf.push_str("pub mod ");
self.buf.push_str(&to_snake(module));
self.buf.push_str(" {\n");
self.add_prelude();

self.type_path.push(module.into());

Expand All @@ -935,7 +923,7 @@ impl<'a> CodeGenerator<'a> {
Type::Int32 | Type::Sfixed32 | Type::Sint32 | Type::Enum => String::from("i32"),
Type::Int64 | Type::Sfixed64 | Type::Sint64 => String::from("i64"),
Type::Bool => String::from("bool"),
Type::String => format!("{}::alloc::string::String", prost_path(self.config)),
Type::String => "String".to_string(),
Type::Bytes => self
.config
.bytes_type
Expand Down Expand Up @@ -1056,6 +1044,12 @@ impl<'a> CodeGenerator<'a> {
message_name,
)
}

fn add_prelude(&mut self) {
self.buf.push_str("#[allow(unused_imports)]");
self.buf
.push_str(&format!("use {}::facade::*;", prost_path(self.config)));
}
}

/// Returns `true` if the repeated field type can be packed.
Expand Down
9 changes: 5 additions & 4 deletions prost-build/src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#[non_exhaustive]
#[derive(Default, Clone, Copy, Debug, PartialEq)]
pub(crate) enum MapType {
// IMPROVEMENT: place behind std feature flag
/// The [`std::collections::HashMap`] type.
#[default]
HashMap,
/// The [`std::collections::BTreeMap`] type.
/// The [`alloc::collections::BTreeMap`] type.
BTreeMap,
}

Expand All @@ -32,8 +33,8 @@ impl MapType {
/// 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",
MapType::HashMap => "HashMap",
MapType::BTreeMap => "BTreeMap",
}
}
}
Expand All @@ -50,7 +51,7 @@ impl BytesType {
/// 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::Vec => "Vec<u8>",
BytesType::Bytes => "::prost::bytes::Bytes",
}
}
Expand Down
4 changes: 1 addition & 3 deletions prost-build/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::collections::HashMap;
use std::default;
use std::env;
use std::ffi::{OsStr, OsString};
use std::fmt;
use std::fs;
Expand All @@ -11,6 +8,7 @@ use std::process::Command;
use log::debug;
use log::trace;

use prost::facade::*;
use prost::Message;
use prost_types::{FileDescriptorProto, FileDescriptorSet};

Expand Down
8 changes: 4 additions & 4 deletions prost-build/src/extern_paths.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::{hash_map, HashMap};

use itertools::Itertools;

use crate::ident::{to_snake, to_upper_camel};

use prost::facade::*;

fn validate_proto_path(path: &str) -> Result<(), String> {
if path.chars().next().map(|c| c != '.').unwrap_or(true) {
return Err(format!(
Expand Down Expand Up @@ -37,7 +37,7 @@ impl ExternPaths {
extern_paths.insert(".google.protobuf.BoolValue".to_string(), "bool".to_string())?;
extern_paths.insert(
".google.protobuf.BytesValue".to_string(),
"::prost::alloc::vec::Vec<u8>".to_string(),
"Vec<u8>".to_string(),
)?;
extern_paths.insert(
".google.protobuf.DoubleValue".to_string(),
Expand All @@ -49,7 +49,7 @@ impl ExternPaths {
extern_paths.insert(".google.protobuf.Int64Value".to_string(), "i64".to_string())?;
extern_paths.insert(
".google.protobuf.StringValue".to_string(),
"::prost::alloc::string::String".to_string(),
"String".to_string(),
)?;
extern_paths.insert(
".google.protobuf.UInt32Value".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Container {
#[prost(oneof="container::Data", tags="1, 2")]
#[prost(oneof = "container::Data", tags = "1, 2")]
pub data: ::core::option::Option<container::Data>,
}
/// Nested message and enum types in `Container`.
pub mod container {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Oneof)]
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Data {
#[prost(message, tag="1")]
Foo(::prost::alloc::boxed::Box<super::Foo>),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe the generated code should always use a fully qualified type path, as they could otherwise clash with a user-generated type.

#[prost(message, tag="2")]
#[prost(message, tag = "1")]
Foo(Box<super::Foo>),
#[prost(message, tag = "2")]
Bar(super::Bar),
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Foo {
#[prost(string, tag="1")]
pub foo: ::prost::alloc::string::String,
#[prost(string, tag = "1")]
pub foo: String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Bar {
#[prost(message, optional, boxed, tag="1")]
pub qux: ::core::option::Option<::prost::alloc::boxed::Box<Qux>>,
#[prost(message, optional, boxed, tag = "1")]
pub qux: ::core::option::Option<Box<Qux>>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Qux {
}
pub struct Qux {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Container {
#[prost(oneof = "container::Data", tags = "1, 2")]
pub data: ::core::option::Option<container::Data>,
pub data: Option<container::Data>,
}
/// Nested message and enum types in `Container`.
pub mod container {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Data {
#[prost(message, tag = "1")]
Foo(::prost::alloc::boxed::Box<super::Foo>),
Foo(Box<super::Foo>),
#[prost(message, tag = "2")]
Bar(super::Bar),
}
Expand All @@ -20,13 +20,13 @@ pub mod container {
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Foo {
#[prost(string, tag = "1")]
pub foo: ::prost::alloc::string::String,
pub foo: String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Bar {
#[prost(message, optional, boxed, tag = "1")]
pub qux: ::core::option::Option<::prost::alloc::boxed::Box<Qux>>,
pub qux: Option<Box<Qux>>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down
8 changes: 4 additions & 4 deletions prost-build/src/fixtures/helloworld/_expected_helloworld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Message {
#[prost(string, tag="1")]
pub say: ::prost::alloc::string::String,
#[prost(string, tag = "1")]
pub say: String,
}
#[derive(derive_builder::Builder)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Response {
#[prost(string, tag="1")]
pub say: ::prost::alloc::string::String,
#[prost(string, tag = "1")]
pub say: String,
}
#[some_enum_attr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Message {
#[prost(string, tag = "1")]
pub say: ::prost::alloc::string::String,
pub say: String,
}
#[derive(derive_builder::Builder)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Response {
#[prost(string, tag = "1")]
pub say: ::prost::alloc::string::String,
pub say: String,
}
#[some_enum_attr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
Expand Down
Loading