-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add the `std` feature to the quick-protobuf runtime crate - Put uses of Vec and std::io::Write behind the `std` feature - Change other uses of `std` to `core` - Add the `quick-protobuf/no-std-example` compile test
- Loading branch information
Showing
14 changed files
with
239 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "no-std-example" | ||
version = "0.1.0" | ||
authors = ["Russell Mull <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
quick-protobuf = { path = "..", default-features = false, features = ["with_arrayvec"] } | ||
arrayvec = { version = "0.4.11", default-features = false } | ||
|
||
[workspace] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This is just a compile-test; the actual functionality is all available in normal | ||
builds. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![no_std] | ||
|
||
use quick_protobuf::{serialize_into_slice, deserialize_from_slice}; | ||
use crate::protos::no_std::*; | ||
|
||
mod protos; | ||
|
||
pub fn round_trip() { | ||
let message = NoStdMessage::default(); | ||
|
||
let mut buf = [0u8; 1024]; | ||
serialize_into_slice(&message, &mut buf).unwrap(); | ||
|
||
let read_message = deserialize_from_slice(&buf).unwrap(); | ||
assert_eq!(message, read_message); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Automatically generated mod.rs | ||
pub mod protos; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
syntax = "proto3"; | ||
|
||
package protos.no_std; | ||
|
||
enum MyEnum { | ||
Val0 = 0; | ||
Val1 = 1; | ||
} | ||
|
||
message EmbeddedMessage { | ||
int32 val = 1; | ||
MyEnum e = 2; | ||
} | ||
|
||
message NoStdMessage { | ||
fixed32 num = 1; | ||
repeated fixed32 nums = 2 [rust_gen_arrayvec = 16]; | ||
EmbeddedMessage message = 3; | ||
repeated EmbeddedMessage messages = 4 [rust_gen_arrayvec = 16]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Automatically generated mod.rs | ||
pub mod no_std; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Automatically generated rust module for 'no_std.proto' file | ||
|
||
#![allow(non_snake_case)] | ||
#![allow(non_upper_case_globals)] | ||
#![allow(non_camel_case_types)] | ||
#![allow(unused_imports)] | ||
#![allow(unknown_lints)] | ||
#![allow(clippy)] | ||
#![cfg_attr(rustfmt, rustfmt_skip)] | ||
|
||
|
||
use quick_protobuf::{MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result}; | ||
use quick_protobuf::sizeofs::*; | ||
use super::super::*; | ||
|
||
#[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
pub enum MyEnum { | ||
Val0 = 0, | ||
Val1 = 1, | ||
} | ||
|
||
impl Default for MyEnum { | ||
fn default() -> Self { | ||
MyEnum::Val0 | ||
} | ||
} | ||
|
||
impl From<i32> for MyEnum { | ||
fn from(i: i32) -> Self { | ||
match i { | ||
0 => MyEnum::Val0, | ||
1 => MyEnum::Val1, | ||
_ => Self::default(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> From<&'a str> for MyEnum { | ||
fn from(s: &'a str) -> Self { | ||
match s { | ||
"Val0" => MyEnum::Val0, | ||
"Val1" => MyEnum::Val1, | ||
_ => Self::default(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Default, PartialEq, Clone)] | ||
pub struct EmbeddedMessage { | ||
pub val: i32, | ||
pub e: protos::no_std::MyEnum, | ||
} | ||
|
||
impl<'a> MessageRead<'a> for EmbeddedMessage { | ||
fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> { | ||
let mut msg = Self::default(); | ||
while !r.is_eof() { | ||
match r.next_tag(bytes) { | ||
Ok(8) => msg.val = r.read_int32(bytes)?, | ||
Ok(16) => msg.e = r.read_enum(bytes)?, | ||
Ok(t) => { r.read_unknown(bytes, t)?; } | ||
Err(e) => return Err(e), | ||
} | ||
} | ||
Ok(msg) | ||
} | ||
} | ||
|
||
impl MessageWrite for EmbeddedMessage { | ||
fn get_size(&self) -> usize { | ||
0 | ||
+ if self.val == 0i32 { 0 } else { 1 + sizeof_varint(*(&self.val) as u64) } | ||
+ if self.e == protos::no_std::MyEnum::Val0 { 0 } else { 1 + sizeof_varint(*(&self.e) as u64) } | ||
} | ||
|
||
fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> { | ||
if self.val != 0i32 { w.write_with_tag(8, |w| w.write_int32(*&self.val))?; } | ||
if self.e != protos::no_std::MyEnum::Val0 { w.write_with_tag(16, |w| w.write_enum(*&self.e as i32))?; } | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Debug, Default, PartialEq, Clone)] | ||
pub struct NoStdMessage { | ||
pub num: u32, | ||
pub nums: arrayvec::ArrayVec<[u32; 16]>, | ||
pub message: Option<protos::no_std::EmbeddedMessage>, | ||
pub messages: arrayvec::ArrayVec<[protos::no_std::EmbeddedMessage; 16]>, | ||
} | ||
|
||
impl<'a> MessageRead<'a> for NoStdMessage { | ||
fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> { | ||
let mut msg = Self::default(); | ||
while !r.is_eof() { | ||
match r.next_tag(bytes) { | ||
Ok(13) => msg.num = r.read_fixed32(bytes)?, | ||
Ok(18) => msg.nums = r.read_packed_arrayvec(bytes, |r, bytes| Ok(r.read_fixed32(bytes)?))?, | ||
Ok(26) => msg.message = Some(r.read_message::<protos::no_std::EmbeddedMessage>(bytes)?), | ||
Ok(34) => msg.messages.push(r.read_message::<protos::no_std::EmbeddedMessage>(bytes)?), | ||
Ok(t) => { r.read_unknown(bytes, t)?; } | ||
Err(e) => return Err(e), | ||
} | ||
} | ||
Ok(msg) | ||
} | ||
} | ||
|
||
impl MessageWrite for NoStdMessage { | ||
fn get_size(&self) -> usize { | ||
0 | ||
+ if self.num == 0u32 { 0 } else { 1 + 4 } | ||
+ if self.nums.is_empty() { 0 } else { 1 + sizeof_len(self.nums.len() * 4) } | ||
+ self.message.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size())) | ||
+ self.messages.iter().map(|s| 1 + sizeof_len((s).get_size())).sum::<usize>() | ||
} | ||
|
||
fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> { | ||
if self.num != 0u32 { w.write_with_tag(13, |w| w.write_fixed32(*&self.num))?; } | ||
w.write_packed_fixed_with_tag(18, &self.nums)?; | ||
if let Some(ref s) = self.message { w.write_with_tag(26, |w| w.write_message(s))?; } | ||
for s in &self.messages { w.write_with_tag(34, |w| w.write_message(s))?; } | ||
Ok(()) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.