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

Make prost a no_std compatible library, and prost-build able to gen… #215

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fc7a772
Make prost a `no_std` compatible library, and prost-build able to gen…
cbeck88 Aug 13, 2019
cc51c9d
WIP fixes to prost-derive, and porting of tests to alloc configuration
cbeck88 Sep 12, 2019
28b9326
fixups to prost-build, tests-alloc
cbeck88 Sep 12, 2019
01a500d
more WIP
cbeck88 Sep 12, 2019
21b5866
more WIP -- tests-alloc working, prost-derive configuration issue ide…
cbeck88 Sep 12, 2019
8e6d794
use git dependencies instead of patch
cbeck88 Sep 12, 2019
e5d878c
Use git dependencies for bytes, and use alt-failure crate in prost-de…
cbeck88 Sep 12, 2019
0804e3d
fix build failure in no_std mode
cbeck88 Sep 12, 2019
7b9beab
avoid need for user to declare dependency on bytes
cbeck88 Sep 13, 2019
af60702
uprev bytes to 0.5
cbeck88 Dec 10, 2019
da06e9c
Merge remote-tracking branch 'danburkert/master' into no_std
dflemstr Feb 24, 2020
a14e694
Make alloc into an attribute tag instead of a prost-build feature
dflemstr Feb 24, 2020
9758c14
Run bootstrap for prost-types
dflemstr Feb 24, 2020
4d1e923
Manually map to io::Error in prost-build
dflemstr Feb 24, 2020
e3015a6
Run cargo fmt
dflemstr Feb 24, 2020
af705d5
Merge pull request #1 from dflemstr/no_std
cbeck88 Feb 27, 2020
3939650
Remove "collections_lib_name" and "alloc" config tags
cbeck88 Mar 2, 2020
d61beee
Remove `alloc` proc_macro attributes from prost-types
cbeck88 Mar 2, 2020
2d77d5f
more fixes in prost-types
cbeck88 Mar 2, 2020
64af573
more fixes in prost-derive
cbeck88 Mar 2, 2020
fa0a5ed
Make prost-build not insert `, alloc` proc-macro attributes
cbeck88 Mar 2, 2020
51e4539
Remove code comments, which I think are from bad merge resolution
cbeck88 Mar 2, 2020
db72894
Remove "CollectionsLib" from prost-build, but leave "force_btree_map"
cbeck88 Mar 3, 2020
abe2005
Simplifications in prost crate itself
cbeck88 Mar 3, 2020
f2a241d
fixup in tests
cbeck88 Mar 3, 2020
4d7bf10
address many review comments
cbeck88 Mar 3, 2020
41e5bf7
remove unnecessary `extern crate alloc;`
cbeck88 Mar 3, 2020
f2bf500
Move std-dependent stuff into an inline module, per review
cbeck88 Mar 3, 2020
4dab985
fixup an earlier commit
cbeck88 Mar 3, 2020
8f2ba49
fix build
cbeck88 Mar 3, 2020
cc3574b
Make std_conversions inline module and move stuff there, per review
cbeck88 Mar 3, 2020
f5a7d00
Remove tests-infra and tests-alloc
cbeck88 Mar 3, 2020
490247d
Remove reference to removed config function
cbeck88 Mar 3, 2020
7a292b5
Fix build
cbeck88 Mar 3, 2020
7a0b294
remove noise in the diff
cbeck88 Mar 3, 2020
454a01d
Add missing alloc tests mod to tests lib
cbeck88 Mar 3, 2020
59d36dd
Remove leading :: in many prost-types and prost-build code generator
cbeck88 Mar 4, 2020
4e19053
Do hidden re-export of alloc crate from prost, use in prost-build, pr…
cbeck88 Mar 5, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Expand Up @@ -36,12 +36,13 @@ exclude = [
bench = false

[features]
default = ["prost-derive"]
default = ["prost-derive", "std"]
no-recursion-limit = []
std = [] # When std is disabled, we provide no_std support in prost

[dependencies]
bytes = "0.5"
prost-derive = { version = "0.6.1", path = "prost-derive", optional = true }
bytes = { version = "0.5", default-features = false }
prost-derive = { version = "0.6.1", path = "prost-derive", default-features = false, optional = true }

[dev-dependencies]
criterion = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion conformance/Cargo.toml
Expand Up @@ -6,7 +6,7 @@ publish = false
edition = "2018"

[dependencies]
bytes = "0.5"
bytes = { version = "0.5", default-features = false }
env_logger = { version = "0.7", default-features = false }
log = "0.4"
prost = { path = ".." }
Expand Down
6 changes: 3 additions & 3 deletions prost-build/Cargo.toml
Expand Up @@ -10,14 +10,14 @@ description = "A Protocol Buffers implementation for the Rust Language."
edition = "2018"

[dependencies]
bytes = "0.5"
bytes = { version = "0.5", default-features = false }
heck = "0.3"
itertools = "0.8"
log = "0.4"
multimap = { version = "0.8", default-features = false }
petgraph = { version = "0.5", default-features = false }
prost = { version = "0.6.1", path = ".." }
prost-types = { version = "0.6.1", path = "../prost-types" }
prost = { version = "0.6.1", path = "..", default-features = false }
prost-types = { version = "0.6.1", path = "../prost-types", default-features = false }
tempfile = "3"

[build-dependencies]
Expand Down
29 changes: 17 additions & 12 deletions prost-build/src/code_generator.rs
Expand Up @@ -330,6 +330,7 @@ impl<'a> CodeGenerator<'a> {
if boxed {
self.buf.push_str(", boxed");
}

self.buf.push_str(", tag=\"");
self.buf.push_str(&field.number().to_string());

Expand Down Expand Up @@ -374,12 +375,12 @@ impl<'a> CodeGenerator<'a> {
self.buf.push_str(&to_snake(field.name()));
self.buf.push_str(": ");
if repeated {
self.buf.push_str("::std::vec::Vec<");
self.buf.push_str("::alloc::vec::Vec<");
} else if optional {
self.buf.push_str("::std::option::Option<");
self.buf.push_str("::core::option::Option<");
}
if boxed {
self.buf.push_str("::std::boxed::Box<");
self.buf.push_str("::alloc::boxed::Box<");
}
self.buf.push_str(&ty);
if boxed {
Expand Down Expand Up @@ -416,10 +417,10 @@ impl<'a> CodeGenerator<'a> {
.btree_map
.iter()
.any(|matcher| match_ident(matcher, msg_name, Some(field.name())));
let (annotation_ty, rust_ty) = if btree_map {
("btree_map", "BTreeMap")
let (annotation_ty, lib_name, rust_ty) = if btree_map {
("btree_map", "::alloc::collections", "BTreeMap")
} else {
("map", "HashMap")
("map", "::std::collections", "HashMap")
};

let key_tag = self.field_type_tag(key);
Expand All @@ -434,8 +435,9 @@ impl<'a> CodeGenerator<'a> {
self.append_field_attributes(msg_name, field.name());
self.push_indent();
self.buf.push_str(&format!(
"pub {}: ::std::collections::{}<{}, {}>,\n",
"pub {}: {}::{}<{}, {}>,\n",
to_snake(field.name()),
lib_name,
rust_ty,
key_ty,
value_ty
Expand Down Expand Up @@ -467,7 +469,7 @@ impl<'a> CodeGenerator<'a> {
self.append_field_attributes(fq_message_name, oneof.name());
self.push_indent();
self.buf.push_str(&format!(
"pub {}: ::std::option::Option<{}>,\n",
"pub {}: ::core::option::Option<{}>,\n",
to_snake(oneof.name()),
name
));
Expand Down Expand Up @@ -528,8 +530,11 @@ impl<'a> CodeGenerator<'a> {
);

if boxed {
self.buf
.push_str(&format!("{}(Box<{}>),\n", to_upper_camel(field.name()), ty));
self.buf.push_str(&format!(
"{}(::alloc::boxed::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 @@ -721,8 +726,8 @@ 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 => String::from("std::string::String"),
Type::Bytes => String::from("std::vec::Vec<u8>"),
Type::String => String::from("::alloc::string::String"),
Type::Bytes => String::from("::alloc::vec::Vec<u8>"),
Type::Group | Type::Message => self.resolve_ident(field.type_name()),
}
}
Expand Down
5 changes: 4 additions & 1 deletion prost-build/src/lib.rs
Expand Up @@ -547,7 +547,10 @@ impl Config {
}

let buf = fs::read(descriptor_set)?;
let descriptor_set = FileDescriptorSet::decode(&*buf)?;
// Manually map error, because in no_std mode, prost::DecodeError will not implement
// Into<io::Error>
let descriptor_set = FileDescriptorSet::decode(&buf[..])
.map_err(|error| Error::new(ErrorKind::InvalidData, format!("{}", error)))?;

let modules = self.generate(descriptor_set.file)?;
for (module, content) in modules {
Expand Down
2 changes: 1 addition & 1 deletion prost-derive/src/field/group.rs
Expand Up @@ -126,7 +126,7 @@ impl Field {

pub fn clear(&self, ident: TokenStream) -> TokenStream {
match self.label {
Label::Optional => quote!(#ident = ::std::option::Option::None),
Label::Optional => quote!(#ident = ::core::option::Option::None),
Label::Required => quote!(#ident.clear()),
Label::Repeated => quote!(#ident.clear()),
}
Expand Down
22 changes: 14 additions & 8 deletions prost-derive/src/field/map.rs
Expand Up @@ -272,11 +272,11 @@ impl Field {
let insert_doc = format!("Inserts a key value pair into `{}`.", ident);
Some(quote! {
#[doc=#get_doc]
pub fn #get(&self, key: #key_ref_ty) -> ::std::option::Option<#ty> {
pub fn #get(&self, key: #key_ref_ty) -> ::core::option::Option<#ty> {
self.#ident.get(#take_ref key).cloned().and_then(#ty::from_i32)
}
#[doc=#insert_doc]
pub fn #insert(&mut self, key: #key_ty, value: #ty) -> ::std::option::Option<#ty> {
pub fn #insert(&mut self, key: #key_ty, value: #ty) -> ::core::option::Option<#ty> {
self.#ident.insert(key, value as i32).and_then(#ty::from_i32)
}
})
Expand All @@ -294,12 +294,18 @@ impl Field {
MapTy::HashMap => Ident::new("HashMap", Span::call_site()),
MapTy::BTreeMap => Ident::new("BTreeMap", Span::call_site()),
};
// HashMap is in std, BTreeMap is in alloc
let libname = match self.map_ty {
MapTy::HashMap => Ident::new("std", Span::call_site()),
MapTy::BTreeMap => Ident::new("alloc", Span::call_site()),
};

// A fake field for generating the debug wrapper
let key_wrapper = fake_scalar(self.key_ty.clone()).debug(quote!(KeyWrapper));
let key = self.key_ty.rust_type();
let value_wrapper = self.value_ty.debug();
let fmt = quote! {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
#key_wrapper
#value_wrapper
let mut builder = f.debug_map();
Expand All @@ -313,17 +319,17 @@ impl Field {
ValueTy::Scalar(ref ty) => {
let value = ty.rust_type();
quote! {
struct #wrapper_name<'a>(&'a ::std::collections::#type_name<#key, #value>);
impl<'a> ::std::fmt::Debug for #wrapper_name<'a> {
struct #wrapper_name<'a>(&'a ::#libname::collections::#type_name<#key, #value>);
impl<'a> ::core::fmt::Debug for #wrapper_name<'a> {
#fmt
}
}
}
ValueTy::Message => quote! {
struct #wrapper_name<'a, V: 'a>(&'a ::std::collections::#type_name<#key, V>);
impl<'a, V> ::std::fmt::Debug for #wrapper_name<'a, V>
struct #wrapper_name<'a, V: 'a>(&'a ::#libname::collections::#type_name<#key, V>);
impl<'a, V> ::core::fmt::Debug for #wrapper_name<'a, V>
where
V: ::std::fmt::Debug + 'a,
V: ::core::fmt::Debug + 'a,
{
#fmt
}
Expand Down
2 changes: 1 addition & 1 deletion prost-derive/src/field/message.rs
Expand Up @@ -126,7 +126,7 @@ impl Field {

pub fn clear(&self, ident: TokenStream) -> TokenStream {
match self.label {
Label::Optional => quote!(#ident = ::std::option::Option::None),
Label::Optional => quote!(#ident = ::core::option::Option::None),
Label::Required => quote!(#ident.clear()),
Label::Repeated => quote!(#ident.clear()),
}
Expand Down
6 changes: 3 additions & 3 deletions prost-derive/src/field/mod.rs
Expand Up @@ -4,8 +4,8 @@ mod message;
mod oneof;
mod scalar;

use std::fmt;
use std::slice;
use core::fmt;
use core::slice;

use anyhow::{bail, Error};
use proc_macro2::TokenStream;
Expand Down Expand Up @@ -135,7 +135,7 @@ impl Field {
pub fn default(&self) -> TokenStream {
match *self {
Field::Scalar(ref scalar) => scalar.default(),
_ => quote!(::std::default::Default::default()),
_ => quote!(::core::default::Default::default()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion prost-derive/src/field/oneof.rs
Expand Up @@ -94,6 +94,6 @@ impl Field {
}

pub fn clear(&self, ident: TokenStream) -> TokenStream {
quote!(#ident = ::std::option::Option::None)
quote!(#ident = ::core::option::Option::None)
}
}