Compare commits

...

1 Commits

Author SHA1 Message Date
Jeffrey Griffin
907f7d6e71 add std feature for no-std compatibility 2019-09-21 17:51:06 -07:00
11 changed files with 72 additions and 42 deletions

View File

@ -33,17 +33,18 @@ exclude = [
]
[features]
default = ["prost-derive"]
default = ["prost-derive", "std"]
no-recursion-limit = []
std = []
[dependencies]
bytes = "0.4.7"
bytes = { version = "0.5", default-features = false }
prost-derive = { version = "0.5.0", path = "prost-derive", optional = true }
[dev-dependencies]
criterion = "0.2"
env_logger = { version = "0.6", default-features = false }
failure = "0.1"
failure = { version = "0.1", default-features = false }
log = "0.4"
protobuf = { version = "0", path = "protobuf" }
quickcheck = "0.8"

View File

@ -538,7 +538,7 @@ impl Config {
let mut buf = Vec::new();
fs::File::open(descriptor_set)?.read_to_end(&mut buf)?;
let descriptor_set = FileDescriptorSet::decode(&buf)?;
let descriptor_set = FileDescriptorSet::decode(&buf[..])?;
let modules = self.generate(descriptor_set.file)?;
for (module, content) in modules {

View File

@ -14,7 +14,7 @@ edition = "2018"
proc_macro = true
[dependencies]
failure = { version = "0.1", default-features = false, features = ["std"] }
failure = { version = "0.1", default-features = false }
itertools = "0.8"
proc-macro2 = "0.4.4"
quote = "0.6.3"

View File

@ -15,5 +15,5 @@ doctest = false
test = false
[dependencies]
bytes = "0.4.7"
prost = { version = "0.5.0", path = ".." }
bytes = { version = "0.5", default-features = false }
prost = { version = "0.5.0", path = "..", default-features = false }

View File

@ -6,7 +6,7 @@ publish = false
edition = "2018"
[dependencies]
bytes = "0.4.7"
bytes = "0.5"
prost = { path = ".." }
prost-types = { path = "../prost-types" }

View File

@ -2,10 +2,13 @@
//!
//! Meant to be used only from `Message` implementations.
use std::cmp::min;
use std::mem;
use std::u32;
use std::usize;
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use core::cmp::min;
use core::mem;
use core::u32;
use core::usize;
use ::bytes::{Buf, BufMut};
@ -1040,8 +1043,7 @@ pub mod group {
/// generic over `HashMap` and `BTreeMap`.
macro_rules! map {
($map_ty:ident) => {
use std::collections::$map_ty;
use std::hash::Hash;
use core::hash::Hash;
use crate::encoding::*;
@ -1225,11 +1227,17 @@ macro_rules! map {
};
}
#[cfg(feature = "std")]
pub mod hash_map {
use std::collections::HashMap;
map!(HashMap);
}
pub mod btree_map {
#[cfg(feature = "std")]
use std::collections::BTreeMap;
#[cfg(not(feature = "std"))]
use alloc::collections::BTreeMap;
map!(BTreeMap);
}
@ -1240,7 +1248,7 @@ mod test {
use std::io::Cursor;
use std::u64;
use ::bytes::{Bytes, BytesMut, IntoBuf};
use ::bytes::{Bytes, BytesMut};
use quickcheck::TestResult;
use crate::encoding::*;
@ -1266,7 +1274,7 @@ mod test {
let mut buf = BytesMut::with_capacity(expected_len);
encode(tag, value.borrow(), &mut buf);
let mut buf = buf.freeze().into_buf();
let mut buf = buf.freeze().to_bytes();
if buf.remaining() != expected_len {
return TestResult::error(format!(
@ -1366,7 +1374,7 @@ mod test {
let mut buf = BytesMut::with_capacity(expected_len);
encode(tag, value.borrow(), &mut buf);
let mut buf = buf.freeze().into_buf();
let mut buf = buf.freeze();
if buf.remaining() != expected_len {
return TestResult::error(format!(
@ -1443,11 +1451,11 @@ mod test {
assert_eq!(encoded_len_varint(value), encoded.len());
let roundtrip_value = decode_varint(&mut encoded.into_buf()).expect("decoding failed");
let roundtrip_value = decode_varint(&mut encoded).expect("decoding failed");
assert_eq!(value, roundtrip_value);
let roundtrip_value =
decode_varint_slow(&mut encoded.into_buf()).expect("slow decoding failed");
decode_varint_slow(&mut encoded).expect("slow decoding failed");
assert_eq!(value, roundtrip_value);
}

View File

@ -1,8 +1,11 @@
//! Protobuf encoding and decoding errors.
use std::borrow::Cow;
use alloc::borrow::Cow;
use alloc::vec::Vec;
use core::fmt;
#[cfg(feature = "std")]
use std::error;
use std::fmt;
#[cfg(feature = "std")]
use std::io;
/// A Protobuf message decoding error.
@ -54,12 +57,14 @@ impl fmt::Display for DecodeError {
}
}
#[cfg(feature = "std")]
impl error::Error for DecodeError {
fn description(&self) -> &str {
&self.description
}
}
#[cfg(feature = "std")]
impl From<DecodeError> for io::Error {
fn from(error: DecodeError) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, error)
@ -95,11 +100,15 @@ impl EncodeError {
pub fn remaining(&self) -> usize {
self.remaining
}
fn description(&self) -> &str {
"failed to encode Protobuf message: insufficient buffer capacity"
}
}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(error::Error::description(self))?;
f.write_str(EncodeError::description(self))?;
write!(
f,
" (required: {}, remaining: {})",
@ -108,12 +117,14 @@ impl fmt::Display for EncodeError {
}
}
#[cfg(feature = "std")]
impl error::Error for EncodeError {
fn description(&self) -> &str {
"failed to encode Protobuf message: insufficient buffer capacity"
EncodeError::description(self)
}
}
#[cfg(feature = "std")]
impl From<EncodeError> for io::Error {
fn from(error: EncodeError) -> io::Error {
io::Error::new(io::ErrorKind::InvalidInput, error)

View File

@ -1,5 +1,14 @@
#![doc(html_root_url = "https://docs.rs/prost/0.5.0")]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std as alloc;
#[cfg(feature = "std")]
extern crate core;
mod error;
mod message;
mod types;
@ -10,7 +19,7 @@ pub mod encoding;
pub use crate::error::{DecodeError, EncodeError};
pub use crate::message::Message;
use bytes::{BufMut, IntoBuf};
use bytes::{Buf, BufMut};
use crate::encoding::{decode_varint, encode_varint, encoded_len_varint};
@ -58,12 +67,11 @@ pub fn length_delimiter_len(length: usize) -> usize {
/// input is required to decode the full delimiter.
/// * If the supplied buffer contains more than 10 bytes, then the buffer contains an invalid
/// delimiter, and typically the buffer should be considered corrupt.
pub fn decode_length_delimiter<B>(buf: B) -> Result<usize, DecodeError>
pub fn decode_length_delimiter<B>(buf: &mut B) -> Result<usize, DecodeError>
where
B: IntoBuf,
B: Buf,
{
let mut buf = buf.into_buf();
let length = decode_varint(&mut buf)?;
let length = decode_varint(buf)?;
if length > usize::max_value() as u64 {
return Err(DecodeError::new(
"length delimiter exceeds maximum usize value",

View File

@ -1,7 +1,8 @@
use std::fmt::Debug;
use std::usize;
use alloc::boxed::Box;
use core::fmt::Debug;
use core::usize;
use ::bytes::{Buf, BufMut, IntoBuf};
use ::bytes::{Buf, BufMut};
use crate::encoding::*;
use crate::DecodeError;
@ -80,17 +81,17 @@ pub trait Message: Debug + Send + Sync {
/// The entire buffer will be consumed.
fn decode<B>(buf: B) -> Result<Self, DecodeError>
where
B: IntoBuf,
B: Buf,
Self: Default,
{
let mut message = Self::default();
Self::merge(&mut message, &mut buf.into_buf()).map(|_| message)
Self::merge(&mut message, buf).map(|_| message)
}
/// Decodes a length-delimited instance of the message from the buffer.
fn decode_length_delimited<B>(buf: B) -> Result<Self, DecodeError>
where
B: IntoBuf,
B: Buf,
Self: Default,
{
let mut message = Self::default();
@ -101,12 +102,11 @@ pub trait Message: Debug + Send + Sync {
/// Decodes an instance of the message from a buffer, and merges it into `self`.
///
/// The entire buffer will be consumed.
fn merge<B>(&mut self, buf: B) -> Result<(), DecodeError>
fn merge<B>(&mut self, mut buf: B) -> Result<(), DecodeError>
where
B: IntoBuf,
B: Buf,
Self: Sized,
{
let mut buf = buf.into_buf();
let ctx = DecodeContext::default();
while buf.has_remaining() {
let (tag, wire_type) = decode_key(&mut buf)?;
@ -117,15 +117,15 @@ pub trait Message: Debug + Send + Sync {
/// Decodes a length-delimited instance of the message from buffer, and
/// merges it into `self`.
fn merge_length_delimited<B>(&mut self, buf: B) -> Result<(), DecodeError>
fn merge_length_delimited<B>(&mut self, mut buf: B) -> Result<(), DecodeError>
where
B: IntoBuf,
B: Buf,
Self: Sized,
{
message::merge(
WireType::LengthDelimited,
self,
&mut buf.into_buf(),
&mut buf,
DecodeContext::default(),
)
}

View File

@ -5,6 +5,9 @@
//! the `prost-types` crate in order to avoid a cyclic dependency between `prost` and
//! `prost-build`.
use alloc::string::String;
use alloc::vec::Vec;
use ::bytes::{Buf, BufMut};
use crate::encoding::*;

View File

@ -76,7 +76,7 @@ pub mod groups {
use std::error::Error;
use bytes::{Buf, IntoBuf};
use bytes::Buf;
use prost::Message;
@ -180,7 +180,6 @@ where
msg.encode(&mut buf).unwrap();
assert_eq!(expected_len, buf.len());
let mut buf = buf.into_buf();
let roundtrip = M::decode(&mut buf).unwrap();
if buf.has_remaining() {