├── .gitignore ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── noproto-derive ├── Cargo.toml └── src │ ├── field.rs │ └── lib.rs ├── rustfmt.toml └── src ├── impls.rs ├── lib.rs ├── read.rs └── write.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## Unreleased 9 | 10 | No unreleased changes yet. 11 | 12 | ## 0.1.0 - 2023-12-20 13 | 14 | - First release 15 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "noproto" 3 | version = "0.1.0" 4 | license = "MIT OR Apache-2.0" 5 | edition = "2021" 6 | description = "no-std, no-alloc protocol buffers implementation for embedded systems." 7 | repository = "https://github.com/embassy-rs/noproto" 8 | categories = [ 9 | "embedded", 10 | "no-std", 11 | "network-programming", 12 | "encoding", 13 | ] 14 | keywords = ["protobuf", "serialization"] 15 | 16 | [features] 17 | default = ["derive"] 18 | derive = ["dep:noproto-derive"] 19 | 20 | [dependencies] 21 | heapless = "0.8" 22 | noproto-derive = { version = "0.1.0", path = "noproto-derive", optional = true } 23 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2019-2022 Embassy project contributors 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019-2022 Embassy project contributors 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # noproto 2 | 3 | No-`std`, no-`alloc` protocol buffers (protobuf) implementation in Rust, for embedded systems. 4 | Optimized for binary size and memory usage, not for performance. 5 | 6 | Status: **very experimental,** :radioactive: do not use in production yet. In particular, it doesn't 7 | handle many protobuf types well (see below). 8 | 9 | ## Features 10 | 11 | Implemented: 12 | 13 | - Derive macros. 14 | - `heapless::Vec`, `heapless::String` impls. 15 | - `optional` 16 | - `repeated` 17 | - `oneof` 18 | - `enum` 19 | 20 | Not implemented (yet?): 21 | 22 | - Support multiple protobuf encodings. Protobuf "types" are more like "type + wire encoding" all in one, 23 | so one Rust type can be encoded multiple ways on the wire. `noproto` currently assumes the Rust type is enough 24 | to deduce how it should be encoded on the wire, which is not true. 25 | - Support more types (see below) 26 | - Impls for `alloc` containers (goal is to be `no-alloc`, but we could still have them optionally). 27 | - Impls for `&[T]` for repeated fields (only doable for writing, not reading) 28 | - Tool to compile `.proto` files into Rust code. 29 | - Maps 30 | - Deprecated field groups. 31 | 32 | ## Type mapping 33 | 34 | | Protobuf | Rust | 35 | |-|-| 36 | | `bool` | bool | 37 | | `int32` | TODO | 38 | | `uint32` | `u32` | 39 | | `sint32` | `i32` | 40 | | `fixed32` | TODO | 41 | | `sfixed32` | TODO | 42 | | `int64` | TODO | 43 | | `uint64` | `u64` | 44 | | `sint64` | `i64` | 45 | | `fixed64` | TODO | 46 | | `sfixed64` | TODO | 47 | | `float` | TODO | 48 | | `double` | TODO | 49 | | `string` | `heapless::String` | 50 | | `bytes` | `heapless::Vec` | 51 | 52 | ## Minimum supported Rust version (MSRV) 53 | 54 | `noproto` is guaranteed to compile on the latest stable Rust version at the time of release. It might compile with older versions but that may change in any new patch release. 55 | 56 | ## License 57 | 58 | This work is licensed under either of 59 | 60 | - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or 61 | ) 62 | - MIT license ([LICENSE-MIT](LICENSE-MIT) or ) 63 | 64 | at your option. 65 | -------------------------------------------------------------------------------- /noproto-derive/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "noproto-derive" 3 | version = "0.1.0" 4 | license = "MIT OR Apache-2.0" 5 | edition = "2021" 6 | description = "Derive macros for the `noproto` crate. Do not use this crate directly." 7 | repository = "https://github.com/embassy-rs/noproto" 8 | categories = [ 9 | "embedded", 10 | "no-std", 11 | "network-programming", 12 | "encoding", 13 | ] 14 | keywords = ["protobuf", "serialization"] 15 | 16 | [lib] 17 | proc_macro = true 18 | 19 | [dependencies] 20 | anyhow = "1.0.1" 21 | itertools = { version = "0.10", default-features = false, features = ["use_alloc"] } 22 | proc-macro2 = "1" 23 | quote = "1" 24 | syn = { version = "1.0.3", features = [ "extra-traits" ] } 25 | -------------------------------------------------------------------------------- /noproto-derive/src/field.rs: -------------------------------------------------------------------------------- 1 | use std::fmt; 2 | 3 | use anyhow::{bail, Error}; 4 | use syn::{Attribute, Lit, Meta, MetaList, MetaNameValue, NestedMeta}; 5 | 6 | #[derive(Clone, Debug, PartialEq, Eq)] 7 | pub enum Kind { 8 | Single, 9 | Repeated, 10 | Optional, 11 | Oneof, 12 | } 13 | 14 | #[derive(Clone)] 15 | pub struct Field { 16 | pub kind: Kind, 17 | pub tags: Vec, 18 | } 19 | 20 | impl Field { 21 | pub fn new(attrs: Vec) -> Result { 22 | let attrs = noproto_attrs(attrs); 23 | 24 | let mut tag = None; 25 | let mut tags = None; 26 | let mut kind = None; 27 | let mut unknown_attrs = Vec::new(); 28 | 29 | for attr in &attrs { 30 | if let Some(x) = tag_attr(attr)? { 31 | set_option(&mut tag, x, "duplicate tag attributes")?; 32 | } else if let Some(x) = tags_attr(attr)? { 33 | set_option(&mut tags, x, "duplicate tags attributes")?; 34 | } else if let Some(x) = kind_attr(attr) { 35 | set_option(&mut kind, x, "duplicate kind attribute")?; 36 | } else { 37 | unknown_attrs.push(attr); 38 | } 39 | } 40 | 41 | match unknown_attrs.len() { 42 | 0 => (), 43 | 1 => bail!("unknown attribute: {:?}", unknown_attrs[0]), 44 | _ => bail!("unknown attributes: {:?}", unknown_attrs), 45 | } 46 | 47 | let kind = kind.unwrap_or(Kind::Single); 48 | let tags = match kind { 49 | Kind::Oneof => { 50 | if tag.is_some() { 51 | bail!("tag attribute must not be set in oneof.") 52 | } 53 | match tags { 54 | Some(tags) => tags, 55 | None => bail!("missing tags attribute in oneof"), 56 | } 57 | } 58 | _ => match tag { 59 | Some(tag) => vec![tag], 60 | None => bail!("missing tag attribute"), 61 | }, 62 | }; 63 | 64 | Ok(Self { tags, kind }) 65 | } 66 | } 67 | 68 | #[derive(Clone)] 69 | pub struct OneofVariant { 70 | pub tag: u32, 71 | } 72 | 73 | impl OneofVariant { 74 | pub fn new(attrs: Vec) -> Result { 75 | let attrs = noproto_attrs(attrs); 76 | 77 | let mut tag = None; 78 | let mut unknown_attrs = Vec::new(); 79 | 80 | for attr in &attrs { 81 | if let Some(x) = tag_attr(attr)? { 82 | set_option(&mut tag, x, "duplicate tag attributes")?; 83 | } else { 84 | unknown_attrs.push(attr); 85 | } 86 | } 87 | 88 | match unknown_attrs.len() { 89 | 0 => (), 90 | 1 => bail!("unknown attribute: {:?}", unknown_attrs[0]), 91 | _ => bail!("unknown attributes: {:?}", unknown_attrs), 92 | } 93 | 94 | let tag = match tag { 95 | Some(tag) => tag, 96 | None => bail!("missing tag attribute"), 97 | }; 98 | 99 | Ok(Self { tag }) 100 | } 101 | } 102 | 103 | pub(super) fn tag_attr(attr: &Meta) -> Result, Error> { 104 | if !attr.path().is_ident("tag") { 105 | return Ok(None); 106 | } 107 | match *attr { 108 | Meta::List(ref meta_list) => { 109 | // TODO(rustlang/rust#23121): slice pattern matching would make this much nicer. 110 | if meta_list.nested.len() == 1 { 111 | if let NestedMeta::Lit(Lit::Int(ref lit)) = meta_list.nested[0] { 112 | return Ok(Some(lit.base10_parse()?)); 113 | } 114 | } 115 | bail!("invalid tag attribute: {:?}", attr); 116 | } 117 | Meta::NameValue(ref meta_name_value) => match meta_name_value.lit { 118 | Lit::Str(ref lit) => lit.value().parse::().map_err(Error::from).map(Option::Some), 119 | Lit::Int(ref lit) => Ok(Some(lit.base10_parse()?)), 120 | _ => bail!("invalid tag attribute: {:?}", attr), 121 | }, 122 | _ => bail!("invalid tag attribute: {:?}", attr), 123 | } 124 | } 125 | 126 | fn tags_attr(attr: &Meta) -> Result>, Error> { 127 | if !attr.path().is_ident("tags") { 128 | return Ok(None); 129 | } 130 | match *attr { 131 | Meta::List(ref meta_list) => { 132 | let mut tags = Vec::with_capacity(meta_list.nested.len()); 133 | for item in &meta_list.nested { 134 | if let NestedMeta::Lit(Lit::Int(ref lit)) = *item { 135 | tags.push(lit.base10_parse()?); 136 | } else { 137 | bail!("invalid tag attribute: {:?}", attr); 138 | } 139 | } 140 | Ok(Some(tags)) 141 | } 142 | Meta::NameValue(MetaNameValue { 143 | lit: Lit::Str(ref lit), .. 144 | }) => lit 145 | .value() 146 | .split(',') 147 | .map(|s| s.trim().parse::().map_err(Error::from)) 148 | .collect::, _>>() 149 | .map(Some), 150 | _ => bail!("invalid tag attribute: {:?}", attr), 151 | } 152 | } 153 | 154 | fn kind_attr(attr: &Meta) -> Option { 155 | let Meta::Path(ref path) = *attr else { return None }; 156 | 157 | if path.is_ident("repeated") { 158 | Some(Kind::Repeated) 159 | } else if path.is_ident("optional") { 160 | Some(Kind::Optional) 161 | } else if path.is_ident("oneof") { 162 | Some(Kind::Oneof) 163 | } else { 164 | None 165 | } 166 | } 167 | 168 | pub fn set_option(option: &mut Option, value: T, message: &str) -> Result<(), Error> { 169 | if let Some(ref existing) = *option { 170 | bail!("{}: {:?} and {:?}", message, existing, value); 171 | } 172 | *option = Some(value); 173 | Ok(()) 174 | } 175 | 176 | /// Get the items belonging to the 'noproto' list attribute, e.g. `#[noproto(foo, bar="baz")]`. 177 | fn noproto_attrs(attrs: Vec) -> Vec { 178 | attrs 179 | .iter() 180 | .flat_map(Attribute::parse_meta) 181 | .flat_map(|meta| match meta { 182 | Meta::List(MetaList { path, nested, .. }) => { 183 | if path.is_ident("noproto") { 184 | nested.into_iter().collect() 185 | } else { 186 | Vec::new() 187 | } 188 | } 189 | _ => Vec::new(), 190 | }) 191 | .flat_map(|attr| -> Result<_, _> { 192 | match attr { 193 | NestedMeta::Meta(attr) => Ok(attr), 194 | NestedMeta::Lit(lit) => bail!("invalid noproto attribute: {:?}", lit), 195 | } 196 | }) 197 | .collect() 198 | } 199 | -------------------------------------------------------------------------------- /noproto-derive/src/lib.rs: -------------------------------------------------------------------------------- 1 | // The `quote!` macro requires deep recursion. 2 | #![recursion_limit = "4096"] 3 | 4 | extern crate alloc; 5 | extern crate proc_macro; 6 | 7 | use anyhow::{bail, Error}; 8 | use field::{Kind, OneofVariant}; 9 | use itertools::Itertools; 10 | use proc_macro::TokenStream; 11 | use proc_macro2::Span; 12 | use quote::quote; 13 | use syn::punctuated::Punctuated; 14 | use syn::{Data, DataEnum, DataStruct, DeriveInput, Expr, Fields, FieldsNamed, FieldsUnnamed, Ident, Index, Variant}; 15 | 16 | mod field; 17 | use crate::field::Field; 18 | 19 | fn try_message(input: TokenStream) -> Result { 20 | let input: DeriveInput = syn::parse(input)?; 21 | 22 | let ident = input.ident; 23 | 24 | let variant_data = match input.data { 25 | Data::Struct(variant_data) => variant_data, 26 | Data::Enum(..) => bail!("Message can not be derived for an enum"), 27 | Data::Union(..) => bail!("Message can not be derived for a union"), 28 | }; 29 | 30 | let generics = &input.generics; 31 | let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); 32 | 33 | let (_is_struct, fields) = match variant_data { 34 | DataStruct { 35 | fields: Fields::Named(FieldsNamed { named: fields, .. }), 36 | .. 37 | } => (true, fields.into_iter().collect()), 38 | DataStruct { 39 | fields: Fields::Unnamed(FieldsUnnamed { unnamed: fields, .. }), 40 | .. 41 | } => (false, fields.into_iter().collect()), 42 | DataStruct { 43 | fields: Fields::Unit, .. 44 | } => (false, Vec::new()), 45 | }; 46 | 47 | let mut fields = fields 48 | .into_iter() 49 | .enumerate() 50 | .map(|(i, field)| { 51 | let field_ident = field.ident.map(|x| quote!(#x)).unwrap_or_else(|| { 52 | let index = Index { 53 | index: i as u32, 54 | span: Span::call_site(), 55 | }; 56 | quote!(#index) 57 | }); 58 | match Field::new(field.attrs) { 59 | Ok(field) => Ok((field_ident, field)), 60 | Err(err) => Err(err.context(format!("invalid message field {}.{}", ident, field_ident))), 61 | } 62 | }) 63 | .collect::, _>>()?; 64 | 65 | // Sort the fields by tag number so that fields will be encoded in tag order. 66 | // TODO: This encodes oneof fields in the position of their lowest tag, 67 | // regardless of the currently occupied variant, is that consequential? 68 | // See: https://developers.google.com/protocol-buffers/docs/encoding#order 69 | fields.sort_by_key(|&(_, ref field)| field.tags.iter().copied().min().unwrap()); 70 | let fields = fields; 71 | 72 | let mut tags = fields.iter().flat_map(|(_, field)| &field.tags).collect::>(); 73 | let num_tags = tags.len(); 74 | tags.sort_unstable(); 75 | tags.dedup(); 76 | if tags.len() != num_tags { 77 | bail!("message {} has fields with duplicate tags", ident); 78 | } 79 | 80 | let write = fields.iter().map(|&(ref field_ident, ref field)| { 81 | let tag = field.tags[0]; 82 | let ident = quote!(self.#field_ident); 83 | match field.kind { 84 | Kind::Single => quote!(w.write_field(#tag, &#ident)?;), 85 | Kind::Repeated => quote!(w.write_repeated(#tag, &#ident)?;), 86 | Kind::Optional => quote!(w.write_optional(#tag, &#ident)?;), 87 | Kind::Oneof => quote!(w.write_oneof(&#ident)?;), 88 | } 89 | }); 90 | 91 | let read = fields.iter().map(|&(ref field_ident, ref field)| { 92 | let ident = quote!(self.#field_ident); 93 | let read = match field.kind { 94 | Kind::Single => quote!(r.read(&mut #ident)?;), 95 | Kind::Repeated => quote!(r.read_repeated(&mut #ident)?;), 96 | Kind::Optional => quote!(r.read_optional(&mut #ident)?;), 97 | Kind::Oneof => quote!(r.read_oneof(&mut #ident)?;), 98 | }; 99 | 100 | let tags = field.tags.iter().map(|&tag| quote!(#tag)); 101 | let tags = Itertools::intersperse(tags, quote!(|)); 102 | 103 | quote!(#(#tags)* => { #read }) 104 | }); 105 | 106 | let expanded = quote! { 107 | impl #impl_generics ::noproto::Message for #ident #ty_generics #where_clause { 108 | const WIRE_TYPE: ::noproto::WireType = ::noproto::WireType::LengthDelimited; 109 | 110 | fn write_raw(&self, w: &mut ::noproto::encoding::ByteWriter) -> Result<(), ::noproto::WriteError> { 111 | #(#write)* 112 | Ok(()) 113 | } 114 | 115 | fn read_raw(&mut self, r: &mut ::noproto::encoding::ByteReader) -> Result<(), ::noproto::ReadError> { 116 | for r in r.read_fields() { 117 | let r = r?; 118 | match r.tag() { 119 | #(#read)* 120 | _ => {} 121 | } 122 | } 123 | Ok(()) 124 | } 125 | } 126 | }; 127 | 128 | Ok(expanded.into()) 129 | } 130 | 131 | #[proc_macro_derive(Message, attributes(noproto))] 132 | pub fn message(input: TokenStream) -> TokenStream { 133 | try_message(input).unwrap() 134 | } 135 | 136 | fn try_enumeration(input: TokenStream) -> Result { 137 | let input: DeriveInput = syn::parse(input)?; 138 | let ident = input.ident; 139 | 140 | let generics = &input.generics; 141 | let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); 142 | 143 | let punctuated_variants = match input.data { 144 | Data::Enum(DataEnum { variants, .. }) => variants, 145 | Data::Struct(_) => bail!("Enumeration can not be derived for a struct"), 146 | Data::Union(..) => bail!("Enumeration can not be derived for a union"), 147 | }; 148 | 149 | // Map the variants into 'fields'. 150 | let mut variants: Vec<(Ident, Expr)> = Vec::new(); 151 | for Variant { 152 | ident, 153 | fields, 154 | discriminant, 155 | .. 156 | } in punctuated_variants 157 | { 158 | match fields { 159 | Fields::Unit => (), 160 | Fields::Named(_) | Fields::Unnamed(_) => { 161 | bail!("Enumeration variants may not have fields") 162 | } 163 | } 164 | 165 | match discriminant { 166 | Some((_, expr)) => variants.push((ident, expr)), 167 | None => bail!("Enumeration variants must have a discriminant"), 168 | } 169 | } 170 | 171 | if variants.is_empty() { 172 | panic!("Enumeration must have at least one variant"); 173 | } 174 | 175 | let _default = variants[0].0.clone(); 176 | 177 | let _is_valid = variants.iter().map(|&(_, ref value)| quote!(#value => true)); 178 | 179 | let write = variants 180 | .iter() 181 | .map(|(variant, value)| quote!(#ident::#variant => #value)); 182 | 183 | let read = variants 184 | .iter() 185 | .map(|(variant, value)| quote!(#value => #ident::#variant )); 186 | 187 | let expanded = quote! { 188 | impl #impl_generics ::noproto::Message for #ident #ty_generics #where_clause { 189 | 190 | const WIRE_TYPE: ::noproto::WireType = ::noproto::WireType::Varint; 191 | 192 | fn write_raw(&self, w: &mut ::noproto::encoding::ByteWriter) -> Result<(), ::noproto::WriteError> { 193 | let val = match self { 194 | #(#write,)* 195 | }; 196 | w.write_varuint32(*self as _) 197 | } 198 | 199 | fn read_raw(&mut self, r: &mut ::noproto::encoding::ByteReader) -> Result<(), ::noproto::ReadError> { 200 | *self = match r.read_varuint32()? { 201 | #(#read,)* 202 | _ => return Err(::noproto::ReadError), 203 | }; 204 | Ok(()) 205 | } 206 | } 207 | }; 208 | 209 | Ok(expanded.into()) 210 | } 211 | 212 | #[proc_macro_derive(Enumeration, attributes(noproto))] 213 | pub fn enumeration(input: TokenStream) -> TokenStream { 214 | try_enumeration(input).unwrap() 215 | } 216 | 217 | fn try_oneof(input: TokenStream) -> Result { 218 | let input: DeriveInput = syn::parse(input)?; 219 | 220 | let ident = input.ident; 221 | 222 | let variants = match input.data { 223 | Data::Enum(DataEnum { variants, .. }) => variants, 224 | Data::Struct(..) => bail!("Oneof can not be derived for a struct"), 225 | Data::Union(..) => bail!("Oneof can not be derived for a union"), 226 | }; 227 | 228 | let generics = &input.generics; 229 | let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); 230 | 231 | // Map the variants 232 | let mut oneof_variants: Vec<(Ident, OneofVariant)> = Vec::new(); 233 | for Variant { 234 | attrs, 235 | ident: variant_ident, 236 | fields: variant_fields, 237 | .. 238 | } in variants 239 | { 240 | let variant_fields = match variant_fields { 241 | Fields::Unit => Punctuated::new(), 242 | Fields::Named(FieldsNamed { named: fields, .. }) 243 | | Fields::Unnamed(FieldsUnnamed { unnamed: fields, .. }) => fields, 244 | }; 245 | if variant_fields.len() != 1 { 246 | bail!("Oneof enum variants must have a single field"); 247 | } 248 | 249 | match OneofVariant::new(attrs) { 250 | Ok(variant) => oneof_variants.push((variant_ident, variant)), 251 | Err(err) => bail!("invalid oneof variant {}.{}: {}", ident, variant_ident, err), 252 | } 253 | } 254 | 255 | let mut tags = oneof_variants.iter().map(|(_, v)| v.tag).collect::>(); 256 | tags.sort_unstable(); 257 | tags.dedup(); 258 | if tags.len() != oneof_variants.len() { 259 | panic!("invalid oneof {}: variants have duplicate tags", ident); 260 | } 261 | 262 | let write = oneof_variants.iter().map(|(variant_ident, variant)| { 263 | let tag = variant.tag; 264 | quote!(#ident::#variant_ident(value) => { w.write_field(#tag, value)?; }) 265 | }); 266 | 267 | let read = oneof_variants.iter().map(|(variant_ident, variant)| { 268 | let tag = variant.tag; 269 | quote!(#tag => { 270 | *self = #ident::#variant_ident(r.read_oneof_variant()?); 271 | }) 272 | }); 273 | 274 | let read_option = oneof_variants.iter().map(|(variant_ident, variant)| { 275 | let tag = variant.tag; 276 | quote!(#tag => { 277 | *this = Some(#ident::#variant_ident(r.read_oneof_variant()?)); 278 | }) 279 | }); 280 | 281 | let expanded = quote! { 282 | impl #impl_generics ::noproto::Oneof for #ident #ty_generics #where_clause { 283 | fn write_raw(&self, w: &mut ::noproto::encoding::ByteWriter) -> Result<(), ::noproto::WriteError> { 284 | match self { 285 | #(#write)* 286 | } 287 | Ok(()) 288 | } 289 | 290 | fn read_raw(&mut self, r: ::noproto::encoding::FieldReader) -> Result<(), ::noproto::ReadError> { 291 | match r.tag() { 292 | #(#read)* 293 | _ => return Err(::noproto::ReadError), 294 | } 295 | Ok(()) 296 | } 297 | 298 | fn read_raw_option(this: &mut Option, r: ::noproto::encoding::FieldReader) -> Result<(), ::noproto::ReadError> { 299 | match r.tag() { 300 | #(#read_option)* 301 | _ => return Err(::noproto::ReadError), 302 | } 303 | Ok(()) 304 | } 305 | } 306 | }; 307 | 308 | Ok(expanded.into()) 309 | } 310 | 311 | #[proc_macro_derive(Oneof, attributes(noproto))] 312 | pub fn oneof(input: TokenStream) -> TokenStream { 313 | try_oneof(input).unwrap() 314 | } 315 | -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | group_imports = "StdExternalCrate" 2 | imports_granularity = "Module" 3 | max_width=120 -------------------------------------------------------------------------------- /src/impls.rs: -------------------------------------------------------------------------------- 1 | use crate::read::ByteReader; 2 | use crate::write::ByteWriter; 3 | use crate::{Message, Oneof, OptionalMessage, ReadError, RepeatedMessage, WireType, WriteError}; 4 | 5 | impl Message for bool { 6 | const WIRE_TYPE: WireType = WireType::Varint; 7 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError> { 8 | w.write_varuint32(*self as _) 9 | } 10 | fn read_raw(&mut self, r: &mut ByteReader) -> Result<(), ReadError> { 11 | let val = r.read_varuint32()?; 12 | 13 | *self = match val { 14 | 0 => false, 15 | 1 => true, 16 | _ => return Err(ReadError), 17 | }; 18 | Ok(()) 19 | } 20 | } 21 | 22 | impl Message for u8 { 23 | const WIRE_TYPE: WireType = WireType::Varint; 24 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError> { 25 | w.write_varuint32(*self as _) 26 | } 27 | fn read_raw(&mut self, r: &mut ByteReader) -> Result<(), ReadError> { 28 | *self = r.read_varuint32()?.try_into().map_err(|_| ReadError)?; 29 | Ok(()) 30 | } 31 | } 32 | 33 | impl Message for u16 { 34 | const WIRE_TYPE: WireType = WireType::Varint; 35 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError> { 36 | w.write_varuint32(*self as _) 37 | } 38 | fn read_raw(&mut self, r: &mut ByteReader) -> Result<(), ReadError> { 39 | *self = r.read_varuint32()?.try_into().map_err(|_| ReadError)?; 40 | Ok(()) 41 | } 42 | } 43 | 44 | impl Message for u32 { 45 | const WIRE_TYPE: WireType = WireType::Varint; 46 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError> { 47 | w.write_varuint32(*self) 48 | } 49 | fn read_raw(&mut self, r: &mut ByteReader) -> Result<(), ReadError> { 50 | *self = r.read_varuint32()?.try_into().map_err(|_| ReadError)?; 51 | Ok(()) 52 | } 53 | } 54 | 55 | impl Message for u64 { 56 | const WIRE_TYPE: WireType = WireType::Varint; 57 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError> { 58 | w.write_varuint64(*self) 59 | } 60 | fn read_raw(&mut self, r: &mut ByteReader) -> Result<(), ReadError> { 61 | *self = r.read_varuint64()?.try_into().map_err(|_| ReadError)?; 62 | Ok(()) 63 | } 64 | } 65 | 66 | impl Message for i8 { 67 | const WIRE_TYPE: WireType = WireType::Varint; 68 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError> { 69 | w.write_varint32(*self as _) 70 | } 71 | fn read_raw(&mut self, r: &mut ByteReader) -> Result<(), ReadError> { 72 | *self = r.read_varint32()?.try_into().map_err(|_| ReadError)?; 73 | Ok(()) 74 | } 75 | } 76 | 77 | impl Message for i16 { 78 | const WIRE_TYPE: WireType = WireType::Varint; 79 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError> { 80 | w.write_varint32(*self as _) 81 | } 82 | fn read_raw(&mut self, r: &mut ByteReader) -> Result<(), ReadError> { 83 | *self = r.read_varint32()?.try_into().map_err(|_| ReadError)?; 84 | Ok(()) 85 | } 86 | } 87 | 88 | impl Message for i32 { 89 | const WIRE_TYPE: WireType = WireType::Varint; 90 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError> { 91 | w.write_varint32(*self) 92 | } 93 | fn read_raw(&mut self, r: &mut ByteReader) -> Result<(), ReadError> { 94 | *self = r.read_varint32()?.try_into().map_err(|_| ReadError)?; 95 | Ok(()) 96 | } 97 | } 98 | 99 | impl Message for i64 { 100 | const WIRE_TYPE: WireType = WireType::Varint; 101 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError> { 102 | w.write_varint64(*self) 103 | } 104 | fn read_raw(&mut self, r: &mut ByteReader) -> Result<(), ReadError> { 105 | *self = r.read_varint64()?.try_into().map_err(|_| ReadError)?; 106 | Ok(()) 107 | } 108 | } 109 | 110 | impl Message for heapless::String { 111 | const WIRE_TYPE: WireType = WireType::LengthDelimited; 112 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError> { 113 | w.write(self.as_bytes()) 114 | } 115 | fn read_raw(&mut self, r: &mut ByteReader) -> Result<(), ReadError> { 116 | let data = r.read_to_end()?; 117 | let data = core::str::from_utf8(data).map_err(|_| ReadError)?; 118 | self.clear(); 119 | self.push_str(data).map_err(|_| ReadError)?; 120 | Ok(()) 121 | } 122 | } 123 | 124 | impl Message for heapless::Vec { 125 | const WIRE_TYPE: WireType = WireType::LengthDelimited; 126 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError> { 127 | w.write(self) 128 | } 129 | fn read_raw(&mut self, r: &mut ByteReader) -> Result<(), ReadError> { 130 | let data = r.read_to_end()?; 131 | self.clear(); 132 | self.extend_from_slice(data).map_err(|_| ReadError)?; 133 | Ok(()) 134 | } 135 | } 136 | 137 | impl RepeatedMessage for heapless::Vec { 138 | type Message = M; 139 | 140 | type Iter<'a> = core::slice::Iter<'a, M> where Self: 'a ; 141 | 142 | fn iter(&self) -> Result, WriteError> { 143 | Ok(self[..].iter()) 144 | } 145 | 146 | fn append(&mut self, m: Self::Message) -> Result<(), ReadError> { 147 | self.push(m).map_err(|_| ReadError) 148 | } 149 | } 150 | 151 | impl OptionalMessage for Option { 152 | type Message = M; 153 | 154 | fn get(&self) -> Option<&Self::Message> { 155 | self.as_ref() 156 | } 157 | 158 | fn set(&mut self, m: Self::Message) -> Result<(), ReadError> { 159 | *self = Some(m); 160 | Ok(()) 161 | } 162 | } 163 | 164 | impl Oneof for Option { 165 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError> { 166 | if let Some(x) = self { 167 | x.write_raw(w)?; 168 | } 169 | Ok(()) 170 | } 171 | 172 | fn read_raw(&mut self, r: crate::encoding::FieldReader) -> Result<(), ReadError> { 173 | M::read_raw_option(self, r) 174 | } 175 | 176 | fn read_raw_option(_this: &mut Option, _r: crate::encoding::FieldReader) -> Result<(), ReadError> { 177 | panic!("cannot nest options with oneof.") 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![doc = include_str!("../README.md")] 2 | #![cfg_attr(not(feature = "std"), no_std)] 3 | #![warn(missing_docs)] 4 | 5 | mod impls; 6 | mod read; 7 | mod write; 8 | 9 | pub use read::ReadError; 10 | use read::{ByteReader, FieldReader}; 11 | use write::ByteWriter; 12 | pub use write::WriteError; 13 | 14 | pub mod encoding { 15 | //! Encoding and decoding of primitive types. 16 | pub use crate::read::*; 17 | pub use crate::write::*; 18 | } 19 | 20 | // Re-export #[derive(Message, Enumeration, Oneof)]. 21 | #[cfg(feature = "derive")] 22 | #[allow(unused_imports)] 23 | #[macro_use] 24 | extern crate noproto_derive; 25 | #[cfg(feature = "derive")] 26 | #[doc(hidden)] 27 | pub use noproto_derive::*; 28 | 29 | /// Wire type of a field. 30 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] 31 | #[repr(u8)] 32 | pub enum WireType { 33 | /// Varint. 34 | Varint = 0, 35 | //SixtyFourBit = 1, 36 | /// Length-delimited. 37 | LengthDelimited = 2, 38 | //StartGroup = 3, 39 | //EndGroup = 4, 40 | //ThirtyTwoBit = 5, 41 | } 42 | 43 | /// A protobuf message. 44 | pub trait Message { 45 | /// The wire type of the message. 46 | const WIRE_TYPE: WireType; 47 | /// Serialize the message. 48 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError>; 49 | /// Deserialize the message. 50 | fn read_raw(&mut self, r: &mut ByteReader) -> Result<(), ReadError>; 51 | } 52 | 53 | /// An optional protobuf message. 54 | pub trait OptionalMessage { 55 | /// The message type. 56 | type Message: Message + Default; 57 | 58 | /// Get the message, if it exists. 59 | fn get(&self) -> Option<&Self::Message>; 60 | /// Set the message. 61 | fn set(&mut self, m: Self::Message) -> Result<(), ReadError>; 62 | } 63 | 64 | /// A repeated protobuf message. 65 | pub trait RepeatedMessage { 66 | /// The message type. 67 | type Message: Message + Default; 68 | /// An iterator over the messages. 69 | type Iter<'a>: Iterator 70 | where 71 | Self: 'a; 72 | 73 | /// Get an iterator over the messages. 74 | fn iter(&self) -> Result, WriteError>; 75 | /// Append a message. 76 | fn append(&mut self, m: Self::Message) -> Result<(), ReadError>; 77 | } 78 | 79 | /// A oneof protobuf message. 80 | pub trait Oneof: Sized { 81 | /// Serialize the message. 82 | fn write_raw(&self, w: &mut ByteWriter) -> Result<(), WriteError>; 83 | /// Deserialize the message. 84 | fn read_raw(&mut self, r: FieldReader) -> Result<(), ReadError>; 85 | /// Deserialize a oneof variant. 86 | fn read_raw_option(this: &mut Option, r: FieldReader) -> Result<(), ReadError>; 87 | } 88 | 89 | /// Serialize a protobuf message to a buffer. 90 | pub fn write(msg: &M, buf: &mut [u8]) -> Result { 91 | let mut w = ByteWriter::new(buf); 92 | msg.write_raw(&mut w)?; 93 | Ok(w.pos()) 94 | } 95 | 96 | /// Deserialize a protobuf message from a buffer. 97 | pub fn read(buf: &[u8]) -> Result { 98 | let mut msg = M::default(); 99 | let mut r = ByteReader::new(buf); 100 | msg.read_raw(&mut r)?; 101 | Ok(msg) 102 | } 103 | -------------------------------------------------------------------------------- /src/read.rs: -------------------------------------------------------------------------------- 1 | use crate::{Message, Oneof, OptionalMessage, RepeatedMessage, WireType}; 2 | 3 | /// Error returned by [`ByteReader`]. 4 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] 5 | pub struct ReadError; 6 | 7 | /// Reader for protobuf messages. 8 | pub struct ByteReader<'a> { 9 | data: &'a [u8], 10 | } 11 | 12 | impl<'a> ByteReader<'a> { 13 | /// Create a new [`ByteReader`] that reads from `data`. 14 | pub fn new(data: &'a [u8]) -> Self { 15 | Self { data } 16 | } 17 | 18 | /// Get a reference to the remaining bytes. 19 | pub fn inner(&self) -> &[u8] { 20 | self.data 21 | } 22 | 23 | /// Check if the reader is at the end of the buffer. 24 | pub fn eof(&self) -> bool { 25 | self.data.is_empty() 26 | } 27 | 28 | /// Read `N` bytes from the buffer. 29 | pub fn read(&mut self) -> Result<[u8; N], ReadError> { 30 | let n = self.data.get(0..N).ok_or(ReadError)?; 31 | self.data = &self.data[N..]; 32 | Ok(n.try_into().unwrap()) 33 | } 34 | 35 | /// Read a single byte from the buffer. 36 | pub fn read_u8(&mut self) -> Result { 37 | Ok(u8::from_le_bytes(self.read()?)) 38 | } 39 | 40 | /// Read a u16 from the buffer. 41 | pub fn read_u16(&mut self) -> Result { 42 | Ok(u16::from_le_bytes(self.read()?)) 43 | } 44 | 45 | /// Read a u32 from the buffer. 46 | pub fn read_u32(&mut self) -> Result { 47 | Ok(u32::from_le_bytes(self.read()?)) 48 | } 49 | 50 | /// Read a u64 from the buffer. 51 | pub fn read_u64(&mut self) -> Result { 52 | Ok(u64::from_le_bytes(self.read()?)) 53 | } 54 | 55 | /// Read a slice of length `len` from the buffer. 56 | pub fn read_slice(&mut self, len: usize) -> Result<&'a [u8], ReadError> { 57 | let res = self.data.get(0..len).ok_or(ReadError)?; 58 | self.data = &self.data[len..]; 59 | Ok(res) 60 | } 61 | 62 | /// Read the remaining bytes from the buffer. 63 | pub fn read_to_end(&mut self) -> Result<&'a [u8], ReadError> { 64 | let res = self.data; 65 | self.data = &[]; 66 | Ok(res) 67 | } 68 | 69 | /// Read a variable length slice from the buffer. 70 | pub fn read_varslice(&mut self) -> Result<&'a [u8], ReadError> { 71 | let len = self.read_varuint32()? as usize; 72 | self.read_slice(len) 73 | } 74 | 75 | /// Read varint-encoded bytes from the buffer. 76 | pub fn read_varuint_bytes(&mut self) -> Result<&'a [u8], ReadError> { 77 | for i in 0.. { 78 | if i >= self.data.len() { 79 | return Err(ReadError); 80 | } 81 | if self.data[i] & 0x80 == 0 { 82 | let res = &self.data[..i + 1]; 83 | self.data = &self.data[i + 1..]; 84 | return Ok(res); 85 | } 86 | } 87 | unreachable!() 88 | } 89 | 90 | /// Read varint-encoded u32 from the buffer. 91 | pub fn read_varuint32(&mut self) -> Result { 92 | let mut res = 0; 93 | let mut shift = 0; 94 | loop { 95 | let x = self.read_u8()?; 96 | 97 | // avoid shift overflow if the varuint is more than 32bit. 98 | // this happpens in practice: negative int32's are encoded as 64bit two's complement 99 | // (in nanopb at least, I haven't checked other impls.) 100 | if shift < 32 { 101 | res |= (x as u32 & 0x7F) << shift; 102 | } 103 | 104 | if x & 0x80 == 0 { 105 | break; 106 | } 107 | shift += 7; 108 | } 109 | Ok(res) 110 | } 111 | 112 | /// Read a varint-encoded i32 from the buffer. 113 | pub fn read_varint32(&mut self) -> Result { 114 | let u = self.read_varuint32()?; 115 | 116 | // zigzag encoding 117 | Ok(((u >> 1) as i32) ^ -((u & 1) as i32)) 118 | } 119 | 120 | /// Read a varint-encoded u64 from the buffer. 121 | pub fn read_varuint64(&mut self) -> Result { 122 | let mut res = 0; 123 | let mut shift = 0; 124 | loop { 125 | let x = self.read_u8()?; 126 | 127 | // avoid shift overflow if the varuint is more than 64bit. 128 | if shift < 64 { 129 | res |= (x as u64 & 0x7F) << shift; 130 | } 131 | 132 | if x & 0x80 == 0 { 133 | break; 134 | } 135 | shift += 7; 136 | } 137 | Ok(res) 138 | } 139 | 140 | /// Read a varint-encoded i64 from the buffer. 141 | pub fn read_varint64(&mut self) -> Result { 142 | let u = self.read_varuint64()?; 143 | 144 | // zigzag encoding 145 | Ok(((u >> 1) as i64) ^ -((u & 1) as i64)) 146 | } 147 | 148 | /// Return an iterator over the fields in the buffer. 149 | pub fn read_fields(&mut self) -> FieldIter<'_, 'a> { 150 | FieldIter { r: self } 151 | } 152 | } 153 | 154 | /// Iterator over the fields in a buffer. 155 | pub struct FieldIter<'a, 'b> { 156 | r: &'a mut ByteReader<'b>, 157 | } 158 | 159 | impl<'a, 'b> Iterator for FieldIter<'a, 'b> { 160 | type Item = Result, ReadError>; 161 | 162 | fn next(&mut self) -> Option { 163 | if self.r.eof() { 164 | return None; 165 | } 166 | 167 | // Read header 168 | let header = match self.r.read_varuint32() { 169 | Ok(x) => x, 170 | Err(e) => return Some(Err(e)), 171 | }; 172 | let tag = header >> 3; 173 | let wire_type = match header & 0b111 { 174 | 0 => WireType::Varint, 175 | 2 => WireType::LengthDelimited, 176 | _ => return Some(Err(ReadError)), 177 | }; 178 | 179 | let data = match wire_type { 180 | WireType::Varint => match self.r.read_varuint_bytes() { 181 | Ok(x) => x, 182 | Err(e) => return Some(Err(e)), 183 | }, 184 | WireType::LengthDelimited => { 185 | let len = match self.r.read_varuint32() { 186 | Ok(x) => x as usize, 187 | Err(e) => return Some(Err(e)), 188 | }; 189 | 190 | match self.r.read_slice(len) { 191 | Ok(x) => x, 192 | Err(e) => return Some(Err(e)), 193 | } 194 | } 195 | }; 196 | Some(Ok(FieldReader { tag, data, wire_type })) 197 | } 198 | } 199 | 200 | /// Reader for fields in a protobuf message. 201 | pub struct FieldReader<'a> { 202 | tag: u32, 203 | data: &'a [u8], 204 | wire_type: WireType, 205 | } 206 | 207 | impl<'a> FieldReader<'a> { 208 | /// Get the tag of the field. 209 | pub fn tag(&self) -> u32 { 210 | self.tag 211 | } 212 | 213 | /// Read into a message of type `M`. 214 | pub fn read(self, msg: &mut M) -> Result<(), ReadError> { 215 | if self.wire_type != M::WIRE_TYPE { 216 | return Err(ReadError); 217 | } 218 | 219 | msg.read_raw(&mut ByteReader::new(self.data)) 220 | } 221 | 222 | /// Read a repeated field into a message of type `M`. 223 | pub fn read_repeated(self, msg: &mut M) -> Result<(), ReadError> { 224 | if self.wire_type != M::Message::WIRE_TYPE { 225 | return Err(ReadError); 226 | } 227 | 228 | let mut m = M::Message::default(); 229 | self.read(&mut m)?; 230 | msg.append(m)?; 231 | Ok(()) 232 | } 233 | 234 | /// Read an optional field into a message of type `M`. 235 | pub fn read_optional(self, msg: &mut M) -> Result<(), ReadError> { 236 | if self.wire_type != M::Message::WIRE_TYPE { 237 | return Err(ReadError); 238 | } 239 | 240 | let mut m = M::Message::default(); 241 | self.read(&mut m)?; 242 | msg.set(m)?; 243 | Ok(()) 244 | } 245 | 246 | /// Read a oneof field into a message of type `M`. 247 | pub fn read_oneof(self, msg: &mut M) -> Result<(), ReadError> { 248 | msg.read_raw(self) 249 | } 250 | 251 | /// Read a oneof variant into a message of type `M`. 252 | pub fn read_oneof_variant(self) -> Result { 253 | if self.wire_type != M::WIRE_TYPE { 254 | return Err(ReadError); 255 | } 256 | 257 | let mut msg: M = Default::default(); 258 | msg.read_raw(&mut ByteReader::new(self.data))?; 259 | Ok(msg) 260 | } 261 | } 262 | -------------------------------------------------------------------------------- /src/write.rs: -------------------------------------------------------------------------------- 1 | use crate::{Message, Oneof, OptionalMessage, RepeatedMessage, WireType}; 2 | 3 | /// Error returned by [`ByteWriter`]. 4 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] 5 | pub struct WriteError; 6 | 7 | /// Writer for protobuf messages. 8 | pub struct ByteWriter<'a> { 9 | buf: &'a mut [u8], 10 | pos: usize, 11 | } 12 | 13 | impl<'a> ByteWriter<'a> { 14 | /// Create a new [`ByteWriter`] that writes to `buf`. 15 | pub fn new(buf: &'a mut [u8]) -> Self { 16 | Self { buf, pos: 0 } 17 | } 18 | 19 | /// Get the bytes written so far. 20 | pub fn bytes(&self) -> &[u8] { 21 | &self.buf[..self.pos] 22 | } 23 | 24 | /// Write `bytes` to the buffer. 25 | pub fn write(&mut self, bytes: &[u8]) -> Result<(), WriteError> { 26 | if self.buf.len() - self.pos < bytes.len() { 27 | return Err(WriteError); 28 | } 29 | self.buf[self.pos..][..bytes.len()].copy_from_slice(bytes); 30 | self.pos += bytes.len(); 31 | Ok(()) 32 | } 33 | 34 | /// Write a single byte to the buffer. 35 | pub fn write_u8(&mut self, val: u8) -> Result<(), WriteError> { 36 | self.write(&val.to_le_bytes()) 37 | } 38 | 39 | /// Write u16 to the buffer. 40 | pub fn write_u16(&mut self, val: u16) -> Result<(), WriteError> { 41 | self.write(&val.to_le_bytes()) 42 | } 43 | 44 | /// Write u32 to the buffer. 45 | pub fn write_u32(&mut self, val: u32) -> Result<(), WriteError> { 46 | self.write(&val.to_le_bytes()) 47 | } 48 | 49 | /// Write u64 to the buffer. 50 | pub fn write_u64(&mut self, val: u64) -> Result<(), WriteError> { 51 | self.write(&val.to_le_bytes()) 52 | } 53 | 54 | /// Write varint-encoded u32 to the buffer. 55 | pub fn write_varuint32(&mut self, mut val: u32) -> Result<(), WriteError> { 56 | loop { 57 | let mut part = val & 0x7F; 58 | let rest = val >> 7; 59 | if rest != 0 { 60 | part |= 0x80 61 | } 62 | 63 | self.write_u8(part as u8)?; 64 | 65 | if rest == 0 { 66 | return Ok(()); 67 | } 68 | val = rest 69 | } 70 | } 71 | 72 | /// Write varint-encoded u64 to the buffer. 73 | pub fn write_varuint64(&mut self, mut val: u64) -> Result<(), WriteError> { 74 | loop { 75 | let mut part = val & 0x7F; 76 | let rest = val >> 7; 77 | if rest != 0 { 78 | part |= 0x80 79 | } 80 | 81 | self.write_u8(part as u8)?; 82 | 83 | if rest == 0 { 84 | return Ok(()); 85 | } 86 | val = rest 87 | } 88 | } 89 | 90 | /// Write varint-encoded i32 to the buffer. 91 | pub fn write_varint32(&mut self, val: i32) -> Result<(), WriteError> { 92 | self.write_varuint32(((val >> 31) ^ (val << 1)) as u32) 93 | } 94 | 95 | /// Write varint-encoded i64 to the buffer. 96 | pub fn write_varint64(&mut self, val: i64) -> Result<(), WriteError> { 97 | self.write_varuint64(((val >> 63) ^ (val << 1)) as u64) 98 | } 99 | 100 | /// Write length-delimited data to the buffer. 101 | pub fn write_length_delimited( 102 | &mut self, 103 | f: impl FnOnce(&mut ByteWriter) -> Result<(), WriteError>, 104 | ) -> Result<(), WriteError> { 105 | // Write the data 106 | let start = self.pos; 107 | f(self)?; 108 | let len = self.pos - start; 109 | 110 | // Encode length header 111 | let mut header = [0; 16]; 112 | let mut header = ByteWriter::new(&mut header); 113 | header.write_varuint32(len.try_into().unwrap())?; 114 | let header = header.bytes(); 115 | 116 | // Move the data to make space for the header. 117 | if self.buf.len() - self.pos < header.len() { 118 | return Err(WriteError); 119 | } 120 | self.buf.copy_within(start..self.pos, start + header.len()); 121 | 122 | // Insert the header 123 | self.buf[start..][..header.len()].copy_from_slice(header); 124 | self.pos += header.len(); 125 | 126 | Ok(()) 127 | } 128 | 129 | /// Write a protobuf field to the buffer. 130 | pub fn write_field(&mut self, tag: u32, msg: &M) -> Result<(), WriteError> { 131 | self.write_varuint32((tag << 3) | (M::WIRE_TYPE as u32))?; 132 | 133 | match M::WIRE_TYPE { 134 | WireType::LengthDelimited => self.write_length_delimited(|w| msg.write_raw(w)), 135 | _ => msg.write_raw(self), 136 | } 137 | } 138 | 139 | /// Write a repeated protobuf field to the buffer. 140 | pub fn write_repeated(&mut self, tag: u32, msg: &M) -> Result<(), WriteError> { 141 | for i in msg.iter()? { 142 | self.write_field(tag, i)?; 143 | } 144 | Ok(()) 145 | } 146 | 147 | /// Write an optional protobuf field to the buffer. 148 | pub fn write_optional(&mut self, tag: u32, msg: &M) -> Result<(), WriteError> { 149 | if let Some(msg) = msg.get() { 150 | self.write_field(tag, msg)?; 151 | } 152 | Ok(()) 153 | } 154 | 155 | /// Write a oneof protobuf field to the buffer. 156 | pub fn write_oneof(&mut self, msg: &M) -> Result<(), WriteError> { 157 | msg.write_raw(self) 158 | } 159 | 160 | pub(crate) fn pos(&self) -> usize { 161 | self.pos 162 | } 163 | } 164 | --------------------------------------------------------------------------------