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

Add no_std support #145

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions generate_modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ proto_sets=(
quick-protobuf/examples/pb_rs_v3
quick-protobuf/tests/packed_primitives
quick-protobuf/tests/rust_protobuf/common
quick-protobuf/no-std-example/src
)

for ps in "${proto_sets[@]}"; do
Expand Down
28 changes: 27 additions & 1 deletion pb-rs/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ named!(
do_parse!(
tag!("import")
>> many1!(br)
>> opt!(tag!("public"))
>> opt!(multispace)
>> tag!("\"")
>> path: map!(map_res!(take_until!("\""), str::from_utf8), |s| Path::new(
s
Expand Down Expand Up @@ -148,12 +150,18 @@ named!(
)
);

named!(
option_key<&str>,
alt_complete!(delimited!(tag!("("), word_ref, tag!(")"))
| word_ref)
);

named!(
key_val<(&str, &str)>,
do_parse!(
tag!("[")
>> many0!(br)
>> key: word_ref
>> key: option_key
>> many0!(br)
>> tag!("=")
>> many0!(br)
Expand Down Expand Up @@ -265,6 +273,12 @@ named!(
.find(|&&(k, _)| k == "deprecated")
.map_or(false, |&(_, v)| str::FromStr::from_str(v)
.expect("Cannot parse Deprecated value")),
max_length: key_vals
.iter()
.find(|&&(k, _)| k == "rust_max_length" || k == "rust_ext.rust_max_length")
.map(|&(_, v)| v
.parse::<u32>()
.expect("Cannot parse rust_max_length value")),
})
)
);
Expand Down Expand Up @@ -667,4 +681,16 @@ mod test {
}
}

#[test]
fn test_option_key_plain() {
let s = option_key("test".as_bytes()).unwrap().1;
assert_eq!("test", s);
}

#[test]
fn test_option_key_parens() {
let s = option_key("(test)".as_bytes()).unwrap().1;
assert_eq!("test", s);
}

}
44 changes: 33 additions & 11 deletions pb-rs/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ impl FieldType {
fn has_lifetime(
&self,
desc: &FileDescriptor,
max_length: bool,
packed: bool,
ignore: &mut Vec<MessageIndex>,
) -> bool {
Expand All @@ -277,9 +278,10 @@ impl FieldType {
| FieldType::Sfixed32
| FieldType::String_
| FieldType::Bytes_
| FieldType::Float => packed, // Cow<[M]>
| FieldType::Float => !max_length && packed, // Cow<[M]>
FieldType::Map(ref key, ref value) => {
key.has_lifetime(desc, false, ignore) || value.has_lifetime(desc, false, ignore)
key.has_lifetime(desc, false, false, ignore)
|| value.has_lifetime(desc, false, false, ignore)
}
_ => false,
}
Expand Down Expand Up @@ -432,9 +434,14 @@ pub struct Field {
pub packed: Option<bool>,
pub boxed: bool,
pub deprecated: bool,
pub max_length: Option<u32>,
}

impl Field {
fn max_length(&self) -> bool {
self.max_length.is_some()
}

fn packed(&self) -> bool {
self.packed.unwrap_or(false)
}
Expand Down Expand Up @@ -495,6 +502,12 @@ impl Field {
{
writeln!(w, "Option<{}>,", rust_type)?
}
Frequency::Repeated if self.max_length.is_some() => writeln!(
w,
"arrayvec::ArrayVec<[{}; {}]>,",
rust_type,
self.max_length.unwrap()
)?,
Frequency::Repeated
if self.packed() && self.typ.is_fixed_size() && !config.dont_use_cow =>
{
Expand Down Expand Up @@ -540,6 +553,13 @@ impl Field {
Frequency::Required | Frequency::Optional => {
writeln!(w, "msg.{} = {},", name, val_cow)?
}
Frequency::Repeated if self.packed() && self.max_length.is_some() => {
writeln!(
w,
"msg.{} = r.read_packed_arrayvec(bytes, |r, bytes| Ok({}))?,",
name, val
)?;
}
Frequency::Repeated if self.packed() && self.typ.is_fixed_size() => {
writeln!(w, "msg.{} = r.read_packed_fixed(bytes)?.into(),", name)?;
}
Expand Down Expand Up @@ -800,9 +820,10 @@ impl Message {
return false;
}
ignore.push(self.index.clone());
let res = self
.all_fields()
.any(|f| f.typ.has_lifetime(desc, f.packed(), ignore));
let res = self.all_fields().any(|f| {
f.typ
.has_lifetime(desc, f.max_length(), f.packed(), ignore)
});
ignore.pop();
res
}
Expand Down Expand Up @@ -1137,7 +1158,7 @@ impl Message {
fn write_write_message<W: Write>(&self, w: &mut W, desc: &FileDescriptor) -> Result<()> {
writeln!(
w,
" fn write_message<W: Write>(&self, w: &mut Writer<W>) -> Result<()> {{"
" fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {{"
)?;
for f in self.fields.iter().filter(|f| !f.deprecated) {
f.write_write(w, desc)?;
Expand Down Expand Up @@ -1434,9 +1455,11 @@ pub struct OneOf {

impl OneOf {
fn has_lifetime(&self, desc: &FileDescriptor) -> bool {
self.fields
.iter()
.any(|f| !f.deprecated && f.typ.has_lifetime(desc, f.packed(), &mut Vec::new()))
self.fields.iter().any(|f| {
!f.deprecated
&& f.typ
.has_lifetime(desc, f.max_length(), f.packed(), &mut Vec::new())
})
}

fn set_package(&mut self, package: &str, module: &str) {
Expand Down Expand Up @@ -2145,7 +2168,6 @@ impl FileDescriptor {
)?;
return Ok(());
}
writeln!(w, "use std::io::Write;")?;
if self
.messages
.iter()
Expand All @@ -2162,7 +2184,7 @@ impl FileDescriptor {
}
writeln!(
w,
"use quick_protobuf::{{MessageRead, MessageWrite, BytesReader, Writer, Result}};"
"use quick_protobuf::{{MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}};"
)?;

if self.owned {
Expand Down
3 changes: 2 additions & 1 deletion perftest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build = "build.rs"

[dependencies]
protobuf = "2.0.4"
quick-protobuf = { path = "../quick-protobuf" }
quick-protobuf = { path = "../quick-protobuf", default-features = false, features = ["with_arrayvec"] }
time = "0.1.40"
rand = "0.5.5"
prost = "0.4.0"
Expand All @@ -19,3 +19,4 @@ failure = "0.1.5"
protobuf-codegen-pure = "2.0.4"
pb-rs = { path = "../pb-rs" }
prost-build = "0.4.0"
[features]
Loading