├── .editorconfig ├── .gitignore ├── .rustfmt.toml ├── .travis.yml ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE.txt ├── README.md ├── install-protobuf.sh ├── perftest ├── bytes │ ├── Cargo.toml │ ├── benches │ │ └── vs.rs │ ├── build.rs │ └── src │ │ ├── .gitignore │ │ ├── lib.rs │ │ └── messages.proto └── vs-cxx │ ├── .gitignore │ ├── Cargo.toml │ ├── build-perftest-cxx.sh │ ├── build-perftest-data.sh │ ├── build-perftest.sh │ ├── build.rs │ ├── perftest-cxx.cxx │ ├── perftest.rs │ ├── perftest_data.pbtxt │ └── perftest_data.proto ├── proto ├── google │ └── protobuf │ │ ├── any.proto │ │ ├── api.proto │ │ ├── compiler │ │ └── plugin.proto │ │ ├── descriptor.proto │ │ ├── duration.proto │ │ ├── empty.proto │ │ ├── fetch-protos.sh │ │ ├── field_mask.proto │ │ ├── source_context.proto │ │ ├── struct.proto │ │ ├── timestamp.proto │ │ ├── type.proto │ │ └── wrappers.proto └── rustproto.proto ├── protobuf-codegen-pure-test ├── .gitignore ├── Cargo.toml ├── build.rs └── src │ ├── lib.rs │ ├── v2 │ └── .gitignore │ └── v3 │ └── .gitignore ├── protobuf-codegen-pure ├── Cargo.toml ├── README.md └── src │ ├── convert.rs │ └── lib.rs ├── protobuf-codegen ├── Cargo.toml ├── README.md └── src │ ├── bin │ ├── protobuf-bin-gen-rust-do-not-use.rs │ └── protoc-gen-rust.rs │ ├── code_writer.rs │ ├── customize.rs │ ├── enums.rs │ ├── extensions.rs │ ├── field.rs │ ├── lib.rs │ ├── message.rs │ ├── oneof.rs │ ├── rust_types_values.rs │ └── well_known_types.rs ├── protobuf-test-common ├── Cargo.toml └── src │ ├── build.rs │ ├── hex.rs │ ├── lib.rs │ └── test.rs ├── protobuf-test ├── .gitignore ├── Cargo.toml ├── README.md ├── build.rs ├── src │ ├── common │ │ ├── mod.rs │ │ ├── v2 │ │ │ ├── .gitignore │ │ │ ├── test_basic.rs │ │ │ ├── test_basic_pb.proto │ │ │ ├── test_carllerche_bytes.rs │ │ │ ├── test_carllerche_bytes_pb.proto │ │ │ ├── test_enum_alias.rs │ │ │ ├── test_enum_alias_pb.proto │ │ │ ├── test_enum_unknown_values_preserved.rs │ │ │ ├── test_enum_unknown_values_preserved_pb.proto │ │ │ ├── test_enum_values.rs │ │ │ ├── test_enum_values_pb.proto │ │ │ ├── test_ident.rs │ │ │ ├── test_ident_pb.proto │ │ │ ├── test_import_descriptor.rs │ │ │ ├── test_import_descriptor_pb.proto │ │ │ ├── test_lite_runtime.rs │ │ │ ├── test_lite_runtime_pb.proto │ │ │ ├── test_nonunique_enum.rs │ │ │ ├── test_nonunique_enum_pb.proto │ │ │ ├── test_oneof.rs │ │ │ ├── test_oneof_expose.rs │ │ │ ├── test_oneof_expose_pb.proto │ │ │ ├── test_oneof_pb.proto │ │ │ ├── test_oneof_recursive.rs │ │ │ ├── test_oneof_recursive_pb.proto │ │ │ ├── test_reflect.rs │ │ │ ├── test_reflect_pb.proto │ │ │ ├── test_repeated_packed.rs │ │ │ ├── test_repeated_packed_pb.proto │ │ │ ├── test_root.rs │ │ │ ├── test_root_pb.proto │ │ │ ├── test_sync.rs │ │ │ ├── test_sync_pb.proto │ │ │ ├── test_text_format.rs │ │ │ └── test_text_format_pb.proto │ │ └── v3 │ │ │ └── .gitignore │ ├── google │ │ ├── mod.rs │ │ └── protobuf │ │ │ ├── .gitignore │ │ │ ├── README.md │ │ │ ├── import-tests.sh │ │ │ ├── mod.rs │ │ │ ├── unittest.proto │ │ │ ├── unittest_arena.proto │ │ │ ├── unittest_custom_options.proto │ │ │ ├── unittest_drop_unknown_fields.proto │ │ │ ├── unittest_embed_optimize_for.proto │ │ │ ├── unittest_empty.proto │ │ │ ├── unittest_import.proto │ │ │ ├── unittest_import_lite.proto │ │ │ ├── unittest_import_public.proto │ │ │ ├── unittest_import_public_lite.proto │ │ │ ├── unittest_lazy_dependencies.proto │ │ │ ├── unittest_lazy_dependencies_custom_option.proto │ │ │ ├── unittest_lazy_dependencies_enum.proto │ │ │ ├── unittest_lite.proto │ │ │ ├── unittest_lite_imports_nonlite.proto │ │ │ ├── unittest_mset.proto │ │ │ ├── unittest_mset_wire_format.proto │ │ │ ├── unittest_no_arena.proto │ │ │ ├── unittest_no_arena_import.proto │ │ │ ├── unittest_no_arena_lite.proto │ │ │ ├── unittest_no_field_presence.proto │ │ │ ├── unittest_no_generic_services.proto │ │ │ ├── unittest_optimize_for.proto │ │ │ ├── unittest_preserve_unknown_enum.proto │ │ │ ├── unittest_preserve_unknown_enum2.proto │ │ │ ├── unittest_proto3_arena.proto │ │ │ ├── unittest_proto3_arena_lite.proto │ │ │ ├── unittest_proto3_lite.proto │ │ │ └── unittest_well_known_types.proto │ ├── lib.rs │ ├── v2 │ │ ├── .gitignore │ │ ├── struct.proto │ │ ├── test-sanitize-file-name_pb.proto │ │ ├── test_default_values.rs │ │ ├── test_default_values_pb.proto │ │ ├── test_group_pb.proto │ │ ├── test_import_nested_imported_pb.proto │ │ ├── test_import_nested_pb.proto │ │ ├── test_import_nonunique_1_pb.proto │ │ ├── test_import_nonunique_2_pb.proto │ │ ├── test_import_nonunique_pb.proto │ │ ├── test_import_pkg_nested_imported_pb.proto │ │ ├── test_import_pkg_nested_pb.proto │ │ ├── test_import_root_imported_pb.proto │ │ ├── test_import_root_pb.proto │ │ ├── test_oneof_default_value.rs │ │ ├── test_oneof_default_value_pb.proto │ │ ├── test_required.rs │ │ ├── test_required_pb.proto │ │ ├── test_special~characters file{name}_pb.proto │ │ ├── test_unknown_suffix.rs │ │ └── test_unknown_suffix_pb.proto3 │ └── v3 │ │ ├── .gitignore │ │ ├── test_ident_pb.proto │ │ ├── test_issue_190_pb.proto │ │ ├── test_map.rs │ │ ├── test_map_carllerche.rs │ │ ├── test_map_carllerche_pb.proto │ │ ├── test_map_pb.proto │ │ ├── test_zeros_are_not_written.rs │ │ └── test_zeros_are_not_written_pb.proto └── test.sh ├── protobuf ├── Cargo.toml ├── README.md ├── benches │ ├── coded_input_stream.rs │ └── coded_output_stream.rs ├── full-rebuild.sh ├── regenerate.sh └── src │ ├── buf_read_iter.rs │ ├── cached_size.rs │ ├── chars.rs │ ├── clear.rs │ ├── compiler_plugin.rs │ ├── core.rs │ ├── descriptor.rs │ ├── descriptorx.rs │ ├── error.rs │ ├── ext.rs │ ├── lazy.rs │ ├── lib.rs │ ├── misc.rs │ ├── paginate.rs │ ├── plugin.rs │ ├── reflect │ ├── accessor.rs │ ├── map.rs │ ├── mod.rs │ ├── optional.rs │ ├── repeated.rs │ └── value.rs │ ├── repeated.rs │ ├── rt.rs │ ├── rust.rs │ ├── rustproto.rs │ ├── singular.rs │ ├── stream.rs │ ├── strx.rs │ ├── text_format.rs │ ├── types.rs │ ├── unknown.rs │ ├── varint.rs │ ├── well_known_types │ ├── any.rs │ ├── api.rs │ ├── duration.rs │ ├── empty.rs │ ├── field_mask.rs │ ├── mod.rs │ ├── source_context.rs │ ├── struct_pb.rs │ ├── timestamp.rs │ ├── type_pb.rs │ └── wrappers.rs │ └── zigzag.rs ├── protoc-rust ├── Cargo.toml ├── README.md └── src │ └── lib.rs └── protoc ├── Cargo.toml ├── README.md ├── src └── lib.rs ├── test-protoc ├── Cargo.toml ├── build.rs └── src │ ├── .gitignore │ ├── data.proto │ └── lib.rs └── test.sh /.editorconfig: -------------------------------------------------------------------------------- 1 | [*] 2 | end_of_line = lf 3 | insert_final_newline = true 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 4 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.dSYM 2 | *~ 3 | .*.swp 4 | .gdb_history 5 | tmp* 6 | *.dylib 7 | *.rlib 8 | *.bin 9 | *.so 10 | src/test/v*/pb_*.rs 11 | src/test/v*/*_pb.rs 12 | src/test/lib 13 | protobuf-bin-gen-rust 14 | protoc-gen-rust 15 | target/ 16 | Cargo.lock 17 | .idea 18 | *.iml 19 | -------------------------------------------------------------------------------- /.rustfmt.toml: -------------------------------------------------------------------------------- 1 | space_before_bound = true 2 | array_layout = "Block" 3 | normalize_comments = false 4 | fn_args_layout = "Block" 5 | fn_arg_indent = "Block" 6 | fn_brace_style = "SameLineWhere" 7 | fn_call_style = "Block" 8 | fn_return_indent = "WithWhereClause" 9 | reorder_imported_names = true 10 | where_pred_indent = "Block" 11 | # https://github.com/rust-lang-nursery/rustfmt/issues/1755 12 | closure_block_indent_threshold = -1 13 | error_on_line_overflow = false 14 | generics_indent = "Block" 15 | reorder_imports_in_group = true 16 | take_source_hints = true 17 | use_try_shorthand = true 18 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | language: rust 4 | 5 | matrix: 6 | include: 7 | - rust: stable 8 | env: PROTOBUF_VERSION=3.1.0 9 | - rust: beta 10 | env: PROTOBUF_VERSION=3.1.0 11 | - rust: nightly 12 | env: PROTOBUF_VERSION=3.1.0 13 | - rust: stable 14 | env: PROTOBUF_VERSION=2.6.1 15 | - rust: stable 16 | env: PROTOBUF_VERSION=3.1.0 RUST_PROTOBUF_FEATURES=with-bytes 17 | - rust: nightly 18 | env: PROTOBUF_VERSION=3.1.0 RUST_PROTOBUF_FEATURES=with-bytes 19 | 20 | before_install: 21 | - ./install-protobuf.sh 22 | - PATH=/home/travis/bin:$PATH protoc --version 23 | 24 | script: 25 | - rustc --version 26 | # Full rebuild with regenerate 27 | - PATH=/home/travis/bin:$PATH protobuf/full-rebuild.sh 28 | - PATH=/home/travis/bin:$PATH ./protoc/test.sh 29 | - test "$TRAVIS_RUST_VERSION" != "nightly" || PATH=/home/travis/bin:$PATH cargo build --all --features=with-bytes --bins --tests --examples --benches 30 | 31 | notifications: 32 | email: 33 | on_success: never 34 | 35 | # vim: set ts=2 sw=2 et: 36 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [Unreleased] 4 | 5 | - [Protobuf no longer exposes internal `hex` 6 | module](https://github.com/stepancheg/rust-protobuf/commit/8ad9687529a565c5ef2db93732cc20c8d8d22f00) 7 | - Generated code can now be customized not only with `rustproto.proto` 8 | but also when invoked programmatically with 9 | [`protoc-rust`](https://github.com/stepancheg/rust-protobuf/blob/b8573bd53cf5a9611598abbf02b71c49e59a8891/protobuf-codegen/src/customize.rs#L9) 10 | - [Drop `MessageStatic` trait](https://github.com/stepancheg/rust-protobuf/issues/214) 11 | - [`protobuf-codegen` is a separate crate](https://github.com/stepancheg/rust-protobuf/pull/261) 12 | - [Drop old reflection 13 | accessors](https://github.com/stepancheg/rust-protobuf/commit/7a03aee4e67bdd25ae6c403f37386707a0ab5eb9). 14 | Now code may need to be regenerated when protobuf version changed. 15 | - [Option to specify recursion limit](https://github.com/stepancheg/rust-protobuf/pull/248) 16 | - [Implement `std::io` traits by `CodedInputStream` and 17 | `CodedOutputStream`](https://github.com/stepancheg/rust-protobuf/pull/232) 18 | - [Implement conversions for `Repeated*`](https://github.com/stepancheg/rust-protobuf/pull/236) 19 | - [Generated code now uses closures instead of private functions 20 | for reflection](https://github.com/stepancheg/rust-protobuf/pull/267) 21 | - [Proto files with suffixes others than `.proto` 22 | are now supported](https://github.com/stepancheg/rust-protobuf/pull/265) 23 | - [Oneof are now public by 24 | default](https://github.com/stepancheg/rust-protobuf/commit/8bd911e2ea0d4461580105209ae11d9d3ec21fd0) 25 | 26 | ## [1.5] branch 27 | - [Better error message when `protoc` command is not 28 | found](https://github.com/stepancheg/rust-protobuf/commit/d59eb368deea1d292a161c3f30ff1123a022046d) 29 | 30 | ## [1.5.1] - 2018-04-02 31 | - [Fix serialization or large repeated packed fields](https://github.com/stepancheg/rust-protobuf/issues/281) 32 | 33 | ## [1.5.0] - 2018-03-25 34 | - [Unknown enum values are now stored in unknown fields](https://github.com/stepancheg/rust-protobuf/pull/276) 35 | 36 | ## [1.4.5] - 2018-04-02 37 | - [Fix serialization or large repeated packed fields](https://github.com/stepancheg/rust-protobuf/issues/281) 38 | 39 | ## [1.4.4] - 2018-03-05 40 | - [Escape macro keyword](https://github.com/stepancheg/rust-protobuf/pull/269) 41 | 42 | ## [1.4.3] - 2017-12-03 43 | - [Allow enum variants to be named `Self`](https://github.com/stepancheg/rust-protobuf/pull/259) 44 | 45 | ## [1.4.2] - 2017-10-14 46 | - [Properly read messages from blocking streams](https://github.com/stepancheg/rust-protobuf/issues/157) 47 | 48 | ## [1.4.1] - 2017-06-24 49 | - [Convert `String` to `Chars`](https://github.com/stepancheg/rust-protobuf/pull/225) 50 | 51 | ## [1.4] - 2017-06-24 52 | - Start of changelog 53 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = [ 3 | "protoc", 4 | "protoc/test-protoc", 5 | "protoc-rust", 6 | "protobuf", 7 | "protobuf-codegen", 8 | "protobuf-codegen-pure", 9 | "protobuf-codegen-pure-test", 10 | "protobuf-test", 11 | "protobuf-test-common", 12 | "perftest/vs-cxx", 13 | "perftest/bytes", 14 | ] 15 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013, Stepan Koltsov 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, this 11 | list of conditions and the following disclaimer in the documentation and/or 12 | other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 18 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 21 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | rust-protobuf 2 | ============= 3 | 4 | 5 | [![Build Status](https://img.shields.io/travis/stepancheg/rust-protobuf.svg)](https://travis-ci.org/stepancheg/rust-protobuf) 6 | [![crates.io version](https://img.shields.io/crates/v/protobuf.svg)](https://crates.io/crates/protobuf) 7 | [![License](https://img.shields.io/crates/l/protobuf.svg)](https://github.com/stepancheg/rust-protobuf/blob/master/LICENSE.txt) 8 | 9 | [Protobuf](https://developers.google.com/protocol-buffers/docs/overview) implementation in [Rust](https://www.rust-lang.org/). 10 | 11 | * Written in pure rust 12 | * Generate rust code 13 | * Has runtime library for generated code 14 | (Coded{Input|Output}Stream impl) 15 | 16 | ## How to generate rust code 17 | 18 | There are several ways to generate rust code from `.proto` files 19 | 20 | ### Invoke protoc programmatically with protoc-rust crate (recommended) 21 | 22 | Have a look at readme in [protoc-rust crate](https://github.com/stepancheg/rust-protobuf/tree/master/protoc-rust). 23 | 24 | ### Use pure rust protobuf parser and code generator (alpha) 25 | 26 | Readme should be in 27 | [protobuf-codegen-pure crate](https://github.com/stepancheg/rust-protobuf/tree/master/protobuf-codegen-pure). 28 | 29 | ### Use protoc-gen-rust plugin 30 | 31 | Readme is [here](https://github.com/stepancheg/rust-protobuf/tree/master/protobuf-codegen). 32 | 33 | ## Generated code 34 | 35 | Have a look at generated files, used internally in rust-protobuf: 36 | 37 | * [descriptor.rs](https://github.com/stepancheg/rust-protobuf/blob/master/protobuf/src/descriptor.rs) 38 | for [descriptor.proto](https://github.com/stepancheg/rust-protobuf/blob/master/proto/google/protobuf/descriptor.proto) 39 | (that is part of Google protobuf) 40 | 41 | ## Rustdoc 42 | 43 | docs.rs hosts [rustdoc for protobuf](https://docs.rs/protobuf/*/protobuf/). 44 | 45 | ## Copy-on-write 46 | 47 | Rust-protobuf can be used with [bytes crate](https://github.com/carllerche/bytes). 48 | 49 | To enable `Bytes` you need to: 50 | 51 | 1. Enable `with-bytes` feature in rust-protobuf: 52 | 53 | ``` 54 | [dependencies] 55 | protobuf = { version = "1.3", features = ["with-bytes"] } 56 | ``` 57 | 58 | 2. Enable bytes option in `.proto` file: 59 | 60 | ``` 61 | import "rustproto.proto"; 62 | 63 | option (rustproto.carllerche_bytes_for_bytes_all) = true; 64 | option (rustproto.carllerche_bytes_for_string_all) = true; 65 | ``` 66 | 67 | With these options enabled, fields of type `bytes` or `string` are 68 | generated as `Bytes` or `Chars` respectively. When `CodedInputStream` is constructed 69 | from `Bytes` object, fields of these types get subslices of original `Bytes` object, 70 | instead of being allocated on heap. 71 | 72 | ## Related projects 73 | 74 | * [quick-protobuf](https://github.com/tafia/quick-protobuf) — alternative protobuf implementation in Rust 75 | * [prost](https://github.com/danburkert/prost) — another protobuf implementation in Rust 76 | * [serde-protobuf](https://github.com/dflemstr/serde-protobuf) 77 | * [grpc-rust](https://github.com/stepancheg/grpc-rust) — implementation of gRPC based on this library 78 | * [grpc-rs](https://github.com/pingcap/grpc-rs/) — another gRPC implementation for Rust 79 | -------------------------------------------------------------------------------- /install-protobuf.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -ex 4 | 5 | die() { 6 | echo "$@" >&2 7 | exit 1 8 | } 9 | 10 | test -n "$PROTOBUF_VERSION" || die "PROTOBUF_VERSION env var is undefined" 11 | 12 | case "$PROTOBUF_VERSION" in 13 | 2*) 14 | basename=protobuf-$PROTOBUF_VERSION 15 | ;; 16 | 3*) 17 | basename=protobuf-cpp-$PROTOBUF_VERSION 18 | ;; 19 | *) 20 | die "unknown protobuf version: $PROTOBUF_VERSION" 21 | ;; 22 | esac 23 | 24 | curl -sL https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/$basename.tar.gz | tar zx 25 | 26 | cd protobuf-$PROTOBUF_VERSION 27 | 28 | ./configure --prefix=/home/travis && make -j2 && make install 29 | -------------------------------------------------------------------------------- /perftest/bytes/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "perftest-bytes" 3 | version = "0.1.0" 4 | authors = ["Stepan Koltsov "] 5 | publish = false 6 | 7 | [lib] 8 | test = false 9 | doctest = false 10 | 11 | [features] 12 | default-features = [] 13 | with-bytes = ["bytes", "protobuf/with-bytes"] 14 | 15 | [dependencies.protobuf] 16 | path = "../../protobuf" 17 | 18 | [dependencies.bytes] 19 | version = "0.*" 20 | optional = true 21 | 22 | 23 | [build-dependencies] 24 | protoc-rust = { path = "../../protoc-rust" } 25 | -------------------------------------------------------------------------------- /perftest/bytes/benches/vs.rs: -------------------------------------------------------------------------------- 1 | #![cfg(feature = "bytes")] 2 | #![feature(test)] 3 | 4 | extern crate test; 5 | extern crate bytes; 6 | 7 | extern crate protobuf; 8 | 9 | extern crate perftest_bytes; 10 | 11 | use std::fmt::Write; 12 | 13 | use bytes::Bytes; 14 | 15 | use protobuf::Message; 16 | use perftest_bytes::messages; 17 | 18 | 19 | fn make_string_of_len(len: usize) -> String { 20 | let mut s = String::new(); 21 | for i in 0..len { 22 | write!(s, "{}", i % 10).expect("unreachable"); 23 | } 24 | s 25 | } 26 | 27 | 28 | fn make_repeated(len: usize) -> Vec { 29 | let mut m = messages::TestMessage::new(); 30 | 31 | for i in 0..100 { 32 | m.mut_sr().push(make_string_of_len(i % len)); 33 | m.mut_br() 34 | .push(make_string_of_len((i + len / 2) % len).into_bytes()); 35 | } 36 | 37 | m.write_to_bytes().expect("write") 38 | } 39 | 40 | 41 | #[bench] 42 | fn parse_repeated_small_regular(b: &mut test::Bencher) { 43 | let bs = make_repeated(30); 44 | b.iter(|| { 45 | protobuf::parse_from_bytes::(&bs).expect("parse") 46 | }) 47 | } 48 | 49 | #[bench] 50 | fn parse_repeated_small_bytes(b: &mut test::Bencher) { 51 | let bs = Bytes::from(make_repeated(30)); 52 | b.iter(|| { 53 | protobuf::parse_from_carllerche_bytes::(&bs).expect("parse") 54 | }) 55 | } 56 | 57 | #[bench] 58 | fn parse_repeated_medium_regular(b: &mut test::Bencher) { 59 | let bs = make_repeated(300); 60 | b.iter(|| { 61 | protobuf::parse_from_bytes::(&bs).expect("parse") 62 | }) 63 | } 64 | 65 | #[bench] 66 | fn parse_repeated_medium_bytes(b: &mut test::Bencher) { 67 | let bs = Bytes::from(make_repeated(300)); 68 | b.iter(|| { 69 | protobuf::parse_from_carllerche_bytes::(&bs).expect("parse") 70 | }) 71 | } 72 | 73 | #[bench] 74 | fn parse_repeated_large_regular(b: &mut test::Bencher) { 75 | let bs = make_repeated(3000); 76 | b.iter(|| { 77 | protobuf::parse_from_bytes::(&bs).expect("parse") 78 | }) 79 | } 80 | 81 | #[bench] 82 | fn parse_repeated_large_bytes(b: &mut test::Bencher) { 83 | let bs = Bytes::from(make_repeated(3000)); 84 | b.iter(|| { 85 | protobuf::parse_from_carllerche_bytes::(&bs).expect("parse") 86 | }) 87 | } 88 | 89 | #[bench] 90 | fn parse_repeated_huge_regular(b: &mut test::Bencher) { 91 | let bs = make_repeated(30000); 92 | b.iter(|| { 93 | protobuf::parse_from_bytes::(&bs).expect("parse") 94 | }) 95 | } 96 | 97 | #[bench] 98 | fn parse_repeated_huge_bytes(b: &mut test::Bencher) { 99 | let bs = Bytes::from(make_repeated(30000)); 100 | b.iter(|| { 101 | protobuf::parse_from_carllerche_bytes::(&bs).expect("parse") 102 | }) 103 | } 104 | -------------------------------------------------------------------------------- /perftest/bytes/build.rs: -------------------------------------------------------------------------------- 1 | extern crate protoc_rust; 2 | 3 | fn main() { 4 | protoc_rust::run(protoc_rust::Args { 5 | out_dir: "src", 6 | input: &["src/messages.proto"], 7 | includes: &["src", "../../proto"], 8 | ..Default::default() 9 | }).expect("protoc"); 10 | } 11 | -------------------------------------------------------------------------------- /perftest/bytes/src/.gitignore: -------------------------------------------------------------------------------- 1 | messages.rs 2 | -------------------------------------------------------------------------------- /perftest/bytes/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![cfg(feature = "bytes")] 2 | 3 | extern crate protobuf; 4 | 5 | extern crate bytes; 6 | 7 | pub mod messages; 8 | -------------------------------------------------------------------------------- /perftest/bytes/src/messages.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | import "rustproto.proto"; 4 | 5 | message TestMessage { 6 | optional string s1 = 1; 7 | optional bytes b1 = 2; 8 | repeated string sr = 3; 9 | repeated bytes br = 4; 10 | 11 | optional TestMessage nested = 11; 12 | } 13 | 14 | message TestMessageWithBytes { 15 | option (rustproto.carllerche_bytes_for_bytes) = true; 16 | option (rustproto.carllerche_bytes_for_string) = true; 17 | 18 | optional string s1 = 1; 19 | optional bytes b1 = 2; 20 | repeated string sr = 3; 21 | repeated bytes br = 4; 22 | 23 | optional TestMessageWithBytes nested = 11; 24 | } 25 | -------------------------------------------------------------------------------- /perftest/vs-cxx/.gitignore: -------------------------------------------------------------------------------- 1 | perftest 2 | perftest-cxx 3 | perftest_proto.rs 4 | perftest_data.pbbin 5 | perftest_data.pb.* 6 | perftest_data.rs 7 | -------------------------------------------------------------------------------- /perftest/vs-cxx/Cargo.toml: -------------------------------------------------------------------------------- 1 | [project] 2 | name = "rust-protobuf-perftest" 3 | version = "0.0.0" 4 | authors = ["Stepan Koltsov "] 5 | publish = false 6 | 7 | [features] 8 | default-features = [] 9 | proto3 = [] 10 | with-bytes = ["protobuf/with-bytes"] 11 | 12 | [dependencies.protobuf] 13 | path = "../../protobuf" 14 | 15 | [dependencies.time] 16 | git = "http://github.com/rust-lang/time" 17 | 18 | [dependencies] 19 | rand = "0.*" 20 | 21 | [build-dependencies] 22 | protoc-rust = { path = "../../protoc-rust" } 23 | 24 | [[bin]] 25 | 26 | name = "rust-protobuf-perftest" 27 | path = "perftest.rs" 28 | test = false 29 | -------------------------------------------------------------------------------- /perftest/vs-cxx/build-perftest-cxx.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -ex 2 | 3 | protoc --cpp_out=. ./perftest_data.proto 4 | clang++ -std=c++11 -O3 -g -Wall -o perftest-cxx perftest-cxx.cxx perftest_data.pb.cc -lprotobuf 5 | 6 | # vim: set ts=4 sw=4 et: 7 | -------------------------------------------------------------------------------- /perftest/vs-cxx/build-perftest-data.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -ex 2 | 3 | cd $(dirname $0) 4 | protoc ./perftest_data.proto --encode=PerftestData < perftest_data.pbtxt > perftest_data.pbbin 5 | 6 | # vim: set ts=4 sw=4 et: 7 | -------------------------------------------------------------------------------- /perftest/vs-cxx/build-perftest.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -ex 2 | 3 | cd $(dirname $0) 4 | 5 | ./build-perftest-data.sh 6 | cargo build --release 7 | ./build-perftest-cxx.sh 8 | 9 | # vim: set ts=4 sw=4 et: 10 | -------------------------------------------------------------------------------- /perftest/vs-cxx/build.rs: -------------------------------------------------------------------------------- 1 | extern crate protoc_rust; 2 | 3 | fn main() { 4 | protoc_rust::run(protoc_rust::Args { 5 | out_dir: ".", 6 | input: &["perftest_data.proto"], 7 | ..Default::default() 8 | }).expect("protoc"); 9 | } 10 | -------------------------------------------------------------------------------- /perftest/vs-cxx/perftest_data.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | message Test1 { 4 | optional int32 value = 1; 5 | } 6 | 7 | message TestRepeatedBool { 8 | repeated bool values = 1; 9 | } 10 | 11 | message TestRepeatedPackedInt32 { 12 | repeated int32 values = 1 [ packed = true ]; 13 | } 14 | 15 | message TestRepeatedMessages { 16 | repeated TestRepeatedMessages messages1 = 1; 17 | repeated TestRepeatedMessages messages2 = 2; 18 | repeated TestRepeatedMessages messages3 = 3; 19 | } 20 | 21 | message TestOptionalMessages { 22 | optional TestOptionalMessages message1 = 1; 23 | optional TestOptionalMessages message2 = 2; 24 | optional TestOptionalMessages message3 = 3; 25 | } 26 | 27 | message TestStrings { 28 | optional string s1 = 1; 29 | optional string s2 = 2; 30 | optional string s3 = 3; 31 | } 32 | 33 | message TestBytes { 34 | optional bytes b1 = 1; 35 | } 36 | 37 | message PerftestData { 38 | repeated Test1 test1 = 1; 39 | repeated TestRepeatedBool test_repeated_bool = 2; 40 | repeated TestRepeatedMessages test_repeated_messages = 3; 41 | repeated TestOptionalMessages test_optional_messages = 4; 42 | repeated TestStrings test_strings = 5; 43 | repeated TestRepeatedPackedInt32 test_repeated_packed_int32 = 6; 44 | repeated TestBytes test_small_bytearrays = 7; 45 | repeated TestBytes test_large_bytearrays = 8; 46 | } 47 | -------------------------------------------------------------------------------- /proto/google/protobuf/duration.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto3"; 32 | 33 | package google.protobuf; 34 | 35 | option csharp_namespace = "Google.Protobuf.WellKnownTypes"; 36 | option cc_enable_arenas = true; 37 | option go_package = "github.com/golang/protobuf/ptypes/duration"; 38 | option java_package = "com.google.protobuf"; 39 | option java_outer_classname = "DurationProto"; 40 | option java_multiple_files = true; 41 | option objc_class_prefix = "GPB"; 42 | 43 | // A Duration represents a signed, fixed-length span of time represented 44 | // as a count of seconds and fractions of seconds at nanosecond 45 | // resolution. It is independent of any calendar and concepts like "day" 46 | // or "month". It is related to Timestamp in that the difference between 47 | // two Timestamp values is a Duration and it can be added or subtracted 48 | // from a Timestamp. Range is approximately +-10,000 years. 49 | // 50 | // Example 1: Compute Duration from two Timestamps in pseudo code. 51 | // 52 | // Timestamp start = ...; 53 | // Timestamp end = ...; 54 | // Duration duration = ...; 55 | // 56 | // duration.seconds = end.seconds - start.seconds; 57 | // duration.nanos = end.nanos - start.nanos; 58 | // 59 | // if (duration.seconds < 0 && duration.nanos > 0) { 60 | // duration.seconds += 1; 61 | // duration.nanos -= 1000000000; 62 | // } else if (durations.seconds > 0 && duration.nanos < 0) { 63 | // duration.seconds -= 1; 64 | // duration.nanos += 1000000000; 65 | // } 66 | // 67 | // Example 2: Compute Timestamp from Timestamp + Duration in pseudo code. 68 | // 69 | // Timestamp start = ...; 70 | // Duration duration = ...; 71 | // Timestamp end = ...; 72 | // 73 | // end.seconds = start.seconds + duration.seconds; 74 | // end.nanos = start.nanos + duration.nanos; 75 | // 76 | // if (end.nanos < 0) { 77 | // end.seconds -= 1; 78 | // end.nanos += 1000000000; 79 | // } else if (end.nanos >= 1000000000) { 80 | // end.seconds += 1; 81 | // end.nanos -= 1000000000; 82 | // } 83 | // 84 | // Example 3: Compute Duration from datetime.timedelta in Python. 85 | // 86 | // td = datetime.timedelta(days=3, minutes=10) 87 | // duration = Duration() 88 | // duration.FromTimedelta(td) 89 | // 90 | // 91 | message Duration { 92 | 93 | // Signed seconds of the span of time. Must be from -315,576,000,000 94 | // to +315,576,000,000 inclusive. 95 | int64 seconds = 1; 96 | 97 | // Signed fractions of a second at nanosecond resolution of the span 98 | // of time. Durations less than one second are represented with a 0 99 | // `seconds` field and a positive or negative `nanos` field. For durations 100 | // of one second or more, a non-zero value for the `nanos` field must be 101 | // of the same sign as the `seconds` field. Must be from -999,999,999 102 | // to +999,999,999 inclusive. 103 | int32 nanos = 2; 104 | } 105 | -------------------------------------------------------------------------------- /proto/google/protobuf/empty.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto3"; 32 | 33 | package google.protobuf; 34 | 35 | option csharp_namespace = "Google.Protobuf.WellKnownTypes"; 36 | option go_package = "github.com/golang/protobuf/ptypes/empty"; 37 | option java_package = "com.google.protobuf"; 38 | option java_outer_classname = "EmptyProto"; 39 | option java_multiple_files = true; 40 | option objc_class_prefix = "GPB"; 41 | option cc_enable_arenas = true; 42 | 43 | // A generic empty message that you can re-use to avoid defining duplicated 44 | // empty messages in your APIs. A typical example is to use it as the request 45 | // or the response type of an API method. For instance: 46 | // 47 | // service Foo { 48 | // rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty); 49 | // } 50 | // 51 | // The JSON representation for `Empty` is empty JSON object `{}`. 52 | message Empty {} 53 | -------------------------------------------------------------------------------- /proto/google/protobuf/fetch-protos.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -ex 2 | 3 | # fetch protos from protobuf respository 4 | # comment out `reserved` directive for comatibility with protobuf 2.6.1 5 | # https://github.com/google/protobuf/issues/1669#issuecomment-240598089 6 | for f in descriptor.proto compiler/plugin.proto \ 7 | any.proto \ 8 | api.proto \ 9 | duration.proto \ 10 | empty.proto \ 11 | field_mask.proto \ 12 | source_context.proto \ 13 | struct.proto \ 14 | timestamp.proto \ 15 | type.proto \ 16 | wrappers.proto \ 17 | ; do 18 | curl -s https://raw.githubusercontent.com/google/protobuf/v3.1.0/src/google/protobuf/$f \ 19 | | sed -e 's,^\( *\)\(reserved.*\),\1// \2,' \ 20 | > $f 21 | done 22 | 23 | # vim: set ts=4 sw=4 et: 24 | -------------------------------------------------------------------------------- /proto/google/protobuf/source_context.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto3"; 32 | 33 | package google.protobuf; 34 | 35 | option csharp_namespace = "Google.Protobuf.WellKnownTypes"; 36 | option java_package = "com.google.protobuf"; 37 | option java_outer_classname = "SourceContextProto"; 38 | option java_multiple_files = true; 39 | option objc_class_prefix = "GPB"; 40 | 41 | // `SourceContext` represents information about the source of a 42 | // protobuf element, like the file in which it is defined. 43 | message SourceContext { 44 | // The path-qualified name of the .proto file that contained the associated 45 | // protobuf element. For example: `"google/protobuf/source_context.proto"`. 46 | string file_name = 1; 47 | } 48 | -------------------------------------------------------------------------------- /proto/google/protobuf/struct.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto3"; 32 | 33 | package google.protobuf; 34 | 35 | option csharp_namespace = "Google.Protobuf.WellKnownTypes"; 36 | option cc_enable_arenas = true; 37 | option go_package = "github.com/golang/protobuf/ptypes/struct;structpb"; 38 | option java_package = "com.google.protobuf"; 39 | option java_outer_classname = "StructProto"; 40 | option java_multiple_files = true; 41 | option objc_class_prefix = "GPB"; 42 | 43 | 44 | // `Struct` represents a structured data value, consisting of fields 45 | // which map to dynamically typed values. In some languages, `Struct` 46 | // might be supported by a native representation. For example, in 47 | // scripting languages like JS a struct is represented as an 48 | // object. The details of that representation are described together 49 | // with the proto support for the language. 50 | // 51 | // The JSON representation for `Struct` is JSON object. 52 | message Struct { 53 | // Unordered map of dynamically typed values. 54 | map fields = 1; 55 | } 56 | 57 | // `Value` represents a dynamically typed value which can be either 58 | // null, a number, a string, a boolean, a recursive struct value, or a 59 | // list of values. A producer of value is expected to set one of that 60 | // variants, absence of any variant indicates an error. 61 | // 62 | // The JSON representation for `Value` is JSON value. 63 | message Value { 64 | // The kind of value. 65 | oneof kind { 66 | // Represents a null value. 67 | NullValue null_value = 1; 68 | // Represents a double value. 69 | double number_value = 2; 70 | // Represents a string value. 71 | string string_value = 3; 72 | // Represents a boolean value. 73 | bool bool_value = 4; 74 | // Represents a structured value. 75 | Struct struct_value = 5; 76 | // Represents a repeated `Value`. 77 | ListValue list_value = 6; 78 | } 79 | } 80 | 81 | // `NullValue` is a singleton enumeration to represent the null value for the 82 | // `Value` type union. 83 | // 84 | // The JSON representation for `NullValue` is JSON `null`. 85 | enum NullValue { 86 | // Null value. 87 | NULL_VALUE = 0; 88 | } 89 | 90 | // `ListValue` is a wrapper around a repeated field of values. 91 | // 92 | // The JSON representation for `ListValue` is JSON array. 93 | message ListValue { 94 | // Repeated field of dynamically typed values. 95 | repeated Value values = 1; 96 | } 97 | -------------------------------------------------------------------------------- /proto/google/protobuf/wrappers.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Wrappers for primitive (non-message) types. These types are useful 32 | // for embedding primitives in the `google.protobuf.Any` type and for places 33 | // where we need to distinguish between the absence of a primitive 34 | // typed field and its default value. 35 | 36 | syntax = "proto3"; 37 | 38 | package google.protobuf; 39 | 40 | option csharp_namespace = "Google.Protobuf.WellKnownTypes"; 41 | option cc_enable_arenas = true; 42 | option go_package = "github.com/golang/protobuf/ptypes/wrappers"; 43 | option java_package = "com.google.protobuf"; 44 | option java_outer_classname = "WrappersProto"; 45 | option java_multiple_files = true; 46 | option objc_class_prefix = "GPB"; 47 | 48 | // Wrapper message for `double`. 49 | // 50 | // The JSON representation for `DoubleValue` is JSON number. 51 | message DoubleValue { 52 | // The double value. 53 | double value = 1; 54 | } 55 | 56 | // Wrapper message for `float`. 57 | // 58 | // The JSON representation for `FloatValue` is JSON number. 59 | message FloatValue { 60 | // The float value. 61 | float value = 1; 62 | } 63 | 64 | // Wrapper message for `int64`. 65 | // 66 | // The JSON representation for `Int64Value` is JSON string. 67 | message Int64Value { 68 | // The int64 value. 69 | int64 value = 1; 70 | } 71 | 72 | // Wrapper message for `uint64`. 73 | // 74 | // The JSON representation for `UInt64Value` is JSON string. 75 | message UInt64Value { 76 | // The uint64 value. 77 | uint64 value = 1; 78 | } 79 | 80 | // Wrapper message for `int32`. 81 | // 82 | // The JSON representation for `Int32Value` is JSON number. 83 | message Int32Value { 84 | // The int32 value. 85 | int32 value = 1; 86 | } 87 | 88 | // Wrapper message for `uint32`. 89 | // 90 | // The JSON representation for `UInt32Value` is JSON number. 91 | message UInt32Value { 92 | // The uint32 value. 93 | uint32 value = 1; 94 | } 95 | 96 | // Wrapper message for `bool`. 97 | // 98 | // The JSON representation for `BoolValue` is JSON `true` and `false`. 99 | message BoolValue { 100 | // The bool value. 101 | bool value = 1; 102 | } 103 | 104 | // Wrapper message for `string`. 105 | // 106 | // The JSON representation for `StringValue` is JSON string. 107 | message StringValue { 108 | // The string value. 109 | string value = 1; 110 | } 111 | 112 | // Wrapper message for `bytes`. 113 | // 114 | // The JSON representation for `BytesValue` is JSON string. 115 | message BytesValue { 116 | // The bytes value. 117 | bytes value = 1; 118 | } 119 | -------------------------------------------------------------------------------- /proto/rustproto.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | import "google/protobuf/descriptor.proto"; 4 | 5 | // see https://github.com/gogo/protobuf/blob/master/gogoproto/gogo.proto 6 | // for the original idea 7 | 8 | package rustproto; 9 | 10 | extend google.protobuf.FileOptions { 11 | // When true, oneof field is generated public 12 | optional bool expose_oneof_all = 17001; 13 | // When true all fields are public, and not accessors generated 14 | optional bool expose_fields_all = 17003; 15 | // When false, `get_`, `set_`, `mut_` etc. accessors are not generated 16 | optional bool generate_accessors_all = 17004; 17 | // Use `bytes::Bytes` for `bytes` fields 18 | optional bool carllerche_bytes_for_bytes_all = 17011; 19 | // Use `bytes::Bytes` for `string` fields 20 | optional bool carllerche_bytes_for_string_all = 17012; 21 | } 22 | 23 | extend google.protobuf.MessageOptions { 24 | // When true, oneof field is generated public 25 | optional bool expose_oneof = 17001; 26 | // When true all fields are public, and not accessors generated 27 | optional bool expose_fields = 17003; 28 | // When false, `get_`, `set_`, `mut_` etc. accessors are not generated 29 | optional bool generate_accessors = 17004; 30 | // Use `bytes::Bytes` for `bytes` fields 31 | optional bool carllerche_bytes_for_bytes = 17011; 32 | // Use `bytes::Bytes` for `string` fields 33 | optional bool carllerche_bytes_for_string = 17012; 34 | } 35 | 36 | extend google.protobuf.FieldOptions { 37 | // When true all fields are public, and not accessors generated 38 | optional bool expose_fields_field = 17003; 39 | // When false, `get_`, `set_`, `mut_` etc. accessors are not generated 40 | optional bool generate_accessors_field = 17004; 41 | // Use `bytes::Bytes` for `bytes` fields 42 | optional bool carllerche_bytes_for_bytes_field = 17011; 43 | // Use `bytes::Bytes` for `string` fields 44 | optional bool carllerche_bytes_for_string_field = 17012; 45 | } 46 | -------------------------------------------------------------------------------- /protobuf-codegen-pure-test/.gitignore: -------------------------------------------------------------------------------- 1 | *_pb.rs 2 | *_pb_proto3.rs 3 | -------------------------------------------------------------------------------- /protobuf-codegen-pure-test/Cargo.toml: -------------------------------------------------------------------------------- 1 | [project] 2 | name = "protobuf-codegen-pure-test" 3 | version = "0.0.0" 4 | authors = ["Stepan Koltsov "] 5 | publish = false 6 | 7 | [lib] 8 | doctest = false 9 | 10 | [features] 11 | default-features = [] 12 | proto3 = [] 13 | with-bytes = ["bytes", "protobuf/with-bytes", "protobuf-test-common/with-bytes"] 14 | 15 | [build-dependencies] 16 | protobuf-codegen-pure = { path = "../protobuf-codegen-pure" } 17 | protobuf-test-common = { path = "../protobuf-test-common" } 18 | glob = "0.2" 19 | log = "0.*" 20 | env_logger = "0.5.*" 21 | 22 | [dependencies] 23 | protobuf-test-common = { path = "../protobuf-test-common" } 24 | 25 | [dependencies.protobuf] 26 | path = "../protobuf" 27 | 28 | [dependencies.bytes] 29 | version = "0.*" 30 | optional = true 31 | -------------------------------------------------------------------------------- /protobuf-codegen-pure-test/build.rs: -------------------------------------------------------------------------------- 1 | extern crate glob; 2 | extern crate log; 3 | extern crate env_logger; 4 | 5 | extern crate protobuf_codegen_pure; 6 | 7 | extern crate protobuf_test_common; 8 | 9 | use std::io::Read; 10 | use std::io::Write; 11 | 12 | use std::fs; 13 | use std::path::Path; 14 | 15 | use protobuf_test_common::build::*; 16 | 17 | 18 | fn copy, P2 : AsRef>(src: P1, dst: P2) { 19 | let mut content = Vec::new(); 20 | fs::File::open(src.as_ref()).expect(&format!("open {}", src.as_ref().display())) 21 | .read_to_end(&mut content).expect("read_to_end"); 22 | 23 | let mut write = fs::File::create(dst).expect("create"); 24 | write.write_all(&content).expect("write_all"); 25 | write.flush().expect("flush"); 26 | } 27 | 28 | 29 | fn copy_tests(dir: &str) { 30 | let src_dir = format!("../protobuf-test/{}", dir); 31 | for entry in fs::read_dir(&src_dir).expect(&format!("read_dir {}", src_dir)) { 32 | let file_name = entry.expect("entry").file_name().into_string().unwrap(); 33 | 34 | // skip temporary and generated files 35 | if file_name.starts_with(".") || file_name.ends_with("_pb.rs") { 36 | continue; 37 | } 38 | 39 | copy( 40 | &format!("../protobuf-test/{}/{}", dir, file_name), 41 | &format!("{}/{}", dir, file_name), 42 | ) 43 | } 44 | } 45 | 46 | 47 | fn gen_in_dir_pure(dir: &str) { 48 | gen_in_dir(dir, |GenInDirArgs { out_dir, input, includes, customize }| { 49 | protobuf_codegen_pure::run(protobuf_codegen_pure::Args { 50 | out_dir, input, includes, customize 51 | }) 52 | }); 53 | } 54 | 55 | 56 | fn generate_pb_rs() { 57 | 58 | copy_tests("src/v2"); 59 | 60 | copy_tests("src/v3"); 61 | // TODO 62 | fs::remove_file("src/v3/test_issue_190_pb.proto").expect("rm"); 63 | fs::remove_file("src/v3/test_map_carllerche_pb.proto").expect("rm"); 64 | fs::remove_file("src/v3/test_map_carllerche.rs").expect("rm"); 65 | 66 | gen_in_dir_pure("src/v2"); 67 | gen_in_dir_pure("src/v3"); 68 | } 69 | 70 | 71 | fn main() { 72 | env_logger::init(); 73 | 74 | clean_old_files(); 75 | generate_pb_rs(); 76 | } 77 | -------------------------------------------------------------------------------- /protobuf-codegen-pure-test/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![cfg(test)] 2 | 3 | extern crate protobuf; 4 | 5 | extern crate protobuf_test_common; 6 | 7 | mod v2; 8 | mod v3; 9 | -------------------------------------------------------------------------------- /protobuf-codegen-pure-test/src/v2/.gitignore: -------------------------------------------------------------------------------- 1 | # All files in this directory are generated 2 | * 3 | -------------------------------------------------------------------------------- /protobuf-codegen-pure-test/src/v3/.gitignore: -------------------------------------------------------------------------------- 1 | # All files in this directory are generated 2 | * 3 | -------------------------------------------------------------------------------- /protobuf-codegen-pure/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "protobuf-codegen-pure" 3 | version = "1.6.0" 4 | authors = ["Stepan Koltsov "] 5 | license = "MIT/Apache-2.0" 6 | homepage = "https://github.com/stepancheg/rust-protobuf/tree/master/protobuf-codegen-pure/" 7 | repository = "https://github.com/stepancheg/rust-protobuf/tree/master/protobuf-codegen-pure/" 8 | description = """ 9 | Pure-rust codegen for protobuf using protobuf-parser crate 10 | 11 | WIP 12 | """ 13 | 14 | [lib] 15 | doctest = false 16 | 17 | [dependencies] 18 | protobuf = { path = "../protobuf", version = "1.6.0" } 19 | protobuf-codegen = { path = "../protobuf-codegen", version = "1.6.0" } 20 | protobuf-parser = "=0.1.2" 21 | -------------------------------------------------------------------------------- /protobuf-codegen-pure/README.md: -------------------------------------------------------------------------------- 1 | # API to generate .rs files 2 | 3 | API to generate `.rs` files to be used e. g. [from build.rs](https://github.com/stepancheg/rust-protobuf/blob/master/protobuf-codegen-pure-test/build.rs). 4 | 5 | Example code: 6 | 7 | ``` 8 | extern crate protobuf_codegen_pure; 9 | 10 | protobuf_codegen_pure::run(protobuf_codegen_pure::Args { 11 | out_dir: "src/protos", 12 | input: &["protos/a.proto", "protos/b.proto"], 13 | includes: &["protos"], 14 | customize: Customize { 15 | ..Default::default() 16 | }, 17 | }).expect("protoc"); 18 | ``` 19 | 20 | And in `Cargo.toml`: 21 | 22 | ``` 23 | [build-dependencies] 24 | protobuf_codegen_pure = "1.5" 25 | ``` 26 | 27 | The alternative is to use 28 | [protoc-rust crate](https://github.com/stepancheg/rust-protobuf/tree/master/protoc-rust), 29 | which relies on `protoc` command to parse descriptors (thus it's more reliable), 30 | but it requires `protoc` command in `$PATH`. 31 | -------------------------------------------------------------------------------- /protobuf-codegen/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "protobuf-codegen" 3 | version = "1.6.0" 4 | authors = ["Stepan Koltsov "] 5 | 6 | [dependencies.protobuf] 7 | path = "../protobuf" 8 | 9 | [[bin]] 10 | 11 | name = "protoc-gen-rust" 12 | path = "src/bin/protoc-gen-rust.rs" 13 | test = false 14 | 15 | [[bin]] 16 | 17 | name = "protobuf-bin-gen-rust-do-not-use" 18 | path = "src/bin/protobuf-bin-gen-rust-do-not-use.rs" 19 | test = false 20 | 21 | -------------------------------------------------------------------------------- /protobuf-codegen/README.md: -------------------------------------------------------------------------------- 1 | # protobuf-codegen 2 | 3 | This crate contains protobuf code generator and a `protoc-gen-rust` `protoc` plugin. 4 | 5 | ## protoc-gen-rust 6 | 7 | `protoc-gen-rust` implements standard protobuf `protoc` plugin conventions. 8 | 9 | Probably you do not want to use it directly in Rust environment, there are easier to use alternatives: 10 | 11 | * [protoc-rust crate](https://github.com/stepancheg/rust-protobuf/tree/master/protoc-rust) 12 | which can be invoked programmatically from `build.rs` of your project 13 | which requires only `protoc` in `$PATH` but not `protoc-gen-rust`. 14 | * [protobuf-codegen-pure crate](https://github.com/stepancheg/rust-protobuf/tree/master/protobuf-codegen-pure) 15 | which behaves like protoc-rust, but does not depend on `protoc` binary 16 | 17 | ## But if you really want to use that plugin, here's the instruction 18 | 19 | (Note `protoc` can be invoked programmatically with 20 | [protoc crate](https://github.com/stepancheg/rust-protobuf/tree/master/protoc/)) 21 | 22 | 0) Install protobuf for `protoc` binary. 23 | 24 | On OS X [Homebrew](https://github.com/Homebrew/brew) can be used: 25 | 26 | ``` 27 | brew install protobuf 28 | ``` 29 | 30 | On Ubuntu, `protobuf-compiler` package can be installed: 31 | 32 | ``` 33 | apt-get install protobuf-compiler 34 | ``` 35 | 36 | Protobuf is needed only for code generation, `rust-protobuf` runtime 37 | does not use `protobuf` library. 38 | 39 | 1) Install `protoc-gen-rust` program (which is `protoc` plugin) 40 | 41 | It can be installed either from source or with `cargo install protobuf` command. 42 | 43 | 2) Add `protoc-gen-rust` to $PATH 44 | 45 | If you installed it with cargo, it should be 46 | 47 | ``` 48 | PATH="$HOME/.cargo/bin:$PATH" 49 | ``` 50 | 51 | 3) Generate .rs files: 52 | 53 | ``` 54 | protoc --rust_out . foo.proto 55 | ``` 56 | 57 | This will generate .rs files in current directory. 58 | -------------------------------------------------------------------------------- /protobuf-codegen/src/bin/protobuf-bin-gen-rust-do-not-use.rs: -------------------------------------------------------------------------------- 1 | extern crate protobuf; 2 | extern crate protobuf_codegen; 3 | 4 | use std::fs::*; 5 | use std::io::Read; 6 | use std::path::Path; 7 | 8 | use protobuf::parse_from_reader; 9 | use protobuf::descriptor::*; 10 | use protobuf_codegen::*; 11 | 12 | 13 | fn write_file(bin: &str) { 14 | let mut is = File::open(&Path::new(bin)).unwrap(); 15 | let fds = parse_from_reader::(&mut is as &mut Read).unwrap(); 16 | 17 | let file_names: Vec = fds.get_file() 18 | .iter() 19 | .map(|f| f.get_name().to_string()) 20 | .collect(); 21 | gen_and_write(fds.get_file(), &file_names, Path::new("."), &Default::default()) 22 | .expect("gen_and_write"); 23 | } 24 | 25 | fn main() { 26 | let args: Vec = std::env::args().collect(); 27 | if args.len() != 2 { 28 | panic!("must have exactly one argument"); 29 | } 30 | let ref pb_bin = args[1]; 31 | write_file(&pb_bin); 32 | } 33 | -------------------------------------------------------------------------------- /protobuf-codegen/src/bin/protoc-gen-rust.rs: -------------------------------------------------------------------------------- 1 | extern crate protobuf_codegen; 2 | 3 | fn main() { 4 | protobuf_codegen::protoc_gen_rust_main(); 5 | } 6 | -------------------------------------------------------------------------------- /protobuf-codegen/src/customize.rs: -------------------------------------------------------------------------------- 1 | use protobuf::rustproto; 2 | use protobuf::descriptor::FieldOptions; 3 | use protobuf::descriptor::MessageOptions; 4 | use protobuf::descriptor::FileOptions; 5 | 6 | 7 | /// Specifies style of generated code. 8 | #[derive(Default, Debug, Clone)] 9 | pub struct Customize { 10 | /// Make oneof enum public. 11 | pub expose_oneof: Option, 12 | /// When true all fields are public, and accessors are not generated 13 | pub expose_fields: Option, 14 | /// When false, `get_`, `set_`, `mut_` etc. accessors are not generated 15 | pub generate_accessors: Option, 16 | /// Use `bytes::Bytes` for `bytes` fields 17 | pub carllerche_bytes_for_bytes: Option, 18 | /// Use `bytes::Bytes` for `string` fields 19 | pub carllerche_bytes_for_string: Option, 20 | } 21 | 22 | impl Customize { 23 | /// Update fields of self with fields defined in other customize 24 | pub fn update_with(&mut self, that: &Customize) { 25 | if let Some(v) = that.expose_oneof { 26 | self.expose_oneof = Some(v); 27 | } 28 | if let Some(v) = that.expose_fields { 29 | self.expose_fields = Some(v); 30 | } 31 | if let Some(v) = that.generate_accessors { 32 | self.generate_accessors = Some(v); 33 | } 34 | if let Some(v) = that.carllerche_bytes_for_bytes { 35 | self.carllerche_bytes_for_bytes = Some(v); 36 | } 37 | if let Some(v) = that.carllerche_bytes_for_string { 38 | self.carllerche_bytes_for_string = Some(v); 39 | } 40 | } 41 | 42 | /// Update unset fields of self with fields from other customize 43 | pub fn set_defaults_from(&mut self, other: &Customize) { 44 | let mut tmp = other.clone(); 45 | tmp.update_with(self); 46 | *self = tmp; 47 | } 48 | } 49 | 50 | 51 | pub fn customize_from_rustproto_for_message(source: &MessageOptions) -> Customize { 52 | let expose_oneof = rustproto::exts::expose_oneof.get(source); 53 | let expose_fields = rustproto::exts::expose_fields.get(source); 54 | let generate_accessors = rustproto::exts::generate_accessors.get(source); 55 | let carllerche_bytes_for_bytes = rustproto::exts::carllerche_bytes_for_bytes.get(source); 56 | let carllerche_bytes_for_string = rustproto::exts::carllerche_bytes_for_string.get(source); 57 | Customize { 58 | expose_oneof, 59 | expose_fields, 60 | generate_accessors, 61 | carllerche_bytes_for_bytes, 62 | carllerche_bytes_for_string, 63 | } 64 | } 65 | 66 | pub fn customize_from_rustproto_for_field(source: &FieldOptions) -> Customize { 67 | let expose_oneof = None; 68 | let expose_fields = rustproto::exts::expose_fields_field.get(source); 69 | let generate_accessors = rustproto::exts::generate_accessors_field.get(source); 70 | let carllerche_bytes_for_bytes = rustproto::exts::carllerche_bytes_for_bytes_field.get(source); 71 | let carllerche_bytes_for_string = rustproto::exts::carllerche_bytes_for_string_field.get(source); 72 | Customize { 73 | expose_oneof, 74 | expose_fields, 75 | generate_accessors, 76 | carllerche_bytes_for_bytes, 77 | carllerche_bytes_for_string, 78 | } 79 | } 80 | 81 | pub fn customize_from_rustproto_for_file(source: &FileOptions) -> Customize { 82 | let expose_oneof = rustproto::exts::expose_oneof_all.get(source); 83 | let expose_fields = rustproto::exts::expose_fields_all.get(source); 84 | let generate_accessors = rustproto::exts::generate_accessors_all.get(source); 85 | let carllerche_bytes_for_bytes = rustproto::exts::carllerche_bytes_for_bytes_all.get(source); 86 | let carllerche_bytes_for_string = rustproto::exts::carllerche_bytes_for_string_all.get(source); 87 | Customize { 88 | expose_oneof, 89 | expose_fields, 90 | generate_accessors, 91 | carllerche_bytes_for_bytes, 92 | carllerche_bytes_for_string, 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /protobuf-codegen/src/extensions.rs: -------------------------------------------------------------------------------- 1 | use protobuf::descriptor::*; 2 | use protobuf::descriptorx::*; 3 | use super::code_writer::CodeWriter; 4 | use super::rust_types_values::*; 5 | 6 | 7 | struct ExtGen<'a> { 8 | file: &'a FileDescriptorProto, 9 | root_scope: &'a RootScope<'a>, 10 | field: &'a FieldDescriptorProto, 11 | } 12 | 13 | impl<'a> ExtGen<'a> { 14 | fn extendee_rust_name(&self) -> String { 15 | type_name_to_rust_relative(self.field.get_extendee(), self.file, true, self.root_scope) 16 | } 17 | 18 | fn repeated(&self) -> bool { 19 | match self.field.get_label() { 20 | FieldDescriptorProto_Label::LABEL_REPEATED => true, 21 | FieldDescriptorProto_Label::LABEL_OPTIONAL => false, 22 | FieldDescriptorProto_Label::LABEL_REQUIRED => { 23 | panic!("required ext field: {}", self.field.get_name()) 24 | } 25 | } 26 | } 27 | 28 | fn return_type_gen(&self) -> ProtobufTypeGen { 29 | if self.field.has_type_name() { 30 | let rust_name_relative = type_name_to_rust_relative( 31 | self.field.get_type_name(), 32 | self.file, 33 | true, 34 | self.root_scope, 35 | ); 36 | match self.field.get_field_type() { 37 | FieldDescriptorProto_Type::TYPE_MESSAGE => ProtobufTypeGen::Message( 38 | rust_name_relative, 39 | ), 40 | FieldDescriptorProto_Type::TYPE_ENUM => ProtobufTypeGen::Enum(rust_name_relative), 41 | t => panic!("unknown type: {:?}", t), 42 | } 43 | } else { 44 | ProtobufTypeGen::Primitive(self.field.get_field_type(), PrimitiveTypeVariant::Default) 45 | } 46 | } 47 | 48 | fn write(&self, w: &mut CodeWriter) { 49 | let suffix = if self.repeated() { 50 | "Repeated" 51 | } else { 52 | "Optional" 53 | }; 54 | let field_type = format!("::protobuf::ext::ExtField{}", suffix); 55 | w.pub_const( 56 | self.field.get_name(), // TODO: escape 57 | &format!( 58 | "{}<{}, {}>", 59 | field_type, 60 | self.extendee_rust_name(), 61 | self.return_type_gen().rust_type() 62 | ), 63 | &format!( 64 | "{} {{ field_number: {}, phantom: ::std::marker::PhantomData }}", 65 | field_type, 66 | self.field.get_number() 67 | ), 68 | ); 69 | } 70 | } 71 | 72 | 73 | pub fn write_extensions(file: &FileDescriptorProto, root_scope: &RootScope, w: &mut CodeWriter) { 74 | if file.get_extension().is_empty() { 75 | return; 76 | } 77 | 78 | w.write_line(""); 79 | w.pub_mod("exts", |w| { 80 | w.write_line("use protobuf::Message as Message_imported_for_functions;"); 81 | 82 | for field in file.get_extension() { 83 | if field.get_field_type() == FieldDescriptorProto_Type::TYPE_GROUP { 84 | continue; 85 | } 86 | 87 | w.write_line(""); 88 | ExtGen { 89 | file: file, 90 | root_scope: root_scope, 91 | field: field, 92 | }.write(w); 93 | } 94 | }); 95 | } 96 | -------------------------------------------------------------------------------- /protobuf-codegen/src/well_known_types.rs: -------------------------------------------------------------------------------- 1 | static NAMES: &'static [&'static str] = &[ 2 | "Any", 3 | "Api", 4 | "BoolValue", 5 | "BytesValue", 6 | "DoubleValue", 7 | "Duration", 8 | "Empty", 9 | "Enum", 10 | "EnumValue", 11 | "Field", 12 | // TODO: dotted names 13 | "Field.Cardinality", 14 | "Field.Kind", 15 | "FieldMask", 16 | "FloatValue", 17 | "Int32Value", 18 | "Int64Value", 19 | "ListValue", 20 | "Method", 21 | "Mixin", 22 | "NullValue", 23 | "Option", 24 | "SourceContext", 25 | "StringValue", 26 | "Struct", 27 | "Syntax", 28 | "Timestamp", 29 | "Type", 30 | "UInt32Value", 31 | "UInt64Value", 32 | "Value", 33 | ]; 34 | 35 | fn is_well_known_type(name: &str) -> bool { 36 | NAMES.iter().any(|&n| n == name) 37 | } 38 | 39 | pub fn is_well_known_type_full(name: &str) -> Option<&str> { 40 | if let Some(dot) = name.rfind('.') { 41 | if &name[..dot] == ".google.protobuf" && is_well_known_type(&name[dot + 1..]) { 42 | Some(&name[dot + 1..]) 43 | } else { 44 | None 45 | } 46 | } else { 47 | None 48 | } 49 | } 50 | 51 | #[cfg(test)] 52 | mod test { 53 | use super::*; 54 | 55 | #[test] 56 | fn test_is_well_known_type_full() { 57 | assert_eq!( 58 | Some("BoolValue"), 59 | is_well_known_type_full(".google.protobuf.BoolValue") 60 | ); 61 | assert_eq!(None, is_well_known_type_full(".google.protobuf.Fgfg")); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /protobuf-test-common/Cargo.toml: -------------------------------------------------------------------------------- 1 | [project] 2 | name = "protobuf-test-common" 3 | version = "0.0.0" 4 | authors = ["Stepan Koltsov "] 5 | publish = false 6 | description = """ 7 | Common code of protobuf tests 8 | """ 9 | 10 | [lib] 11 | doctest = false 12 | 13 | [features] 14 | default-features = [] 15 | proto3 = [] 16 | with-bytes = ["bytes", "protobuf/with-bytes"] 17 | 18 | [dependencies] 19 | glob = "0.2" 20 | log = "0.*" 21 | env_logger = "0.5.*" 22 | 23 | [dependencies.protobuf] 24 | path = "../protobuf" 25 | 26 | [dependencies.protobuf-codegen] 27 | path = "../protobuf-codegen" 28 | 29 | [dependencies.bytes] 30 | version = "0.*" 31 | optional = true 32 | -------------------------------------------------------------------------------- /protobuf-test-common/src/build.rs: -------------------------------------------------------------------------------- 1 | //! Common code of `build.rs` of two tests 2 | 3 | pub use protobuf_codegen::Customize; 4 | 5 | use std::io::Write; 6 | use std::io::BufRead; 7 | use std::io::BufReader; 8 | use std::fs; 9 | use std::fmt; 10 | use std::path::Path; 11 | 12 | use glob; 13 | 14 | 15 | pub fn glob_simple(pattern: &str) -> Vec { 16 | glob::glob(pattern) 17 | .expect("glob") 18 | .map(|g| { 19 | g.expect("item") 20 | .as_path() 21 | .to_str() 22 | .expect("utf-8") 23 | .to_owned() 24 | }) 25 | .collect() 26 | } 27 | 28 | 29 | fn read_gitignore(dir: &Path) -> Vec { 30 | let mut patterns = Vec::new(); 31 | 32 | let gitignore = format!("{}/.gitignore", dir.display()); 33 | let gitignore = &Path::new(&gitignore); 34 | if gitignore.exists() { 35 | let gitignore = fs::File::open(gitignore).expect("open gitignore"); 36 | for line in BufReader::new(gitignore).lines() { 37 | let line = line.expect("read_line"); 38 | if line.is_empty() || line.starts_with("#") { 39 | continue; 40 | } 41 | patterns.push(line); 42 | } 43 | } 44 | 45 | patterns 46 | } 47 | 48 | 49 | fn clean_recursively(dir: &Path, patterns: &[&str]) { 50 | assert!(dir.is_dir()); 51 | 52 | let gitignore_patterns = read_gitignore(dir); 53 | 54 | let mut patterns = patterns.to_vec(); 55 | patterns.extend(gitignore_patterns.iter().map(String::as_str)); 56 | 57 | let patterns_compiled: Vec<_> = patterns.iter() 58 | .map(|&p| glob::Pattern::new(p).expect("failed to compile pattern")) 59 | .collect(); 60 | 61 | for entry in fs::read_dir(dir).expect("read_dir") { 62 | let entry = entry.expect("entry"); 63 | let entry_path = entry.path(); 64 | let file_name = entry_path.as_path().file_name().unwrap().to_str().unwrap(); 65 | if entry.metadata().expect("metadata").is_dir() { 66 | clean_recursively(&entry_path, &patterns); 67 | } else if file_name == ".gitignore" { 68 | // keep it 69 | } else { 70 | for pattern in &patterns_compiled { 71 | if pattern.matches(file_name) { 72 | fs::remove_file(&entry_path).expect("remove_file"); 73 | break; 74 | } 75 | } 76 | } 77 | } 78 | } 79 | 80 | 81 | pub fn clean_old_files() { 82 | clean_recursively(&Path::new("src"), &["*_pb.rs", "*_pb_proto3.rs"]); 83 | } 84 | 85 | #[derive(Default)] 86 | pub struct GenInDirArgs<'a> { 87 | pub out_dir: &'a str, 88 | pub input: &'a [&'a str], 89 | pub includes: &'a [&'a str], 90 | pub customize: Customize, 91 | } 92 | 93 | /// Generate mod.rs from all files in a directory 94 | pub fn gen_mod_rs_in_dir(dir: &str) { 95 | assert!(Path::new(dir).is_dir()); 96 | 97 | let mut mod_rs = fs::File::create(&format!("{}/mod.rs", dir)).expect("create"); 98 | 99 | writeln!(mod_rs, "// generated by {}", module_path!()).expect("write"); 100 | writeln!(mod_rs, "").expect("write"); 101 | 102 | let rs_files = glob_simple(&format!("{}/*.rs", dir)); 103 | 104 | for rs in rs_files { 105 | let file_name = Path::new(&rs).file_name().expect("file_name").to_str().expect("file_name"); 106 | if file_name == "mod.rs" { 107 | continue; 108 | } 109 | assert!(file_name.ends_with(".rs")); 110 | let mod_name = &file_name[..file_name.len() - ".rs".len()]; 111 | 112 | if mod_name.contains("carllerche") { 113 | writeln!(mod_rs, r#"#[cfg(feature = "with-bytes")]"#).expect("write"); 114 | } 115 | writeln!(mod_rs, "mod {};", mod_name).expect("write"); 116 | } 117 | 118 | mod_rs.flush().expect("flush"); 119 | } 120 | 121 | pub fn gen_in_dir(dir: &str, gen: F) 122 | where 123 | F : for<'a> Fn(GenInDirArgs<'a>) -> Result<(), E>, 124 | E : fmt::Debug, 125 | { 126 | info!("generating protos in {}", dir); 127 | 128 | let mut protos = Vec::new(); 129 | for suffix in &[".proto", ".proto3"] { 130 | protos.extend(glob_simple(&format!("{}/*{}", dir, suffix))); 131 | } 132 | 133 | assert!(!protos.is_empty()); 134 | 135 | gen(GenInDirArgs { 136 | out_dir: dir, 137 | input: &protos.iter().map(|a| a.as_ref()).collect::>(), 138 | includes: &["../proto", dir], 139 | .. Default::default() 140 | }).expect("protoc"); 141 | 142 | gen_mod_rs_in_dir(dir); 143 | } 144 | -------------------------------------------------------------------------------- /protobuf-test-common/src/hex.rs: -------------------------------------------------------------------------------- 1 | // hex encoder and decoder used by rust-protobuf unittests 2 | 3 | use std::char; 4 | 5 | fn decode_hex_digit(digit: char) -> u8 { 6 | match digit { 7 | '0'...'9' => digit as u8 - '0' as u8, 8 | 'a'...'f' => digit as u8 - 'a' as u8 + 10, 9 | 'A'...'F' => digit as u8 - 'A' as u8 + 10, 10 | _ => panic!(), 11 | } 12 | } 13 | 14 | pub fn decode_hex(hex: &str) -> Vec { 15 | let mut r: Vec = Vec::new(); 16 | let mut chars = hex.chars().enumerate(); 17 | loop { 18 | let (pos, first) = match chars.next() { 19 | None => break, 20 | Some(elt) => elt, 21 | }; 22 | if first == ' ' { 23 | continue; 24 | } 25 | let (_, second) = match chars.next() { 26 | None => panic!("pos = {}d", pos), 27 | Some(elt) => elt, 28 | }; 29 | r.push((decode_hex_digit(first) << 4) | decode_hex_digit(second)); 30 | } 31 | r 32 | } 33 | 34 | fn encode_hex_digit(digit: u8) -> char { 35 | match char::from_digit(digit as u32, 16) { 36 | Some(c) => c, 37 | _ => panic!(), 38 | } 39 | } 40 | 41 | fn encode_hex_byte(byte: u8) -> [char; 2] { 42 | [encode_hex_digit(byte >> 4), encode_hex_digit(byte & 0x0Fu8)] 43 | } 44 | 45 | pub fn encode_hex(bytes: &[u8]) -> String { 46 | let strs: Vec = bytes 47 | .iter() 48 | .map(|byte| encode_hex_byte(*byte).iter().map(|c| *c).collect()) 49 | .collect(); 50 | strs.join(" ") 51 | } 52 | 53 | #[cfg(test)] 54 | mod test { 55 | 56 | use super::decode_hex; 57 | use super::encode_hex; 58 | 59 | #[test] 60 | fn test_decode_hex() { 61 | assert_eq!(decode_hex(""), [].to_vec()); 62 | assert_eq!(decode_hex("00"), [0x00u8].to_vec()); 63 | assert_eq!(decode_hex("ff"), [0xffu8].to_vec()); 64 | assert_eq!(decode_hex("AB"), [0xabu8].to_vec()); 65 | assert_eq!(decode_hex("fa 19"), [0xfau8, 0x19].to_vec()); 66 | } 67 | 68 | #[test] 69 | fn test_encode_hex() { 70 | assert_eq!("".to_string(), encode_hex(&[])); 71 | assert_eq!("00".to_string(), encode_hex(&[0x00])); 72 | assert_eq!("ab".to_string(), encode_hex(&[0xab])); 73 | assert_eq!( 74 | "01 a2 1a fe".to_string(), 75 | encode_hex(&[0x01, 0xa2, 0x1a, 0xfe]) 76 | ); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /protobuf-test-common/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Functions used in protobuf tests 2 | 3 | extern crate protobuf; 4 | extern crate protobuf_codegen; 5 | extern crate glob; 6 | #[macro_use] 7 | extern crate log; 8 | 9 | mod test; 10 | 11 | pub mod build; 12 | pub mod hex; 13 | 14 | pub use test::*; 15 | -------------------------------------------------------------------------------- /protobuf-test-common/src/test.rs: -------------------------------------------------------------------------------- 1 | use hex::encode_hex; 2 | use hex::decode_hex; 3 | 4 | use protobuf::*; 5 | 6 | pub fn test_serialize_deserialize_length_delimited(msg: &M) { 7 | let serialized_bytes = msg.write_length_delimited_to_bytes().unwrap(); 8 | let parsed = parse_length_delimited_from_bytes::(&serialized_bytes).unwrap(); 9 | assert_eq!(*msg, parsed); 10 | } 11 | 12 | pub fn test_serialize_deserialize_no_hex(msg: &M) { 13 | let serialized_bytes = msg.write_to_bytes().unwrap(); 14 | let parsed = parse_from_bytes::(&serialized_bytes).unwrap(); 15 | assert_eq!(*msg, parsed); 16 | } 17 | 18 | pub fn test_serialize_deserialize(hex: &str, msg: &M) { 19 | let expected_bytes = decode_hex(hex); 20 | let expected_hex = encode_hex(&expected_bytes); 21 | let serialized = msg.write_to_bytes().unwrap(); 22 | let serialized_hex = encode_hex(&serialized); 23 | assert_eq!( 24 | expected_hex, 25 | serialized_hex, 26 | "message {}", 27 | M::descriptor_static(None).name() 28 | ); 29 | let parsed = parse_from_bytes::(&expected_bytes).unwrap(); 30 | assert_eq!(*msg, parsed); 31 | 32 | assert_eq!(expected_bytes.len(), msg.compute_size() as usize); 33 | 34 | test_serialize_deserialize_length_delimited(msg); 35 | } 36 | 37 | pub fn test_deserialize(hex: &str, msg: &M) { 38 | let bytes = decode_hex(hex); 39 | let parsed = parse_from_bytes::(&bytes).unwrap(); 40 | assert_eq!(*msg, parsed); 41 | } 42 | 43 | pub fn test_serialize(hex: &str, msg: &M) { 44 | let hex = encode_hex(&decode_hex(hex)); 45 | 46 | let serialized = msg.write_to_bytes().unwrap(); 47 | let serialized_hex = encode_hex(&serialized); 48 | 49 | assert_eq!(serialized_hex, hex); 50 | } 51 | -------------------------------------------------------------------------------- /protobuf-test/.gitignore: -------------------------------------------------------------------------------- 1 | *_pb.rs 2 | *_pb_proto3.rs 3 | -------------------------------------------------------------------------------- /protobuf-test/Cargo.toml: -------------------------------------------------------------------------------- 1 | [project] 2 | name = "protobuf-test" 3 | version = "0.0.0" 4 | authors = ["Stepan Koltsov "] 5 | publish = false 6 | 7 | [lib] 8 | doctest = false 9 | 10 | [features] 11 | default-features = [] 12 | proto3 = [] 13 | with-bytes = ["bytes", "protobuf/with-bytes", "protobuf-test-common/with-bytes"] 14 | 15 | [build-dependencies] 16 | protoc = { path = "../protoc" } 17 | protoc-rust = { path = "../protoc-rust" } 18 | protobuf-test-common = { path = "../protobuf-test-common" } 19 | glob = "0.2" 20 | log = "0.*" 21 | env_logger = "0.5.*" 22 | 23 | [dependencies] 24 | protobuf-test-common = { path = "../protobuf-test-common" } 25 | 26 | [dependencies.protobuf] 27 | path = "../protobuf" 28 | 29 | [dependencies.bytes] 30 | version = "0.*" 31 | optional = true 32 | -------------------------------------------------------------------------------- /protobuf-test/README.md: -------------------------------------------------------------------------------- 1 | # Tests for rust-protobuf 2 | 3 | To execute tests simply execute `cargo test`. 4 | 5 | If protobuf 3 is installed, command will include test for protobuf 3 generated code. 6 | 7 | If `--features=with-bytes` flag is specified, tests will include test for `with-bytes` feature, 8 | which is not enabled by default. 9 | 10 | `./test.sh` is to be used from [travis-ci](https://travis-ci.org/stepancheg/rust-protobuf/), 11 | and not needed for local development. 12 | 13 | `cargo test` executes [`build.rs`](https://github.com/stepancheg/rust-protobuf/blob/master/protobuf-test/build.rs) script, 14 | which generates `.rs` files from `.proto` and `mod.rs` files for certain folder. 15 | 16 | ## Test contents 17 | 18 | * `v2` contains tests specific to protobuf 2 19 | * `v3` contains tests specific to protobuf 2 20 | * `google` contains tests `.proto` files taken from Google's protobuf implementation 21 | * `common` contains tests which are identical for both versions of protobuf syntax. 22 | `common/v2` directory contains sources, and contents of `common/v3` is generated 23 | from `common/v2` by copy and replace. 24 | -------------------------------------------------------------------------------- /protobuf-test/src/common/mod.rs: -------------------------------------------------------------------------------- 1 | // v2 tests which must be compatible with v3 tests 2 | mod v2; 3 | 4 | // v3 tests are generated from v2 tests by copy&replace 5 | #[cfg(proto3)] 6 | mod v3; 7 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/.gitignore: -------------------------------------------------------------------------------- 1 | mod.rs 2 | *_pb.rs 3 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_basic_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package basic; 4 | 5 | message Test1 { 6 | required int32 a = 1; 7 | } 8 | 9 | message Test2 { 10 | required string b = 2; 11 | } 12 | 13 | message Test3 { 14 | required Test1 c = 3; 15 | } 16 | 17 | message TestRecursion { 18 | repeated TestRecursion children = 1; 19 | } 20 | 21 | message Test4 { 22 | repeated int32 d = 4 [packed=true]; 23 | } 24 | 25 | message TestEmpty { 26 | optional int32 foo = 10; 27 | } 28 | 29 | message TestUnknownFields { 30 | required int32 a = 1; 31 | } 32 | 33 | // just check it compiles 34 | message TestSelfReference { 35 | required TestSelfReference r1 = 1; 36 | optional TestSelfReference r2 = 2; 37 | } 38 | 39 | message TestDefaultInstanceField { 40 | optional string s = 1; 41 | } 42 | 43 | message TestDefaultInstance { 44 | optional TestDefaultInstanceField field = 1; 45 | } 46 | 47 | message TestDescriptor { 48 | optional int32 stuff = 10; 49 | } 50 | 51 | enum TestEnumDescriptor { 52 | UNDEFINED = 0; 53 | RED = 1; 54 | BLUE = 2; 55 | GREEN = 3; 56 | } 57 | 58 | message TestTypesSingular { 59 | optional double double_field = 1; 60 | optional float float_field = 2; 61 | optional int32 int32_field = 3; 62 | optional int64 int64_field = 4; 63 | optional uint32 uint32_field = 5; 64 | optional uint64 uint64_field = 6; 65 | optional sint32 sint32_field = 7; 66 | optional sint64 sint64_field = 8; 67 | optional fixed32 fixed32_field = 9; 68 | optional fixed64 fixed64_field = 10; 69 | optional sfixed32 sfixed32_field = 11; 70 | optional sfixed64 sfixed64_field = 12; 71 | optional bool bool_field = 13; 72 | optional string string_field = 14; 73 | optional bytes bytes_field = 15; 74 | optional TestEnumDescriptor enum_field = 16; 75 | } 76 | 77 | message TestTypesRepeated { 78 | repeated double double_field = 1 [packed=false]; 79 | repeated float float_field = 2 [packed=false]; 80 | repeated int32 int32_field = 3 [packed=false]; 81 | repeated int64 int64_field = 4 [packed=false]; 82 | repeated uint32 uint32_field = 5 [packed=false]; 83 | repeated uint64 uint64_field = 6 [packed=false]; 84 | repeated sint32 sint32_field = 7 [packed=false]; 85 | repeated sint64 sint64_field = 8 [packed=false]; 86 | repeated fixed32 fixed32_field = 9 [packed=false]; 87 | repeated fixed64 fixed64_field = 10 [packed=false]; 88 | repeated sfixed32 sfixed32_field = 11 [packed=false]; 89 | repeated sfixed64 sfixed64_field = 12 [packed=false]; 90 | repeated bool bool_field = 13 [packed=false]; 91 | repeated string string_field = 14; 92 | repeated bytes bytes_field = 15; 93 | repeated TestEnumDescriptor enum_field = 16 [packed=false]; 94 | } 95 | 96 | message TestTypesRepeatedPacked { 97 | repeated double double_field = 1 [packed=true]; 98 | repeated float float_field = 2 [packed=true]; 99 | repeated int32 int32_field = 3 [packed=true]; 100 | repeated int64 int64_field = 4 [packed=true]; 101 | repeated uint32 uint32_field = 5 [packed=true]; 102 | repeated uint64 uint64_field = 6 [packed=true]; 103 | repeated sint32 sint32_field = 7 [packed=true]; 104 | repeated sint64 sint64_field = 8 [packed=true]; 105 | repeated fixed32 fixed32_field = 9 [packed=true]; 106 | repeated fixed64 fixed64_field = 10 [packed=true]; 107 | repeated sfixed32 sfixed32_field = 11 [packed=true]; 108 | repeated sfixed64 sfixed64_field = 12 [packed=true]; 109 | repeated bool bool_field = 13 [packed=true]; 110 | repeated string string_field = 14; 111 | repeated bytes bytes_field = 15; 112 | repeated TestEnumDescriptor enum_field = 16 [packed=true]; 113 | } 114 | 115 | message TestInvalidTag { 116 | } 117 | 118 | message TestTruncated { 119 | repeated fixed32 ints = 2 [packed=true]; 120 | } 121 | 122 | message TestBugSint { 123 | optional sint32 s32 = 1; 124 | optional sint64 s64 = 2; 125 | } 126 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_carllerche_bytes.rs: -------------------------------------------------------------------------------- 1 | use bytes::Bytes; 2 | use protobuf::Chars; 3 | 4 | use super::test_carllerche_bytes_pb::*; 5 | 6 | use protobuf_test_common::*; 7 | 8 | #[test] 9 | fn test() { 10 | let mut m = TestCarllercheBytes::new(); 11 | m.set_b1(Bytes::from("aabb")); 12 | m.set_s1(Chars::from("ccdd")); 13 | 14 | let mut br = Vec::new(); 15 | br.push(Bytes::from("bb1")); 16 | br.push(Bytes::from("bb2")); 17 | m.set_br(br); 18 | 19 | let mut sr = Vec::new(); 20 | sr.push(Chars::from("ss1")); 21 | sr.push(Chars::from("ss2")); 22 | m.set_sr(sr); 23 | 24 | test_serialize_deserialize_no_hex(&m); 25 | } 26 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_carllerche_bytes_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | import "rustproto.proto"; 4 | 5 | option (rustproto.carllerche_bytes_for_bytes_all) = true; 6 | option (rustproto.carllerche_bytes_for_string_all) = true; 7 | 8 | message TestCarllercheBytes { 9 | optional bytes b1 = 1; 10 | optional string s1 = 2; 11 | repeated bytes br = 3; 12 | repeated string sr = 4; 13 | } 14 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_enum_alias.rs: -------------------------------------------------------------------------------- 1 | use protobuf::ProtobufEnum; 2 | 3 | use super::test_enum_alias_pb::*; 4 | 5 | use protobuf_test_common::*; 6 | 7 | #[test] 8 | fn test_enum() { 9 | assert_eq!(10, EnumWithAlias::A.value()); 10 | assert_eq!(10, EnumWithAlias::A_AGAIN.value()); 11 | assert_eq!( 12 | &[ 13 | EnumWithAlias::UNKNOWN, 14 | EnumWithAlias::A, 15 | EnumWithAlias::B, 16 | EnumWithAlias::A_AGAIN, 17 | ], 18 | EnumWithAlias::values() 19 | ); 20 | assert_eq!(EnumWithAlias::A, EnumWithAlias::A_AGAIN); 21 | } 22 | 23 | #[test] 24 | fn test_enum_in_message() { 25 | let mut m = TestEnumWithAlias::new(); 26 | m.set_en(EnumWithAlias::A); 27 | test_serialize_deserialize("08 0a", &m); 28 | } 29 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_enum_alias_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package test_enum_alias; 4 | 5 | enum EnumWithAlias { 6 | option allow_alias = true; 7 | UNKNOWN = 0; 8 | A = 10; 9 | B = 20; 10 | A_AGAIN = 10; 11 | } 12 | 13 | message TestEnumWithAlias { 14 | optional EnumWithAlias en = 1; 15 | } 16 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_enum_unknown_values_preserved.rs: -------------------------------------------------------------------------------- 1 | use super::test_enum_unknown_values_preserved_pb::*; 2 | 3 | use protobuf::*; 4 | use protobuf_test_common::*; 5 | 6 | #[test] 7 | fn unknown_values_preserved() { 8 | let mut new = NewMessage::new(); 9 | new.set_eee(NewEnum::C); 10 | 11 | test_serialize_deserialize("08 1e", &new); 12 | 13 | // `OldEnum` doesn't have variant `C = 30`, 14 | // but message still properly serialized and deserialized. 15 | 16 | let old: OldMessage = parse_from_bytes(&hex::decode_hex("08 1e")).expect("parse"); 17 | 18 | test_serialize_deserialize("08 1e", &old); 19 | } 20 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_enum_unknown_values_preserved_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package unknown_values_preserved; 4 | 5 | enum OldEnum { 6 | // Add _OLD suffix to field name to avoid collision with NewEnum 7 | UNKNOWN_OLD = 0; 8 | A_OLD = 10; 9 | B_OLD = 20; 10 | } 11 | 12 | enum NewEnum { 13 | UNKNOWN = 0; 14 | A = 10; 15 | B = 20; 16 | C = 30; 17 | } 18 | 19 | message OldMessage { 20 | optional OldEnum eee = 1; 21 | } 22 | 23 | message NewMessage { 24 | optional NewEnum eee = 1; 25 | } 26 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_enum_values.rs: -------------------------------------------------------------------------------- 1 | use protobuf::*; 2 | 3 | use super::test_enum_values_pb::*; 4 | 5 | #[test] 6 | fn test_enum_values() { 7 | let expected = [ 8 | TestEnumValuesEnum::UNKNOWN, 9 | TestEnumValuesEnum::WINTER, 10 | TestEnumValuesEnum::SPRING, 11 | TestEnumValuesEnum::SUMMER, 12 | TestEnumValuesEnum::AUTUMN, 13 | ]; 14 | assert_eq!(expected, TestEnumValuesEnum::values()); 15 | } 16 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_enum_values_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | enum TestEnumValuesEnum { 4 | UNKNOWN = 0; 5 | WINTER = 11; 6 | SPRING = 22; 7 | SUMMER = 33; 8 | AUTUMN = 44; 9 | } 10 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_ident.rs: -------------------------------------------------------------------------------- 1 | use super::test_ident_pb::*; 2 | 3 | #[test] 4 | fn test() { 5 | let _ = TestType::new(); 6 | } 7 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_ident_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | // rust types defined in prelude 4 | 5 | message Vec { } 6 | 7 | message String { } 8 | 9 | message Option { } 10 | message None { } 11 | message Some { } 12 | 13 | message Message { } 14 | 15 | // rust keywords 16 | 17 | message struct { } 18 | 19 | message Self { 20 | optional string s = 1; 21 | } 22 | 23 | message Outer { 24 | message fn {} 25 | } 26 | 27 | // oneof named type 28 | 29 | message TestType { 30 | oneof type { 31 | string s = 1; 32 | } 33 | repeated string struct = 2; 34 | repeated uint32 ref = 3; 35 | } 36 | 37 | // enum value which is a keyword 38 | 39 | enum MyLittleEnum { 40 | UNKNOWN = 0; 41 | fn = 2; 42 | self = 3; 43 | } 44 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_import_descriptor.rs: -------------------------------------------------------------------------------- 1 | use super::test_import_descriptor_pb::*; 2 | 3 | #[test] 4 | fn test() { 5 | let _ = FooBar::new(); 6 | } 7 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_import_descriptor_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | import "google/protobuf/descriptor.proto"; 4 | 5 | message FooBar { 6 | optional .google.protobuf.DescriptorProto d = 1; 7 | } 8 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_lite_runtime.rs: -------------------------------------------------------------------------------- 1 | use protobuf_test_common::*; 2 | 3 | use super::test_lite_runtime_pb::*; 4 | 5 | #[test] 6 | fn test_lite_runtime() { 7 | let mut m = TestLiteRuntime::new(); 8 | m.set_v(10); 9 | test_serialize_deserialize("08 0a", &m); 10 | 11 | // test it doesn't crash 12 | format!("{:?}", m); 13 | } 14 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_lite_runtime_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package test_lite_runtime; 4 | 5 | option optimize_for = LITE_RUNTIME; 6 | 7 | enum EnumTestLiteRuntime { 8 | UNKNOWN = 0; 9 | ONE = 1; 10 | TWO = 2; 11 | } 12 | 13 | message TestLiteRuntime { 14 | optional int32 v = 1; 15 | } 16 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_nonunique_enum.rs: -------------------------------------------------------------------------------- 1 | use super::test_nonunique_enum_pb::*; 2 | 3 | #[test] 4 | fn test() { 5 | let _ = MessageA_EnumA::FOO; 6 | } 7 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_nonunique_enum_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | message MessageA { 4 | enum EnumA { FOO = 0; } 5 | } 6 | message MessageB { 7 | enum EnumB { FOO = 0; } 8 | } 9 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_oneof.rs: -------------------------------------------------------------------------------- 1 | use protobuf_test_common::*; 2 | 3 | use super::test_oneof_pb::*; 4 | 5 | #[test] 6 | fn test_simple() { 7 | let mut test_message = TestOneof::new(); 8 | test_message.set_uint32_field(150); 9 | test_serialize_deserialize("28 96 01", &test_message); 10 | } 11 | 12 | #[test] 13 | fn test_set_clear_field() { 14 | let mut test_message = TestOneof::new(); 15 | 16 | test_message.set_int32_field(10); 17 | assert!(test_message.has_int32_field()); 18 | assert_eq!(10, test_message.get_int32_field()); 19 | assert!(!test_message.has_bool_field()); 20 | assert_eq!(false, test_message.get_bool_field()); 21 | 22 | test_message.set_bool_field(true); 23 | assert!(test_message.has_bool_field()); 24 | assert_eq!(true, test_message.get_bool_field()); 25 | assert!(!test_message.has_int32_field()); 26 | assert_eq!(0, test_message.get_int32_field()); 27 | 28 | test_message.clear_int32_field(); 29 | assert!(!test_message.has_int32_field()); 30 | assert!(!test_message.has_bool_field()); 31 | assert_eq!(false, test_message.get_bool_field()); 32 | assert_eq!(0, test_message.get_int32_field()); 33 | } 34 | 35 | #[test] 36 | fn test_types() { 37 | fn t(f: F) 38 | where 39 | F : Fn(&mut TestOneof), 40 | { 41 | let mut o = TestOneof::new(); 42 | f(&mut o); 43 | test_serialize_deserialize_no_hex(&o); 44 | } 45 | 46 | t(|o| o.set_double_field(10.0)); 47 | t(|o| o.set_float_field(11.0)); 48 | t(|o| o.set_int32_field(12)); 49 | t(|o| o.set_int64_field(13)); 50 | t(|o| o.set_uint32_field(14)); 51 | t(|o| o.set_uint64_field(15)); 52 | t(|o| o.set_sint32_field(16)); 53 | t(|o| o.set_sint64_field(17)); 54 | t(|o| o.set_fixed32_field(18)); 55 | t(|o| o.set_fixed64_field(19)); 56 | t(|o| o.set_sfixed32_field(20)); 57 | t(|o| o.set_sfixed64_field(21)); 58 | t(|o| o.set_bool_field(true)); 59 | t(|o| o.set_string_field("asas".to_string())); 60 | t(|o| o.set_bytes_field(vec![99, 100])); 61 | t(|o| o.set_enum_field(EnumForOneof::A)); 62 | t(|o| o.mut_message_field().set_f(22)); 63 | } 64 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_oneof_expose.rs: -------------------------------------------------------------------------------- 1 | use super::test_oneof_expose_pb::*; 2 | 3 | #[test] 4 | fn test_simple() { 5 | let _aaa = TestOneofExpose::new().aaa; 6 | } 7 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_oneof_expose_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | import "rustproto.proto"; 4 | 5 | option (rustproto.expose_oneof_all) = true; 6 | 7 | message TestOneofExpose { 8 | oneof aaa { 9 | string s = 1; 10 | int32 x = 2; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_oneof_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | enum EnumForOneof { 4 | UNKNOWN = 0; 5 | A = 10; 6 | } 7 | 8 | message MessageForOneof { 9 | optional int32 f = 1; 10 | } 11 | 12 | message TestOneof { 13 | optional string s = 29; 14 | oneof one { 15 | double double_field = 1; 16 | float float_field = 2; 17 | int32 int32_field = 3; 18 | int64 int64_field = 4; 19 | uint32 uint32_field = 5; 20 | uint64 uint64_field = 6; 21 | sint32 sint32_field = 7; 22 | sint64 sint64_field = 8; 23 | fixed32 fixed32_field = 9; 24 | fixed64 fixed64_field = 10; 25 | sfixed32 sfixed32_field = 11; 26 | sfixed64 sfixed64_field = 12; 27 | bool bool_field = 13; 28 | string string_field = 14; 29 | bytes bytes_field = 15; 30 | EnumForOneof enum_field = 16; 31 | MessageForOneof message_field = 17; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_oneof_recursive.rs: -------------------------------------------------------------------------------- 1 | use super::test_oneof_recursive_pb::*; 2 | 3 | #[test] 4 | fn test() { 5 | let _ = LinkedList::new(); 6 | } 7 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_oneof_recursive_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package test_oneof_recursive; 4 | 5 | message LinkedList { 6 | oneof x { 7 | bool tail = 1; 8 | LinkedList node = 2; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_reflect.rs: -------------------------------------------------------------------------------- 1 | use super::test_reflect_pb::M; 2 | 3 | use protobuf::Message; 4 | 5 | #[test] 6 | fn test_get_sub_message_via_reflection() { 7 | let mut m = M::new(); 8 | m.mut_sub_m().set_n(42); 9 | assert!(m.has_sub_m()); 10 | 11 | let descriptor = m.descriptor().field_by_name("sub_m"); 12 | assert_eq!("sub_m", descriptor.name()); 13 | 14 | let sub_m = descriptor.get_message(&m); 15 | assert_eq!("SubM", sub_m.descriptor().full_name()); 16 | assert_eq!(42, sub_m.descriptor().field_by_name("n").get_i32(sub_m)); 17 | } 18 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_reflect_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | message M { 4 | optional SubM sub_m = 1; 5 | } 6 | 7 | message SubM { 8 | optional int32 n = 1; 9 | } 10 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_repeated_packed.rs: -------------------------------------------------------------------------------- 1 | use super::test_repeated_packed_pb::*; 2 | 3 | use protobuf_test_common::*; 4 | 5 | 6 | // varint field number = 4 7 | // unpacked tag = 20 8 | // packed tag = 22 9 | // sfixed32 field number = 5 10 | // unpacked tag = 2d 11 | // packed tag = 2a 12 | 13 | #[test] 14 | fn test_write_unpacked() { 15 | let mut test = TestUnpacked::new(); 16 | test.set_varints([17i32, 1000].to_vec()); 17 | test_serialize_deserialize("20 11 20 e8 07", &test); 18 | let mut test = TestUnpacked::new(); 19 | test.set_sfixed32s([17i32, 1000].to_vec()); 20 | test_serialize_deserialize("2d 11 00 00 00 2d e8 03 00 00", &test); 21 | } 22 | 23 | #[test] 24 | fn test_read_unpacked_to_unpacked() { 25 | let mut test = TestUnpacked::new(); 26 | test.set_varints([17i32, 1000].to_vec()); 27 | test_deserialize("20 11 20 e8 07", &test); 28 | let mut test = TestUnpacked::new(); 29 | test.set_sfixed32s([17i32, 1000].to_vec()); 30 | test_deserialize("2d 11 00 00 00 2d e8 03 00 00", &test); 31 | } 32 | 33 | #[test] 34 | fn test_read_packed_to_unpacked() { 35 | let mut test = TestUnpacked::new(); 36 | test.set_varints([17i32, 1000].to_vec()); 37 | test_deserialize("22 03 11 e8 07", &test); 38 | let mut test = TestUnpacked::new(); 39 | test.set_sfixed32s([17i32, 1000].to_vec()); 40 | test_deserialize("2a 08 11 00 00 00 e8 03 00 00", &test); 41 | } 42 | 43 | 44 | #[test] 45 | fn test_write_packed() { 46 | let mut test = TestPacked::new(); 47 | test.set_varints([17i32, 1000].to_vec()); 48 | test_serialize_deserialize("22 03 11 e8 07", &test); 49 | let mut test = TestPacked::new(); 50 | test.set_sfixed32s([17i32, 1000].to_vec()); 51 | test_serialize_deserialize("2a 08 11 00 00 00 e8 03 00 00", &test); 52 | } 53 | 54 | #[test] 55 | fn test_read_unpacked_to_packed() { 56 | let mut test = TestPacked::new(); 57 | test.set_varints([17i32, 1000].to_vec()); 58 | test_deserialize("20 11 20 e8 07", &test); 59 | let mut test = TestPacked::new(); 60 | test.set_sfixed32s([17i32, 1000].to_vec()); 61 | test_deserialize("2d 11 00 00 00 2d e8 03 00 00", &test); 62 | } 63 | 64 | #[test] 65 | fn test_read_packed_to_packed() { 66 | let mut test = TestPacked::new(); 67 | test.set_varints([17i32, 1000].to_vec()); 68 | test_deserialize("22 03 11 e8 07", &test); 69 | let mut test = TestPacked::new(); 70 | test.set_sfixed32s([17i32, 1000].to_vec()); 71 | test_deserialize("2a 08 11 00 00 00 e8 03 00 00", &test); 72 | } 73 | 74 | #[test] 75 | fn test_issue_281() { 76 | // Data len len was incorrectly computed. 77 | // For 100 elements, bytes len is 400 78 | // and varint len of 400 is 2, 79 | // while varint len of 100 is 1. 80 | let mut test = TestIssue281::new(); 81 | test.set_values((0..100).collect()); 82 | test_serialize_deserialize_no_hex(&test); 83 | } 84 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_repeated_packed_pb.proto: -------------------------------------------------------------------------------- 1 | message TestPacked { 2 | repeated int32 varints = 4 [packed=true]; 3 | repeated sfixed32 sfixed32s = 5 [packed=true]; 4 | } 5 | 6 | message TestUnpacked { 7 | repeated int32 varints = 4 [packed=false]; 8 | repeated sfixed32 sfixed32s = 5 [packed=false]; 9 | } 10 | 11 | message TestIssue281 { 12 | repeated fixed32 values = 1 [packed=true]; 13 | } 14 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_root.rs: -------------------------------------------------------------------------------- 1 | use super::test_root_pb::*; 2 | 3 | #[test] 4 | fn test() { 5 | let _ = Root::new(); 6 | } 7 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_root_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | // no namespace declaration 4 | 5 | message Root { 6 | message Nested { 7 | } 8 | 9 | // accessing nested message in root namespace 10 | repeated .Root.Nested nested = 1; 11 | } 12 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_sync.rs: -------------------------------------------------------------------------------- 1 | use std::thread; 2 | use std::sync::Arc; 3 | 4 | use protobuf::CodedInputStream; 5 | use protobuf::Message; 6 | 7 | use super::test_sync_pb::*; 8 | 9 | // test messages are sync 10 | #[test] 11 | fn test_sync() { 12 | let m = Arc::new({ 13 | let mut r = TestSync::new(); 14 | r.set_int32_field(23); 15 | r 16 | }); 17 | 18 | let threads: Vec<_> = (0..4) 19 | .map(|_| { 20 | let m_copy = m.clone(); 21 | thread::spawn(move || { 22 | let bytes = m_copy.write_to_bytes().unwrap(); 23 | let mut is = CodedInputStream::from_bytes(&bytes); 24 | let mut read = TestSync::new(); 25 | // API is not very convenient here 26 | read.merge_from(&mut is).unwrap(); 27 | read.check_initialized().unwrap(); 28 | read.get_int32_field() 29 | }) 30 | }) 31 | .collect(); 32 | 33 | let results = threads 34 | .into_iter() 35 | .map(|t| t.join().unwrap()) 36 | .collect::>(); 37 | assert_eq!(&[23, 23, 23, 23], &results[..]); 38 | } 39 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_sync_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | message TestSync { 4 | optional int32 int32_field = 3; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v2/test_text_format_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | enum TestEnum { 4 | UNKNOWN = 0; 5 | DARK = 1; 6 | LIGHT = 2; 7 | } 8 | 9 | message TestMessage { 10 | optional int32 value = 10; 11 | } 12 | 13 | message TestTypes { 14 | optional double double_singular = 1; 15 | optional float float_singular = 2; 16 | optional int32 int32_singular = 3; 17 | optional int64 int64_singular = 4; 18 | optional uint32 uint32_singular = 5; 19 | optional uint64 uint64_singular = 6; 20 | optional sint32 sint32_singular = 7; 21 | optional sint64 sint64_singular = 8; 22 | optional fixed32 fixed32_singular = 9; 23 | optional fixed64 fixed64_singular = 10; 24 | optional sfixed32 sfixed32_singular = 11; 25 | optional sfixed64 sfixed64_singular = 12; 26 | optional bool bool_singular = 13; 27 | optional string string_singular = 14; 28 | optional bytes bytes_singular = 15; 29 | optional TestEnum test_enum_singular = 16; 30 | optional TestMessage test_message_singular = 17; 31 | 32 | repeated double double_repeated = 31; 33 | repeated float float_repeated = 32; 34 | repeated int32 int32_repeated = 33; 35 | repeated int64 int64_repeated = 34; 36 | repeated uint32 uint32_repeated = 35; 37 | repeated uint64 uint64_repeated = 36; 38 | repeated sint32 sint32_repeated = 37; 39 | repeated sint64 sint64_repeated = 38; 40 | repeated fixed32 fixed32_repeated = 39; 41 | repeated fixed64 fixed64_repeated = 40; 42 | repeated sfixed32 sfixed32_repeated = 41; 43 | repeated sfixed64 sfixed64_repeated = 42; 44 | repeated bool bool_repeated = 43; 45 | repeated string string_repeated = 44; 46 | repeated bytes bytes_repeated = 45; 47 | repeated TestEnum test_enum_repeated = 46; 48 | repeated TestMessage test_message_repeated = 47; 49 | } 50 | 51 | message TestTextFormatRustIdentifier { 52 | optional bool const = 1; 53 | } 54 | -------------------------------------------------------------------------------- /protobuf-test/src/common/v3/.gitignore: -------------------------------------------------------------------------------- 1 | *.rs 2 | *.proto 3 | -------------------------------------------------------------------------------- /protobuf-test/src/google/mod.rs: -------------------------------------------------------------------------------- 1 | mod protobuf; 2 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/.gitignore: -------------------------------------------------------------------------------- 1 | unittest*.rs 2 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/README.md: -------------------------------------------------------------------------------- 1 | Files copied from 2 | [Google protobuf implementation](https://github.com/google/protobuf/tree/master/src/google/protobuf) 3 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/import-tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | cd $(dirname $0) 4 | 5 | rm -rf *.proto protobuf-git 6 | 7 | git clone --branch v3.5.2 --depth 1 https://github.com/google/protobuf/ protobuf-git 8 | 9 | cp protobuf-git/src/google/protobuf/unittest*.proto ./ 10 | 11 | rm -rf protobuf-git 12 | 13 | # https://github.com/rust-lang/rust/issues/40119 14 | rm unittest_enormous_descriptor.proto 15 | rm *_proto3.proto 16 | 17 | ( 18 | exec > mod.rs 19 | echo "// Generated by $0" 20 | echo 21 | find . -name '*.proto' | sed -e 's,\.proto$,;,; s,^./,mod ,' 22 | ) 23 | 24 | # vim: set ts=4 sw=4 et: 25 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/mod.rs: -------------------------------------------------------------------------------- 1 | // Generated by ./import-tests.sh 2 | 3 | mod unittest_no_field_presence; 4 | mod unittest_optimize_for; 5 | mod unittest_lazy_dependencies_custom_option; 6 | mod unittest_drop_unknown_fields; 7 | mod unittest_well_known_types; 8 | mod unittest_embed_optimize_for; 9 | mod unittest_arena; 10 | mod unittest_preserve_unknown_enum; 11 | mod unittest_lite; 12 | mod unittest_proto3_arena; 13 | mod unittest_import; 14 | mod unittest_no_generic_services; 15 | mod unittest_import_public; 16 | mod unittest; 17 | mod unittest_no_arena_import; 18 | mod unittest_no_arena; 19 | mod unittest_mset_wire_format; 20 | mod unittest_empty; 21 | mod unittest_import_public_lite; 22 | mod unittest_mset; 23 | mod unittest_no_arena_lite; 24 | mod unittest_import_lite; 25 | mod unittest_lazy_dependencies; 26 | mod unittest_proto3_lite; 27 | mod unittest_proto3_arena_lite; 28 | mod unittest_lite_imports_nonlite; 29 | mod unittest_lazy_dependencies_enum; 30 | mod unittest_preserve_unknown_enum2; 31 | mod unittest_custom_options; 32 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_arena.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto2"; 32 | 33 | import "google/protobuf/unittest_no_arena_import.proto"; 34 | 35 | package proto2_arena_unittest; 36 | 37 | option cc_enable_arenas = true; 38 | 39 | message NestedMessage { 40 | optional int32 d = 1; 41 | } 42 | 43 | message ArenaMessage { 44 | repeated NestedMessage repeated_nested_message = 1; 45 | repeated ImportNoArenaNestedMessage repeated_import_no_arena_message = 2; 46 | }; 47 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_drop_unknown_fields.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto3"; 32 | 33 | package unittest_drop_unknown_fields; 34 | option objc_class_prefix = "DropUnknowns"; 35 | 36 | option csharp_namespace = "Google.Protobuf.TestProtos"; 37 | 38 | message Foo { 39 | enum NestedEnum { 40 | FOO = 0; 41 | BAR = 1; 42 | BAZ = 2; 43 | } 44 | int32 int32_value = 1; 45 | NestedEnum enum_value = 2; 46 | } 47 | 48 | message FooWithExtraFields { 49 | enum NestedEnum { 50 | FOO = 0; 51 | BAR = 1; 52 | BAZ = 2; 53 | QUX = 3; 54 | } 55 | int32 int32_value = 1; 56 | NestedEnum enum_value = 2; 57 | int32 extra_int32_value = 3; 58 | } 59 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_embed_optimize_for.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: kenton@google.com (Kenton Varda) 32 | // Based on original Protocol Buffers design by 33 | // Sanjay Ghemawat, Jeff Dean, and others. 34 | // 35 | // A proto file which imports a proto file that uses optimize_for = CODE_SIZE. 36 | 37 | syntax = "proto2"; 38 | import "google/protobuf/unittest_optimize_for.proto"; 39 | 40 | package protobuf_unittest; 41 | 42 | // We optimize for speed here, but we are importing a proto that is optimized 43 | // for code size. 44 | option optimize_for = SPEED; 45 | 46 | message TestEmbedOptimizedForSize { 47 | // Test that embedding a message which has optimize_for = CODE_SIZE into 48 | // one optimized for speed works. 49 | optional TestOptimizedForSize optional_message = 1; 50 | repeated TestOptimizedForSize repeated_message = 2; 51 | } 52 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_empty.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: kenton@google.com (Kenton Varda) 32 | // Based on original Protocol Buffers design by 33 | // Sanjay Ghemawat, Jeff Dean, and others. 34 | // 35 | // This file intentionally left blank. (At one point this wouldn't compile 36 | // correctly.) 37 | 38 | syntax = "proto2"; 39 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_import.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: kenton@google.com (Kenton Varda) 32 | // Based on original Protocol Buffers design by 33 | // Sanjay Ghemawat, Jeff Dean, and others. 34 | // 35 | // A proto file which is imported by unittest.proto to test importing. 36 | 37 | syntax = "proto2"; 38 | 39 | // We don't put this in a package within proto2 because we need to make sure 40 | // that the generated code doesn't depend on being in the proto2 namespace. 41 | // In test_util.h we do 42 | // "using namespace unittest_import = protobuf_unittest_import". 43 | package protobuf_unittest_import; 44 | 45 | option optimize_for = SPEED; 46 | option cc_enable_arenas = true; 47 | 48 | // Exercise the java_package option. 49 | option java_package = "com.google.protobuf.test"; 50 | 51 | // Do not set a java_outer_classname here to verify that Proto2 works without 52 | // one. 53 | 54 | // Test public import 55 | import public "google/protobuf/unittest_import_public.proto"; 56 | 57 | message ImportMessage { 58 | optional int32 d = 1; 59 | } 60 | 61 | enum ImportEnum { 62 | IMPORT_FOO = 7; 63 | IMPORT_BAR = 8; 64 | IMPORT_BAZ = 9; 65 | } 66 | 67 | 68 | // To use an enum in a map, it must has the first value as 0. 69 | enum ImportEnumForMap { 70 | UNKNOWN = 0; 71 | FOO = 1; 72 | BAR = 2; 73 | } 74 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_import_lite.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: kenton@google.com (Kenton Varda) 32 | // 33 | // This is like unittest_import.proto but with optimize_for = LITE_RUNTIME. 34 | 35 | syntax = "proto2"; 36 | package protobuf_unittest_import; 37 | 38 | option optimize_for = LITE_RUNTIME; 39 | 40 | option java_package = "com.google.protobuf"; 41 | 42 | import public "google/protobuf/unittest_import_public_lite.proto"; 43 | 44 | message ImportMessageLite { 45 | optional int32 d = 1; 46 | } 47 | 48 | enum ImportEnumLite { 49 | IMPORT_LITE_FOO = 7; 50 | IMPORT_LITE_BAR = 8; 51 | IMPORT_LITE_BAZ = 9; 52 | } 53 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_import_public.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: liujisi@google.com (Pherl Liu) 32 | 33 | syntax = "proto2"; 34 | 35 | package protobuf_unittest_import; 36 | 37 | option java_package = "com.google.protobuf.test"; 38 | 39 | message PublicImportMessage { 40 | optional int32 e = 1; 41 | } 42 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_import_public_lite.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: liujisi@google.com (Pherl Liu) 32 | 33 | syntax = "proto2"; 34 | 35 | package protobuf_unittest_import; 36 | 37 | option optimize_for = LITE_RUNTIME; 38 | 39 | option java_package = "com.google.protobuf"; 40 | 41 | message PublicImportMessageLite { 42 | optional int32 e = 1; 43 | } 44 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_lazy_dependencies.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: trafacz@google.com (Todd Rafacz) 32 | // Based on original Protocol Buffers design by 33 | // Sanjay Ghemawat, Jeff Dean, and others. 34 | // 35 | // A proto file we will use for unit testing. 36 | 37 | syntax = "proto2"; 38 | 39 | import "google/protobuf/unittest_lazy_dependencies_custom_option.proto"; 40 | 41 | // Some generic_services option(s) added automatically. 42 | // See: http://go/proto2-generic-services-default 43 | option cc_generic_services = true; // auto-added 44 | option java_generic_services = true; // auto-added 45 | option py_generic_services = true; // auto-added 46 | option cc_enable_arenas = true; 47 | 48 | // We don't put this in a package within proto2 because we need to make sure 49 | // that the generated code doesn't depend on being in the proto2 namespace. 50 | // In test_util.h we do "using namespace unittest = protobuf_unittest". 51 | package protobuf_unittest.lazy_imports; 52 | 53 | // Protos optimized for SPEED use a strict superset of the generated code 54 | // of equivalent ones optimized for CODE_SIZE, so we should optimize all our 55 | // tests for speed unless explicitly testing code size optimization. 56 | option optimize_for = SPEED; 57 | 58 | option java_outer_classname = "UnittestLazyImportsProto"; 59 | 60 | // The following are used to test that the proto file 61 | // with the definition of the following field types is 62 | // not built when this proto file is built. Then test 63 | // that calling message_type() etc will build the correct 64 | // descriptor lazily and return it. 65 | 66 | message ImportedMessage { 67 | optional LazyMessage lazy_message = 1; 68 | } 69 | 70 | message MessageCustomOption { 71 | } 72 | 73 | message MessageCustomOption2 { 74 | option (lazy_enum_option) = LAZY_ENUM_0; 75 | } 76 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_lazy_dependencies_custom_option.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: trafacz@google.com (Todd Rafacz) 32 | // Based on original Protocol Buffers design by 33 | // Sanjay Ghemawat, Jeff Dean, and others. 34 | // 35 | // A proto file we will use for unit testing. 36 | 37 | syntax = "proto2"; 38 | 39 | import "google/protobuf/unittest_lazy_dependencies_enum.proto"; 40 | import "google/protobuf/descriptor.proto"; 41 | 42 | // Some generic_services option(s) added automatically. 43 | // See: http://go/proto2-generic-services-default 44 | option cc_generic_services = true; // auto-added 45 | option java_generic_services = true; // auto-added 46 | option py_generic_services = true; // auto-added 47 | option cc_enable_arenas = true; 48 | 49 | // We don't put this in a package within proto2 because we need to make sure 50 | // that the generated code doesn't depend on being in the proto2 namespace. 51 | // In test_util.h we do "using namespace unittest = protobuf_unittest". 52 | package protobuf_unittest.lazy_imports; 53 | 54 | // Protos optimized for SPEED use a strict superset of the generated code 55 | // of equivalent ones optimized for CODE_SIZE, so we should optimize all our 56 | // tests for speed unless explicitly testing code size optimization. 57 | option optimize_for = SPEED; 58 | 59 | option java_outer_classname = "UnittestLazyImportsCustomOptionProto"; 60 | 61 | message LazyMessage { 62 | optional int32 a = 1; 63 | } 64 | 65 | extend google.protobuf.MessageOptions { 66 | optional LazyEnum lazy_enum_option = 138596335 [default = LAZY_ENUM_1]; 67 | } 68 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_lazy_dependencies_enum.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: trafacz@google.com (Todd Rafacz) 32 | // Based on original Protocol Buffers design by 33 | // Sanjay Ghemawat, Jeff Dean, and others. 34 | // 35 | // A proto file we will use for unit testing. 36 | 37 | syntax = "proto2"; 38 | 39 | // Some generic_services option(s) added automatically. 40 | // See: http://go/proto2-generic-services-default 41 | option cc_generic_services = true; // auto-added 42 | option java_generic_services = true; // auto-added 43 | option py_generic_services = true; // auto-added 44 | option cc_enable_arenas = true; 45 | 46 | // We don't put this in a package within proto2 because we need to make sure 47 | // that the generated code doesn't depend on being in the proto2 namespace. 48 | // In test_util.h we do "using namespace unittest = protobuf_unittest". 49 | package protobuf_unittest.lazy_imports; 50 | 51 | // Protos optimized for SPEED use a strict superset of the generated code 52 | // of equivalent ones optimized for CODE_SIZE, so we should optimize all our 53 | // tests for speed unless explicitly testing code size optimization. 54 | option optimize_for = SPEED; 55 | 56 | option java_outer_classname = "UnittestLazyImportsEnumProto"; 57 | 58 | enum LazyEnum { 59 | LAZY_ENUM_0 = 0; 60 | LAZY_ENUM_1 = 1; 61 | } 62 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_lite_imports_nonlite.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: kenton@google.com (Kenton Varda) 32 | // 33 | // Tests that a "lite" message can import a regular message. 34 | 35 | syntax = "proto2"; 36 | package protobuf_unittest; 37 | 38 | import "google/protobuf/unittest.proto"; 39 | 40 | option optimize_for = LITE_RUNTIME; 41 | 42 | message TestLiteImportsNonlite { 43 | optional TestAllTypes message = 1; 44 | } 45 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_mset.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: kenton@google.com (Kenton Varda) 32 | // Based on original Protocol Buffers design by 33 | // Sanjay Ghemawat, Jeff Dean, and others. 34 | // 35 | // This file is similar to unittest_mset_wire_format.proto, but does not 36 | // have a TestMessageSet, so it can be downgraded to proto1. 37 | 38 | syntax = "proto2"; 39 | 40 | import "google/protobuf/unittest_mset_wire_format.proto"; 41 | 42 | package protobuf_unittest; 43 | 44 | option cc_enable_arenas = true; 45 | option optimize_for = SPEED; 46 | 47 | message TestMessageSetContainer { 48 | optional proto2_wireformat_unittest.TestMessageSet message_set = 1; 49 | } 50 | 51 | message TestMessageSetExtension1 { 52 | extend proto2_wireformat_unittest.TestMessageSet { 53 | optional TestMessageSetExtension1 message_set_extension = 1545008; 54 | } 55 | optional int32 i = 15; 56 | } 57 | 58 | message TestMessageSetExtension2 { 59 | extend proto2_wireformat_unittest.TestMessageSet { 60 | optional TestMessageSetExtension2 message_set_extension = 1547769; 61 | } 62 | optional string str = 25; 63 | } 64 | 65 | // This message was used to generate 66 | // //net/proto2/python/internal/testdata/message_set_message, but is commented 67 | // out since it must not actually exist in code, to simulate an "unknown" 68 | // extension. 69 | // message TestMessageSetUnknownExtension { 70 | // extend TestMessageSet { 71 | // optional TestMessageSetUnknownExtension message_set_extension = 56141421; 72 | // } 73 | // optional int64 a = 1; 74 | // } 75 | 76 | // MessageSet wire format is equivalent to this. 77 | message RawMessageSet { 78 | repeated group Item = 1 { 79 | required int32 type_id = 2; 80 | required bytes message = 3; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_mset_wire_format.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: kenton@google.com (Kenton Varda) 32 | // Based on original Protocol Buffers design by 33 | // Sanjay Ghemawat, Jeff Dean, and others. 34 | // 35 | // This file contains messages for testing message_set_wire_format. 36 | 37 | syntax = "proto2"; 38 | package proto2_wireformat_unittest; 39 | 40 | option cc_enable_arenas = true; 41 | option optimize_for = SPEED; 42 | option csharp_namespace = "Google.ProtocolBuffers.TestProtos"; 43 | 44 | // A message with message_set_wire_format. 45 | message TestMessageSet { 46 | option message_set_wire_format = true; 47 | extensions 4 to max; 48 | } 49 | 50 | message TestMessageSetWireFormatContainer { 51 | optional TestMessageSet message_set = 1; 52 | } 53 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_no_arena_import.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto2"; 32 | 33 | package proto2_arena_unittest; 34 | 35 | message ImportNoArenaNestedMessage { 36 | optional int32 d = 1; 37 | }; 38 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_no_arena_lite.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto2"; 32 | 33 | option optimize_for = LITE_RUNTIME; 34 | 35 | // We don't put this in a package within proto2 because we need to make sure 36 | // that the generated code doesn't depend on being in the proto2 namespace. 37 | // In test_util.h we do "using namespace unittest = protobuf_unittest". 38 | package protobuf_unittest_no_arena; 39 | 40 | message ForeignMessageLite { 41 | optional int32 c = 1; 42 | } 43 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_no_generic_services.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: kenton@google.com (Kenton Varda) 32 | 33 | syntax = "proto2"; 34 | package google.protobuf.no_generic_services_test; 35 | 36 | 37 | // *_generic_services are false by default. 38 | 39 | message TestMessage { 40 | optional int32 a = 1; 41 | extensions 1000 to max; 42 | } 43 | 44 | enum TestEnum { 45 | FOO = 1; 46 | } 47 | 48 | extend TestMessage { 49 | optional int32 test_extension = 1000; 50 | } 51 | 52 | service TestService { 53 | rpc Foo(TestMessage) returns(TestMessage); 54 | } 55 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_optimize_for.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: kenton@google.com (Kenton Varda) 32 | // Based on original Protocol Buffers design by 33 | // Sanjay Ghemawat, Jeff Dean, and others. 34 | // 35 | // A proto file which uses optimize_for = CODE_SIZE. 36 | 37 | syntax = "proto2"; 38 | import "google/protobuf/unittest.proto"; 39 | 40 | package protobuf_unittest; 41 | 42 | option optimize_for = CODE_SIZE; 43 | 44 | message TestOptimizedForSize { 45 | optional int32 i = 1; 46 | optional ForeignMessage msg = 19; 47 | 48 | extensions 1000 to max; 49 | 50 | extend TestOptimizedForSize { 51 | optional int32 test_extension = 1234; 52 | optional TestRequiredOptimizedForSize test_extension2 = 1235; 53 | } 54 | 55 | oneof foo { 56 | int32 integer_field = 2; 57 | string string_field = 3; 58 | } 59 | } 60 | 61 | message TestRequiredOptimizedForSize { 62 | required int32 x = 1; 63 | } 64 | 65 | message TestOptionalOptimizedForSize { 66 | optional TestRequiredOptimizedForSize o = 1; 67 | } 68 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_preserve_unknown_enum.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto3"; 32 | 33 | package proto3_preserve_unknown_enum_unittest; 34 | option objc_class_prefix = "UnknownEnums"; 35 | 36 | option csharp_namespace = "Google.Protobuf.TestProtos"; 37 | 38 | enum MyEnum { 39 | FOO = 0; 40 | BAR = 1; 41 | BAZ = 2; 42 | } 43 | 44 | enum MyEnumPlusExtra { 45 | E_FOO = 0; 46 | E_BAR = 1; 47 | E_BAZ = 2; 48 | E_EXTRA = 3; 49 | } 50 | 51 | message MyMessage { 52 | MyEnum e = 1; 53 | repeated MyEnum repeated_e = 2; 54 | repeated MyEnum repeated_packed_e = 3 [packed=true]; 55 | repeated MyEnumPlusExtra repeated_packed_unexpected_e = 4; // not packed 56 | oneof o { 57 | MyEnum oneof_e_1 = 5; 58 | MyEnum oneof_e_2 = 6; 59 | } 60 | } 61 | 62 | message MyMessagePlusExtra { 63 | MyEnumPlusExtra e = 1; 64 | repeated MyEnumPlusExtra repeated_e = 2; 65 | repeated MyEnumPlusExtra repeated_packed_e = 3 [packed=true]; 66 | repeated MyEnumPlusExtra repeated_packed_unexpected_e = 4 [packed=true]; 67 | oneof o { 68 | MyEnumPlusExtra oneof_e_1 = 5; 69 | MyEnumPlusExtra oneof_e_2 = 6; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /protobuf-test/src/google/protobuf/unittest_preserve_unknown_enum2.proto: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // https://developers.google.com/protocol-buffers/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | syntax = "proto2"; 32 | 33 | package proto2_preserve_unknown_enum_unittest; 34 | 35 | enum MyEnum { 36 | FOO = 0; 37 | BAR = 1; 38 | BAZ = 2; 39 | } 40 | 41 | message MyMessage { 42 | optional MyEnum e = 1; 43 | repeated MyEnum repeated_e = 2; 44 | repeated MyEnum repeated_packed_e = 3 [packed=true]; 45 | repeated MyEnum repeated_packed_unexpected_e = 4; // not packed 46 | oneof o { 47 | MyEnum oneof_e_1 = 5; 48 | MyEnum oneof_e_2 = 6; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /protobuf-test/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![cfg(test)] 2 | 3 | extern crate protobuf; 4 | 5 | extern crate protobuf_test_common; 6 | 7 | #[cfg(feature = "with-bytes")] 8 | extern crate bytes; 9 | 10 | mod v2; 11 | 12 | // `cfg(proto3)` is emitted by `build.rs` 13 | 14 | #[cfg(proto3)] 15 | mod v3; 16 | 17 | mod common; 18 | 19 | #[cfg(proto3)] 20 | mod google; 21 | 22 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/.gitignore: -------------------------------------------------------------------------------- 1 | mod.rs 2 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/struct.proto: -------------------------------------------------------------------------------- 1 | // this file just checks that file name is properly escaped, 2 | // because `struct` is a keyword in rust 3 | message KeepTheFile {} 4 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test-sanitize-file-name_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | message FooBar {} 4 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_default_values.rs: -------------------------------------------------------------------------------- 1 | use std::f32; 2 | use std::f64; 3 | 4 | use super::test_default_values_pb::*; 5 | 6 | #[test] 7 | fn test_default_value_simple() { 8 | let d = TestDefaultValues::new(); 9 | assert_eq!(1.0, d.get_double_field()); 10 | assert_eq!(2.0, d.get_float_field()); 11 | assert_eq!(3, d.get_int32_field()); 12 | assert_eq!(4, d.get_int64_field()); 13 | assert_eq!(5, d.get_uint32_field()); 14 | assert_eq!(6, d.get_uint64_field()); 15 | assert_eq!(7, d.get_sint32_field()); 16 | assert_eq!(8, d.get_sint64_field()); 17 | assert_eq!(9, d.get_fixed32_field()); 18 | assert_eq!(10, d.get_fixed64_field()); 19 | assert_eq!(11, d.get_sfixed32_field()); 20 | assert_eq!(12, d.get_sfixed64_field()); 21 | assert_eq!(true, d.get_bool_field()); 22 | assert_eq!("abc\n22", d.get_string_field()); 23 | assert_eq!(b"cde\n33", d.get_bytes_field()); 24 | assert_eq!(EnumForDefaultValue::TWO, d.get_enum_field()); 25 | assert_eq!(EnumForDefaultValue::ONE, d.get_enum_field_without_default()); 26 | } 27 | 28 | #[test] 29 | fn test_default_value_extreme() { 30 | let d = TestExtremeDefaultValues::new(); 31 | assert_eq!(f64::INFINITY, d.get_inf_double()); 32 | assert_eq!(f64::NEG_INFINITY, d.get_neg_inf_double()); 33 | assert!(d.get_nan_double().is_nan()); 34 | assert_eq!(f32::INFINITY, d.get_inf_float()); 35 | assert_eq!(f32::NEG_INFINITY, d.get_neg_inf_float()); 36 | assert!(d.get_nan_float().is_nan()); 37 | assert_eq!( 38 | b"\0\x01\x07\x08\x0c\n\r\t\x0b\\\'\"\xfe", 39 | d.get_escaped_bytes() 40 | ) 41 | } 42 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_default_values_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package test_default_values; 4 | 5 | enum EnumForDefaultValue { 6 | ONE = 1; 7 | TWO = 2; 8 | THREE = 3; 9 | } 10 | 11 | message TestDefaultValues { 12 | optional double double_field = 1 [default = 1]; 13 | optional float float_field = 2 [default = 2]; 14 | 15 | optional int32 int32_field = 3 [default = 3]; 16 | optional int64 int64_field = 4 [default = 4]; 17 | optional uint32 uint32_field = 5 [default = 5]; 18 | optional uint64 uint64_field = 6 [default = 6]; 19 | optional sint32 sint32_field = 7 [default = 7]; 20 | optional sint64 sint64_field = 8 [default = 8]; 21 | optional fixed32 fixed32_field = 9 [default = 9]; 22 | optional fixed64 fixed64_field = 10 [default = 10]; 23 | optional sfixed32 sfixed32_field = 11 [default = 11]; 24 | optional sfixed64 sfixed64_field = 12 [default = 12]; 25 | optional bool bool_field = 13 [default = true]; 26 | optional string string_field = 14 [default = "abc\n22"]; 27 | optional bytes bytes_field = 15 [default = "cde\n33"]; 28 | optional EnumForDefaultValue enum_field = 16 [default = TWO]; 29 | optional EnumForDefaultValue enum_field_without_default = 17; 30 | } 31 | 32 | message TestExtremeDefaultValues { 33 | // Text for nonfinite floating-point values. 34 | optional bytes escaped_bytes = 1 [default = "\0\001\a\b\f\n\r\t\v\\\'\"\xfe"]; 35 | optional double inf_double = 14 [default = inf]; 36 | optional double neg_inf_double = 15 [default = -inf]; 37 | optional double nan_double = 16 [default = nan]; 38 | optional float inf_float = 17 [default = inf]; 39 | optional float neg_inf_float = 18 [default = -inf]; 40 | optional float nan_float = 19 [default = nan]; 41 | } 42 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_group_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | message MessageWithGroup { 4 | optional string aaa = 1; 5 | 6 | repeated group Identifier = 18 { 7 | optional int32 iii = 19; 8 | optional string sss = 20; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_import_nested_imported_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | message ContainerForNested { 4 | message NestedMessage { 5 | } 6 | 7 | enum NestedEnum { 8 | RED = 1; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_import_nested_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | import "test_import_nested_imported_pb.proto"; 4 | 5 | message ContainsImportedNested { 6 | optional ContainerForNested.NestedMessage m = 1; 7 | optional ContainerForNested.NestedEnum e = 2; 8 | } 9 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_import_nonunique_1_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package nonunique_1; 4 | 5 | message Nonunique {} 6 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_import_nonunique_2_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package nonunique_2; 4 | 5 | message Nonunique {} 6 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_import_nonunique_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | import "test_import_nonunique_1_pb.proto"; 4 | import "test_import_nonunique_2_pb.proto"; 5 | 6 | message TestImportNonunque { 7 | optional nonunique_1.Nonunique n1 = 1; 8 | optional nonunique_2.Nonunique n2 = 2; 9 | } 10 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_import_pkg_nested_imported_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package foo.baz; 4 | 5 | message ContainerForNested { 6 | message NestedMessage { 7 | } 8 | 9 | enum NestedEnum { 10 | RED = 1; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_import_pkg_nested_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | import "test_import_pkg_nested_imported_pb.proto"; 4 | 5 | package foo.bar; 6 | 7 | message ContainsImportedNested { 8 | optional foo.baz.ContainerForNested.NestedMessage m = 1; 9 | optional foo.baz.ContainerForNested.NestedEnum e = 2; 10 | } 11 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_import_root_imported_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | message ImportedMessage { 4 | } 5 | 6 | enum ImportedEnum { 7 | SOMETHING = 1; 8 | } 9 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_import_root_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | import "test_import_root_imported_pb.proto"; 4 | 5 | message ContainsImported { 6 | optional ImportedMessage imported_message = 1; 7 | optional ImportedEnum imported_enum = 2; 8 | } 9 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_oneof_default_value.rs: -------------------------------------------------------------------------------- 1 | use super::test_oneof_default_value_pb::*; 2 | 3 | #[test] 4 | fn test() { 5 | let m = TestOneofDefaultValue::new(); 6 | assert_eq!(9.0, m.get_double_field()); 7 | assert_eq!("ss", m.get_string_field()); 8 | assert_eq!(b"bb", m.get_bytes_field()); 9 | } 10 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_oneof_default_value_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package test_oneof_default_value_pb; 4 | 5 | enum EnumForOneofDefaultValue { 6 | A = 10; 7 | B = 20; 8 | } 9 | 10 | message MessageForOneofDefaultValue { 11 | optional int32 f = 1; 12 | } 13 | 14 | message TestOneofDefaultValue { 15 | optional string s = 29; 16 | oneof one { 17 | double double_field = 1 [default = 9.0]; 18 | float float_field = 2 [default = 10.0]; 19 | int32 int32_field = 3 [default = 11]; 20 | int64 int64_field = 4 [default = 12]; 21 | uint32 uint32_field = 5 [default = 13]; 22 | uint64 uint64_field = 6 [default = 14]; 23 | sint32 sint32_field = 7 [default = 15]; 24 | sint64 sint64_field = 8 [default = 16]; 25 | fixed32 fixed32_field = 9 [default = 17]; 26 | fixed64 fixed64_field = 10 [default = 18]; 27 | sfixed32 sfixed32_field = 11 [default = 19]; 28 | sfixed64 sfixed64_field = 12 [default = 20]; 29 | bool bool_field = 13 [default = true]; 30 | string string_field = 14 [default = "ss"]; 31 | bytes bytes_field = 15 [default = "bb"]; 32 | EnumForOneofDefaultValue enum_field = 16 [default = B]; 33 | MessageForOneofDefaultValue message_field = 17; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_required.rs: -------------------------------------------------------------------------------- 1 | use protobuf::*; 2 | 3 | use super::test_required_pb::*; 4 | 5 | 6 | #[test] 7 | #[should_panic] 8 | fn test_write_missing_required() { 9 | TestRequired::new().write_to_bytes().unwrap(); 10 | } 11 | 12 | #[test] 13 | #[should_panic] 14 | fn test_read_missing_required() { 15 | parse_from_bytes::(&[]).unwrap(); 16 | } 17 | 18 | #[test] 19 | fn test_is_initialized_is_recursive() { 20 | let mut m = TestRequiredOuter::new(); 21 | assert!(!m.is_initialized()); 22 | m.mut_inner(); 23 | assert!(!m.is_initialized()); 24 | m.mut_inner().set_b(false); 25 | assert!(m.is_initialized()); 26 | } 27 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_required_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package test_required; 4 | 5 | message TestRequired { 6 | required bool b = 5; 7 | } 8 | 9 | message TestRequiredOuter { 10 | required TestRequired inner = 1; 11 | } 12 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_special~characters file{name}_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | // More test that filenames are sanitized, even imports. 4 | 5 | package special; 6 | 7 | message Outer { 8 | } 9 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_unknown_suffix.rs: -------------------------------------------------------------------------------- 1 | use super::test_unknown_suffix_pb_proto3::TestUnknownSuffix; 2 | 3 | #[test] 4 | fn test() { 5 | TestUnknownSuffix::new(); 6 | } 7 | -------------------------------------------------------------------------------- /protobuf-test/src/v2/test_unknown_suffix_pb.proto3: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | message TestUnknownSuffix {} 4 | -------------------------------------------------------------------------------- /protobuf-test/src/v3/.gitignore: -------------------------------------------------------------------------------- 1 | mod.rs 2 | -------------------------------------------------------------------------------- /protobuf-test/src/v3/test_ident_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | message Vec { } 4 | 5 | message String { } 6 | 7 | message Option { } 8 | message None { } 9 | message Some { } 10 | 11 | message Message { } 12 | 13 | message TestType { 14 | oneof type { 15 | string s = 1; 16 | } 17 | repeated string struct = 2; 18 | repeated uint32 ref = 3; 19 | } 20 | -------------------------------------------------------------------------------- /protobuf-test/src/v3/test_issue_190_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package issue_190; 4 | 5 | message A { 6 | enum Type { 7 | STRING = 0; 8 | } 9 | } 10 | 11 | message B { 12 | message C { 13 | oneof something { 14 | A.Type type = 1; 15 | } 16 | } 17 | repeated C somethings = 1; 18 | } 19 | -------------------------------------------------------------------------------- /protobuf-test/src/v3/test_map.rs: -------------------------------------------------------------------------------- 1 | use protobuf::text_format::print_to_string; 2 | 3 | use super::test_map_pb::*; 4 | 5 | use protobuf_test_common::*; 6 | 7 | #[test] 8 | fn test_map() { 9 | let mut map = TestMap::new(); 10 | let mut entry = TestMapEntry::new(); 11 | entry.set_v(10); 12 | 13 | test_serialize_deserialize("", &map); 14 | 15 | map.mut_m().insert("two".to_owned(), 2); 16 | test_serialize_deserialize("0a 07 0a 03 74 77 6f 10 02", &map); 17 | 18 | map.mut_m().insert("sixty six".to_owned(), 66); 19 | // Insert map entry sub message 20 | map.mut_mm().insert("map".to_owned(), entry); 21 | // cannot (easily) test hex, because order is not specified 22 | test_serialize_deserialize_no_hex(&map); 23 | } 24 | 25 | #[test] 26 | fn test_map_with_object() { 27 | let mut map = TestMap::new(); 28 | 29 | let mut entry = TestMapEntry::new(); 30 | entry.set_v(10); 31 | 32 | test_serialize_deserialize("", &map); 33 | 34 | map.mut_mm().insert("map".to_owned(), entry); 35 | // cannot (easily) test hex, because order is not specified 36 | test_serialize_deserialize_no_hex(&map); 37 | } 38 | 39 | #[test] 40 | fn text_format() { 41 | let mut map = TestMap::new(); 42 | 43 | assert_eq!(&*print_to_string(&map), ""); 44 | 45 | map.mut_m().insert("two".to_owned(), 2); 46 | 47 | assert_eq!(&*print_to_string(&map), "m {key: \"two\" value: 2}") 48 | } 49 | -------------------------------------------------------------------------------- /protobuf-test/src/v3/test_map_carllerche.rs: -------------------------------------------------------------------------------- 1 | use bytes::Bytes; 2 | 3 | use protobuf::*; 4 | 5 | use protobuf_test_common::*; 6 | 7 | use super::test_map_carllerche_pb::*; 8 | 9 | #[test] 10 | fn test_string_to_int32() { 11 | let mut map = TestMapCarllerche::new(); 12 | map.string_to_int32.insert(Chars::from("abc"), 17); 13 | test_serialize_deserialize("0a 07 0a 03 61 62 63 10 11", &map); 14 | // field 1, length-delimited 15 | // length 16 | // field 1, wire type 2 17 | // length 18 | // a b c 19 | // field 2, varint 20 | // 17 21 | } 22 | 23 | #[test] 24 | fn test_int32_to_bytes() { 25 | let mut map = TestMapCarllerche::new(); 26 | map.int32_to_string.insert(17, Chars::from("abc")); 27 | test_serialize_deserialize("12 07 08 11 12 03 61 62 63", &map); 28 | let mut map = TestMapCarllerche::new(); 29 | map.int32_to_bytes.insert(17, Bytes::from("abc")); 30 | test_serialize_deserialize("1a 07 08 11 12 03 61 62 63", &map); 31 | } 32 | -------------------------------------------------------------------------------- /protobuf-test/src/v3/test_map_carllerche_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | import "rustproto.proto"; 4 | 5 | option (rustproto.carllerche_bytes_for_bytes_all) = true; 6 | option (rustproto.carllerche_bytes_for_string_all) = true; 7 | 8 | message TestMapCarllerche { 9 | map string_to_int32 = 1; 10 | map int32_to_string = 2; 11 | map int32_to_bytes = 3; 12 | } 13 | -------------------------------------------------------------------------------- /protobuf-test/src/v3/test_map_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | message TestMap { 4 | map m = 1; 5 | map mm = 2; 6 | } 7 | 8 | message TestMapEntry { 9 | int64 v = 1; 10 | } 11 | -------------------------------------------------------------------------------- /protobuf-test/src/v3/test_zeros_are_not_written.rs: -------------------------------------------------------------------------------- 1 | use protobuf_test_common::*; 2 | 3 | use super::test_zeros_are_not_written_pb::*; 4 | 5 | #[test] 6 | fn test_zeros_are_not_written() { 7 | let mut m = TestZerosAreNotWritten::new(); 8 | m.set_bool_field(false); 9 | m.set_enum_field(TestEnumDescriptor::UNDEFINED); 10 | m.set_fixed32_field(0); 11 | test_serialize("", &m); 12 | } 13 | -------------------------------------------------------------------------------- /protobuf-test/src/v3/test_zeros_are_not_written_pb.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | enum TestEnumDescriptor { 4 | UNDEFINED = 0; 5 | RED = 1; 6 | BLUE = 2; 7 | GREEN = 3; 8 | } 9 | 10 | message TestZerosAreNotWritten { 11 | double double_field = 1; 12 | float float_field = 2; 13 | int32 int32_field = 3; 14 | int64 int64_field = 4; 15 | uint32 uint32_field = 5; 16 | uint64 uint64_field = 6; 17 | sint32 sint32_field = 7; 18 | sint64 sint64_field = 8; 19 | fixed32 fixed32_field = 9; 20 | fixed64 fixed64_field = 10; 21 | sfixed32 sfixed32_field = 11; 22 | sfixed64 sfixed64_field = 12; 23 | bool bool_field = 13; 24 | string string_field = 14; 25 | bytes bytes_field = 15; 26 | TestEnumDescriptor enum_field = 16; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /protobuf-test/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -ex 2 | 3 | cd $(dirname $0) 4 | 5 | cargo test --features="$RUST_PROTOBUF_FEATURES" 6 | 7 | # vim: set ts=4 sw=4 et: 8 | -------------------------------------------------------------------------------- /protobuf/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | 3 | name = "protobuf" 4 | version = "1.6.0" 5 | authors = ["Stepan Koltsov "] 6 | license = "MIT/Apache-2.0" 7 | homepage = "https://github.com/stepancheg/rust-protobuf/" 8 | repository = "https://github.com/stepancheg/rust-protobuf/" 9 | documentation = "https://github.com/stepancheg/rust-protobuf/blob/master/README.md" 10 | description = """ 11 | Rust implementation of Google protocol buffers 12 | """ 13 | 14 | [lib] 15 | doctest = false 16 | 17 | [features] 18 | with-bytes = ["bytes"] 19 | 20 | [dependencies] 21 | bytes = { version = "0.*", optional = true } 22 | -------------------------------------------------------------------------------- /protobuf/README.md: -------------------------------------------------------------------------------- 1 | ## How to develop rust-protobuf itself 2 | 3 | `cargo test --all` to build everything. 4 | 5 | If code generator is changed, code needs to be regenerated, see 6 | `regenerate.sh`. 7 | -------------------------------------------------------------------------------- /protobuf/benches/coded_input_stream.rs: -------------------------------------------------------------------------------- 1 | #![feature(test)] 2 | 3 | extern crate test; 4 | extern crate protobuf; 5 | 6 | use std::io; 7 | use std::io::Read; 8 | 9 | use protobuf::stream::CodedInputStream; 10 | 11 | use self::test::Bencher; 12 | 13 | fn make_bytes(len: usize) -> Vec { 14 | let mut r = Vec::with_capacity(len); 15 | for i in 0..len { 16 | r.push((i % 10) as u8); 17 | } 18 | test::black_box(r) 19 | } 20 | 21 | #[bench] 22 | fn read_byte(b: &mut Bencher) { 23 | let v = make_bytes(1_000); 24 | b.iter(|| { 25 | let mut is = CodedInputStream::from_bytes(test::black_box(&v)); 26 | while !is.eof().expect("eof") { 27 | test::black_box(is.read_raw_byte().expect("read")); 28 | } 29 | }); 30 | } 31 | 32 | #[bench] 33 | fn read_byte_no_eof(b: &mut Bencher) { 34 | let v = make_bytes(1_000); 35 | b.iter(|| { 36 | let mut is = CodedInputStream::from_bytes(test::black_box(&v)); 37 | for _ in 0..v.len() { 38 | test::black_box(is.read_raw_byte().expect("read")); 39 | } 40 | assert!(is.eof().expect("eof")); 41 | }); 42 | } 43 | 44 | #[bench] 45 | fn read_byte_from_vec(b: &mut Bencher) { 46 | let v = make_bytes(1_000); 47 | b.iter(|| { 48 | let mut v = io::Cursor::new(test::black_box(&v)); 49 | loop { 50 | let mut buf = [0]; 51 | let count = v.read(&mut buf).expect("read"); 52 | if count == 0 { 53 | break; 54 | } 55 | test::black_box(buf); 56 | } 57 | }); 58 | } 59 | 60 | #[bench] 61 | fn read_varint_12(b: &mut Bencher) { 62 | let mut v = Vec::new(); 63 | { 64 | let mut v = protobuf::CodedOutputStream::vec(&mut v); 65 | for i in 0..1000 { 66 | // one or two byte varints 67 | v.write_raw_varint32((i * 7919) % (1 << 14)).expect("write"); 68 | } 69 | v.flush().expect("flush"); 70 | } 71 | b.iter(|| { 72 | let mut is = CodedInputStream::from_bytes(test::black_box(&v)); 73 | let mut count = 0; 74 | while !is.eof().expect("eof") { 75 | test::black_box(is.read_raw_varint32().expect("read")); 76 | count += 1; 77 | } 78 | assert_eq!(1000, count); 79 | }) 80 | } 81 | 82 | #[bench] 83 | fn read_varint_1(b: &mut Bencher) { 84 | let mut v = Vec::new(); 85 | { 86 | let mut v = protobuf::CodedOutputStream::vec(&mut v); 87 | for i in 0..1000 { 88 | // one or two byte varints 89 | v.write_raw_varint32((i * 7919) % (1 << 7)).expect("write"); 90 | } 91 | v.flush().expect("flush"); 92 | } 93 | b.iter(|| { 94 | let mut is = CodedInputStream::from_bytes(test::black_box(&v)); 95 | let mut count = 0; 96 | while !is.eof().expect("eof") { 97 | test::black_box(is.read_raw_varint32().expect("read")); 98 | count += 1; 99 | } 100 | assert_eq!(1000, count); 101 | }) 102 | } 103 | -------------------------------------------------------------------------------- /protobuf/benches/coded_output_stream.rs: -------------------------------------------------------------------------------- 1 | #![feature(test)] 2 | 3 | extern crate test; 4 | extern crate protobuf; 5 | 6 | use protobuf::stream; 7 | 8 | use self::test::Bencher; 9 | 10 | #[inline] 11 | fn buffer_write_byte(os: &mut stream::CodedOutputStream) { 12 | for i in 0..10 { 13 | os.write_raw_byte(test::black_box(i as u8)).unwrap(); 14 | } 15 | os.flush().unwrap(); 16 | } 17 | 18 | #[inline] 19 | fn buffer_write_bytes(os: &mut stream::CodedOutputStream) { 20 | for _ in 0..10 { 21 | os.write_raw_bytes(test::black_box(b"1234567890")).unwrap(); 22 | } 23 | os.flush().unwrap(); 24 | } 25 | 26 | #[bench] 27 | fn bench_buffer(b: &mut Bencher) { 28 | b.iter(|| { 29 | let mut v = Vec::new(); 30 | { 31 | let mut os = stream::CodedOutputStream::new(&mut v); 32 | buffer_write_byte(&mut os); 33 | } 34 | v 35 | }); 36 | } 37 | 38 | #[bench] 39 | fn bench_buffer_bytes(b: &mut Bencher) { 40 | b.iter(|| { 41 | let mut v = Vec::new(); 42 | { 43 | let mut os = stream::CodedOutputStream::new(&mut v); 44 | buffer_write_bytes(&mut os); 45 | } 46 | v 47 | }); 48 | } 49 | -------------------------------------------------------------------------------- /protobuf/full-rebuild.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -ex 2 | 3 | die() { 4 | echo "$@" >&2 5 | exit 1 6 | } 7 | 8 | cd $(dirname $0) 9 | 10 | # Build protoc-gen-rust 11 | cargo build --manifest-path ../protobuf-codegen/Cargo.toml 12 | 13 | protoc_ver=$(protoc --version) 14 | case "$protoc_ver" in 15 | "libprotoc 3"*) 16 | # Generate from descriptor.proto 17 | ./regenerate.sh 18 | # Build again with regenerated descriptor.proto 19 | cargo build --all 20 | ;; 21 | "libprotoc 2"*) 22 | echo "do not regenerate with proto 2" 23 | ;; 24 | *) 25 | die "unknown protoc version" 26 | ;; 27 | esac 28 | 29 | ../protobuf-test/test.sh 30 | 31 | # vim: set ts=4 sw=4 et: 32 | -------------------------------------------------------------------------------- /protobuf/regenerate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -ex 2 | 3 | cd $(dirname $0) 4 | 5 | die() { 6 | echo "$@" >&2 7 | exit 1 8 | } 9 | 10 | protoc_ver=$(protoc --version) 11 | case "$protoc_ver" in 12 | "libprotoc 3"*) ;; 13 | *) 14 | die "you need to use protobuf 3 to regenerate .rs from .proto" 15 | ;; 16 | esac 17 | 18 | where_am_i=$(cd `dirname $0`/..; pwd) 19 | PATH="$where_am_i/target/debug:$PATH" 20 | 21 | rm -rf tmp-generated 22 | mkdir tmp-generated 23 | 24 | protoc --rust_out tmp-generated -I../proto \ 25 | ../proto/google/protobuf/*.proto \ 26 | ../proto/google/protobuf/compiler/* \ 27 | ../proto/rustproto.proto 28 | 29 | mv tmp-generated/descriptor.rs tmp-generated/plugin.rs tmp-generated/rustproto.rs src/ 30 | mv tmp-generated/*.rs src/well_known_types/ 31 | ( 32 | cd src/well_known_types 33 | exec > mod.rs 34 | echo "// This file is generated. Do not edit" 35 | 36 | mod_list() { 37 | ls | grep -v mod.rs | sed -e 's,\.rs$,,' 38 | } 39 | 40 | echo 41 | mod_list | sed -e 's,^,mod ,; s,$,;,' 42 | 43 | echo 44 | mod_list | while read mod; do 45 | echo "pub use self::$mod::*;" 46 | done 47 | ) 48 | 49 | # vim: set ts=4 sw=4 et: 50 | -------------------------------------------------------------------------------- /protobuf/src/cached_size.rs: -------------------------------------------------------------------------------- 1 | use std::sync::atomic::AtomicUsize; 2 | use std::sync::atomic::Ordering; 3 | 4 | /// Cached size field used in generated code. 5 | /// It is always equal to itself to simplify generated code. 6 | /// (Generated code can use `#[derive(Eq)]`). 7 | #[derive(Debug, Default)] 8 | pub struct CachedSize { 9 | size: AtomicUsize, 10 | } 11 | 12 | impl CachedSize { 13 | pub fn get(&self) -> u32 { 14 | self.size.load(Ordering::Relaxed) as u32 15 | } 16 | 17 | pub fn set(&self, size: u32) { 18 | self.size.store(size as usize, Ordering::Relaxed) 19 | } 20 | } 21 | 22 | impl Clone for CachedSize { 23 | fn clone(&self) -> CachedSize { 24 | CachedSize { 25 | size: AtomicUsize::new(self.size.load(Ordering::Relaxed)) 26 | } 27 | } 28 | } 29 | 30 | impl PartialEq for CachedSize { 31 | fn eq(&self, _other: &CachedSize) -> bool { 32 | true 33 | } 34 | } 35 | 36 | impl Eq for CachedSize {} 37 | -------------------------------------------------------------------------------- /protobuf/src/chars.rs: -------------------------------------------------------------------------------- 1 | use std::str; 2 | use std::ops::Deref; 3 | use std::fmt; 4 | 5 | use bytes::Bytes; 6 | 7 | use clear::Clear; 8 | 9 | /// Thin wrapper around `Bytes` which guarantees that bytes are valid UTF-8 string. 10 | #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] 11 | pub struct Chars(Bytes); 12 | 13 | impl Chars { 14 | /// New empty object. 15 | pub fn new() -> Chars { 16 | Chars(Bytes::new()) 17 | } 18 | 19 | /// Try convert from `Bytes` 20 | pub fn from_bytes(bytes: Bytes) -> Result { 21 | str::from_utf8(&bytes)?; 22 | 23 | Ok(Chars(bytes)) 24 | } 25 | 26 | /// Len in bytes. 27 | pub fn len(&self) -> usize { 28 | self.0.len() 29 | } 30 | 31 | pub fn is_empty(&self) -> bool { 32 | self.0.is_empty() 33 | } 34 | } 35 | 36 | impl<'a> From<&'a str> for Chars { 37 | fn from(src: &'a str) -> Chars { 38 | Chars(Bytes::from(src)) 39 | } 40 | } 41 | 42 | impl From for Chars { 43 | fn from(src: String) -> Chars { 44 | Chars(Bytes::from(src)) 45 | } 46 | } 47 | 48 | impl Default for Chars { 49 | fn default() -> Self { 50 | Chars::new() 51 | } 52 | } 53 | 54 | impl Deref for Chars { 55 | type Target = str; 56 | 57 | fn deref(&self) -> &str { 58 | unsafe { str::from_utf8_unchecked(&self.0) } 59 | } 60 | } 61 | 62 | impl Clear for Chars { 63 | fn clear(&mut self) { 64 | self.0.clear(); 65 | } 66 | } 67 | 68 | impl fmt::Display for Chars { 69 | #[inline] 70 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 71 | fmt::Display::fmt(&**self, f) 72 | } 73 | } 74 | 75 | impl fmt::Debug for Chars { 76 | #[inline] 77 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 78 | fmt::Debug::fmt(&**self, f) 79 | } 80 | } 81 | 82 | 83 | #[cfg(test)] 84 | mod test { 85 | use super::Chars; 86 | 87 | #[test] 88 | fn test_display_and_debug() { 89 | let s = "test"; 90 | let string: String = s.into(); 91 | let chars: Chars = s.into(); 92 | 93 | assert_eq!(format!("{}", string), format!("{}", chars)); 94 | assert_eq!(format!("{:?}", string), format!("{:?}", chars)); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /protobuf/src/clear.rs: -------------------------------------------------------------------------------- 1 | #[cfg(feature = "bytes")] 2 | use bytes::Bytes; 3 | 4 | /// anything that can be cleared 5 | pub trait Clear { 6 | /// Clear this make, make it equivalent to newly created object. 7 | fn clear(&mut self); 8 | } 9 | 10 | impl Clear for Option { 11 | fn clear(&mut self) { 12 | self.take(); 13 | } 14 | } 15 | 16 | impl Clear for String { 17 | fn clear(&mut self) { 18 | String::clear(self); 19 | } 20 | } 21 | 22 | impl Clear for Vec { 23 | fn clear(&mut self) { 24 | Vec::clear(self); 25 | } 26 | } 27 | 28 | #[cfg(feature = "bytes")] 29 | impl Clear for Bytes { 30 | fn clear(&mut self) { 31 | Bytes::clear(self); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /protobuf/src/compiler_plugin.rs: -------------------------------------------------------------------------------- 1 | // TODO: move into separate crate 2 | #![doc(hidden)] 3 | 4 | use std::io::stdin; 5 | use std::io::stdout; 6 | use std::str; 7 | use plugin::*; 8 | use protobuf::parse_from_reader; 9 | use protobuf::Message; 10 | use protobuf::descriptor::FileDescriptorProto; 11 | 12 | 13 | pub struct GenResult { 14 | pub name: String, 15 | pub content: Vec, 16 | } 17 | 18 | pub fn plugin_main(gen: F) 19 | where F : Fn(&[FileDescriptorProto], &[String]) -> Vec 20 | { 21 | let req = parse_from_reader::(&mut stdin()).unwrap(); 22 | let result = gen(req.get_proto_file(), req.get_file_to_generate()); 23 | let mut resp = CodeGeneratorResponse::new(); 24 | resp.set_file( 25 | result 26 | .iter() 27 | .map(|file| { 28 | let mut r = CodeGeneratorResponse_File::new(); 29 | r.set_name(file.name.to_string()); 30 | r.set_content(str::from_utf8(file.content.as_ref()).unwrap().to_string()); 31 | r 32 | }) 33 | .collect(), 34 | ); 35 | resp.write_to_writer(&mut stdout()).unwrap(); 36 | } 37 | -------------------------------------------------------------------------------- /protobuf/src/error.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | use std::error::Error; 3 | use std::fmt; 4 | use std::str; 5 | 6 | use wire_format::WireType; 7 | 8 | pub type ProtobufResult = Result; 9 | 10 | /// Enum values added here for diagnostic purposes. 11 | /// Users should not depend on specific values. 12 | #[derive(Debug)] 13 | pub enum WireError { 14 | UnexpectedEof, 15 | UnexpectedWireType(WireType), 16 | IncorrectTag(u32), 17 | IncompleteMap, 18 | IncorrectVarint, 19 | Utf8Error, 20 | InvalidEnumValue(i32), 21 | OverRecursionLimit, 22 | Other, 23 | } 24 | 25 | #[derive(Debug)] 26 | pub enum ProtobufError { 27 | IoError(io::Error), 28 | WireError(WireError), 29 | Utf8(str::Utf8Error), 30 | MessageNotInitialized { message: &'static str }, 31 | } 32 | 33 | impl ProtobufError { 34 | pub fn message_not_initialized(message: &'static str) -> ProtobufError { 35 | ProtobufError::MessageNotInitialized { message: message } 36 | } 37 | } 38 | 39 | impl fmt::Display for ProtobufError { 40 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 41 | fmt::Debug::fmt(self, f) 42 | } 43 | } 44 | 45 | impl Error for ProtobufError { 46 | fn description(&self) -> &str { 47 | match self { 48 | // not sure that cause should be included in message 49 | &ProtobufError::IoError(ref e) => e.description(), 50 | &ProtobufError::WireError(ref e) => { 51 | match *e { 52 | WireError::Utf8Error => "invalid UTF-8 sequence", 53 | WireError::UnexpectedWireType(..) => "unexpected wire type", 54 | WireError::InvalidEnumValue(..) => "invalid enum value", 55 | WireError::IncorrectTag(..) => "incorrect tag", 56 | WireError::IncorrectVarint => "incorrect varint", 57 | WireError::IncompleteMap => "incomplete map", 58 | WireError::UnexpectedEof => "unexpected EOF", 59 | WireError::OverRecursionLimit => "over recursion limit", 60 | WireError::Other => "other error", 61 | } 62 | } 63 | &ProtobufError::Utf8(ref e) => &e.description(), 64 | &ProtobufError::MessageNotInitialized { .. } => "not all message fields set", 65 | } 66 | } 67 | 68 | fn cause(&self) -> Option<&Error> { 69 | match self { 70 | &ProtobufError::IoError(ref e) => Some(e), 71 | &ProtobufError::Utf8(ref e) => Some(e), 72 | &ProtobufError::WireError(..) => None, 73 | &ProtobufError::MessageNotInitialized { .. } => None, 74 | } 75 | } 76 | } 77 | 78 | impl From for ProtobufError { 79 | fn from(err: io::Error) -> Self { 80 | ProtobufError::IoError(err) 81 | } 82 | } 83 | 84 | impl From for ProtobufError { 85 | fn from(err: str::Utf8Error) -> Self { 86 | ProtobufError::Utf8(err) 87 | } 88 | } 89 | 90 | impl From for io::Error { 91 | fn from(err: ProtobufError) -> Self { 92 | match err { 93 | ProtobufError::IoError(e) => e, 94 | ProtobufError::WireError(e) => { 95 | io::Error::new(io::ErrorKind::InvalidData, ProtobufError::WireError(e)) 96 | } 97 | ProtobufError::MessageNotInitialized { message: msg } => { 98 | io::Error::new( 99 | io::ErrorKind::InvalidInput, 100 | ProtobufError::MessageNotInitialized { message: msg }, 101 | ) 102 | } 103 | e => io::Error::new(io::ErrorKind::Other, Box::new(e)), 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /protobuf/src/ext.rs: -------------------------------------------------------------------------------- 1 | use std::marker::PhantomData; 2 | 3 | use core::Message; 4 | use types::ProtobufType; 5 | 6 | /// Optional ext field 7 | pub struct ExtFieldOptional { 8 | pub field_number: u32, 9 | pub phantom: PhantomData<(M, T)>, 10 | } 11 | 12 | /// Repeated ext field 13 | pub struct ExtFieldRepeated { 14 | pub field_number: u32, 15 | pub phantom: PhantomData<(M, T)>, 16 | } 17 | 18 | impl ExtFieldOptional { 19 | pub fn get(&self, m: &M) -> Option { 20 | m.get_unknown_fields() 21 | .get(self.field_number) 22 | .and_then(T::get_from_unknown) 23 | } 24 | } 25 | 26 | impl ExtFieldRepeated { 27 | pub fn get(&self, _m: &M) -> Vec { 28 | unimplemented!() 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /protobuf/src/lazy.rs: -------------------------------------------------------------------------------- 1 | //! Lazily initialized data. 2 | //! Used in generated code. 3 | 4 | use std::mem; 5 | use std::sync; 6 | 7 | /// Lasily initialized data. 8 | // Fields are public until `const` functions available in stable. 9 | pub struct Lazy { 10 | #[doc(hidden)] 11 | pub lock: sync::Once, 12 | #[doc(hidden)] 13 | pub ptr: *const T, 14 | } 15 | 16 | impl Lazy { 17 | /// Get lazy field value, initialize it with given function if not yet. 18 | pub fn get(&'static mut self, init: F) -> &'static T 19 | where 20 | F : FnOnce() -> T, 21 | { 22 | // ~ decouple the lifetimes of 'self' and 'self.lock' such we 23 | // can initialize self.ptr in the call_once closure (note: we 24 | // do have to initialize self.ptr in the closure to guarantee 25 | // the ptr is valid for all calling threads at any point in 26 | // time) 27 | let lock: &sync::Once = unsafe { mem::transmute(&self.lock) }; 28 | lock.call_once(|| unsafe { 29 | self.ptr = mem::transmute(Box::new(init())); 30 | }); 31 | unsafe { &*self.ptr } 32 | } 33 | } 34 | 35 | /// Used to initialize `lock` field in `Lazy` struct. 36 | pub const ONCE_INIT: sync::Once = sync::ONCE_INIT; 37 | 38 | 39 | #[cfg(test)] 40 | mod test { 41 | use super::{Lazy, ONCE_INIT}; 42 | use std::thread; 43 | use std::sync::{Arc, Barrier}; 44 | use std::sync::atomic::{ATOMIC_ISIZE_INIT, AtomicIsize, Ordering}; 45 | 46 | #[test] 47 | fn many_threads_calling_get() { 48 | const N_THREADS: usize = 32; 49 | const N_ITERS_IN_THREAD: usize = 32; 50 | const N_ITERS: usize = 16; 51 | 52 | static mut LAZY: Lazy = Lazy { 53 | lock: ONCE_INIT, 54 | ptr: 0 as *const String, 55 | }; 56 | static CALL_COUNT: AtomicIsize = ATOMIC_ISIZE_INIT; 57 | 58 | let value = "Hello, world!".to_owned(); 59 | 60 | for _ in 0..N_ITERS { 61 | // Reset mutable state. 62 | unsafe { 63 | LAZY = Lazy { 64 | lock: ONCE_INIT, 65 | ptr: 0 as *const String, 66 | } 67 | } 68 | CALL_COUNT.store(0, Ordering::SeqCst); 69 | 70 | // Create a bunch of threads, all calling .get() at the same time. 71 | let mut threads = vec![]; 72 | let barrier = Arc::new(Barrier::new(N_THREADS)); 73 | 74 | for _ in 0..N_THREADS { 75 | let cloned_value_thread = value.clone(); 76 | let cloned_barrier = barrier.clone(); 77 | threads.push(thread::spawn(move || { 78 | // Ensure all threads start at once to maximise contention. 79 | cloned_barrier.wait(); 80 | for _ in 0..N_ITERS_IN_THREAD { 81 | assert_eq!(&cloned_value_thread, unsafe { 82 | LAZY.get(|| { 83 | CALL_COUNT.fetch_add(1, Ordering::SeqCst); 84 | cloned_value_thread.clone() 85 | }) 86 | }); 87 | } 88 | })); 89 | } 90 | 91 | for thread in threads { 92 | thread.join().unwrap(); 93 | } 94 | 95 | assert_eq!(CALL_COUNT.load(Ordering::SeqCst), 1); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /protobuf/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Library to read and write protocol buffers data. 2 | 3 | // TODO: add docs 4 | //#![deny(missing_docs)] 5 | 6 | #[cfg(feature = "bytes")] 7 | extern crate bytes; 8 | 9 | pub use unknown::UnknownFields; 10 | pub use unknown::UnknownFieldsIter; 11 | pub use unknown::UnknownValue; 12 | pub use unknown::UnknownValueRef; 13 | pub use unknown::UnknownValues; 14 | pub use unknown::UnknownValuesIter; 15 | pub use repeated::RepeatedField; 16 | pub use singular::SingularField; 17 | pub use singular::SingularPtrField; 18 | pub use clear::Clear; 19 | pub use core::Message; 20 | pub use core::ProtobufEnum; 21 | pub use core::parse_from_bytes; 22 | pub use core::parse_from_reader; 23 | #[cfg(feature = "bytes")] 24 | pub use core::parse_from_carllerche_bytes; 25 | pub use core::parse_length_delimited_from; 26 | pub use core::parse_length_delimited_from_reader; 27 | pub use core::parse_length_delimited_from_bytes; 28 | pub use stream::CodedInputStream; 29 | pub use stream::CodedOutputStream; 30 | pub use stream::wire_format; 31 | pub use error::ProtobufResult; 32 | pub use error::ProtobufError; 33 | pub use cached_size::CachedSize; 34 | #[cfg(feature = "bytes")] 35 | pub use chars::Chars; 36 | 37 | // generated 38 | pub mod descriptor; 39 | pub mod plugin; 40 | pub mod rustproto; 41 | 42 | mod core; 43 | pub mod rt; 44 | pub mod lazy; 45 | pub mod compiler_plugin; 46 | mod repeated; 47 | mod singular; 48 | mod clear; 49 | pub mod reflect; 50 | pub mod text_format; 51 | pub mod stream; 52 | pub mod error; 53 | pub mod types; 54 | pub mod well_known_types; 55 | pub mod ext; 56 | 57 | // used by test 58 | #[cfg(test)] 59 | #[path = "../../protobuf-test-common/src/hex.rs"] 60 | mod hex; 61 | 62 | // used by rust-grpc 63 | pub mod descriptorx; 64 | 65 | mod zigzag; 66 | mod paginate; 67 | mod unknown; 68 | mod strx; 69 | #[doc(hidden)] // used by codegen 70 | pub mod rust; 71 | mod cached_size; 72 | mod varint; 73 | #[cfg(feature = "bytes")] 74 | mod chars; 75 | 76 | mod misc; 77 | 78 | mod buf_read_iter; 79 | 80 | 81 | // so `use protobuf::*` could work in mod descriptor and well_known_types 82 | mod protobuf { 83 | pub use descriptor; 84 | pub use descriptorx; 85 | pub use reflect; 86 | pub use core::*; 87 | pub use error::*; 88 | pub use stream::*; 89 | pub use rt; 90 | pub use text_format; 91 | pub use types; 92 | pub use lazy; 93 | pub use well_known_types; 94 | pub use ext; 95 | pub use unknown::UnknownFields; 96 | pub use unknown::UnknownFieldsIter; 97 | pub use unknown::UnknownValue; 98 | pub use unknown::UnknownValueRef; 99 | pub use unknown::UnknownValues; 100 | pub use unknown::UnknownValuesIter; 101 | pub use repeated::RepeatedField; 102 | pub use singular::SingularField; 103 | pub use singular::SingularPtrField; 104 | pub use clear::Clear; 105 | pub use cached_size::CachedSize; 106 | } 107 | -------------------------------------------------------------------------------- /protobuf/src/misc.rs: -------------------------------------------------------------------------------- 1 | use std::slice; 2 | use std::mem; 3 | 4 | /// Slice from `vec[vec.len()..vec.capacity()]` 5 | pub unsafe fn remaining_capacity_as_slice_mut(vec: &mut Vec) -> &mut [A] { 6 | slice::from_raw_parts_mut( 7 | vec.as_mut_slice().as_mut_ptr().offset(vec.len() as isize), 8 | vec.capacity() - vec.len(), 9 | ) 10 | } 11 | 12 | pub unsafe fn remove_lifetime_mut(a: &mut A) -> &'static mut A { 13 | mem::transmute(a) 14 | } 15 | 16 | #[cfg(test)] 17 | mod test { 18 | use super::*; 19 | 20 | #[test] 21 | fn test_remaining_capacity_as_slice_mut() { 22 | let mut v = Vec::with_capacity(5); 23 | v.push(10); 24 | v.push(11); 25 | v.push(12); 26 | unsafe { 27 | { 28 | let s = remaining_capacity_as_slice_mut(&mut v); 29 | assert_eq!(2, s.len()); 30 | s[0] = 13; 31 | s[1] = 14; 32 | } 33 | v.set_len(5); 34 | } 35 | assert_eq!(vec![10, 11, 12, 13, 14], v); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /protobuf/src/paginate.rs: -------------------------------------------------------------------------------- 1 | pub trait PaginatableIterator: Sized { 2 | fn paginate(self, page: usize) -> Paginate; 3 | } 4 | 5 | impl> PaginatableIterator for U { 6 | fn paginate(self, page: usize) -> Paginate { 7 | Paginate { 8 | iter: self, 9 | page: page, 10 | } 11 | } 12 | } 13 | 14 | pub struct Paginate { 15 | iter: I, 16 | page: usize, 17 | } 18 | 19 | impl> Iterator for Paginate { 20 | type Item = Vec; 21 | 22 | fn next(&mut self) -> Option> { 23 | let mut r = Vec::new(); 24 | for _ in 0..self.page { 25 | match self.iter.next() { 26 | Some(next) => r.push(next), 27 | None if r.is_empty() => return None, 28 | None => return Some(r), 29 | } 30 | } 31 | Some(r) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /protobuf/src/reflect/map.rs: -------------------------------------------------------------------------------- 1 | use std::hash::Hash; 2 | use std::collections::HashMap; 3 | use std::collections::hash_map; 4 | 5 | use super::value::ProtobufValue; 6 | 7 | 8 | /// Implemented for `HashMap` with appropriate keys and values 9 | pub trait ReflectMap: 'static { 10 | fn reflect_iter(&self) -> ReflectMapIter; 11 | 12 | fn len(&self) -> usize; 13 | } 14 | 15 | impl ReflectMap 16 | for HashMap { 17 | fn reflect_iter<'a>(&'a self) -> ReflectMapIter<'a> { 18 | ReflectMapIter { imp: Box::new(ReflectMapIterImpl::<'a, K, V> { iter: self.iter() }) } 19 | } 20 | 21 | fn len(&self) -> usize { 22 | HashMap::len(self) 23 | } 24 | } 25 | 26 | 27 | trait ReflectMapIterTrait<'a> { 28 | fn next(&mut self) -> Option<(&'a ProtobufValue, &'a ProtobufValue)>; 29 | } 30 | 31 | struct ReflectMapIterImpl<'a, K : Eq + Hash + 'static, V : 'static> { 32 | iter: hash_map::Iter<'a, K, V>, 33 | } 34 | 35 | impl< 36 | 'a, 37 | K : ProtobufValue + Eq + Hash + 'static, 38 | V : ProtobufValue + 'static, 39 | > ReflectMapIterTrait<'a> for ReflectMapIterImpl<'a, K, V> { 40 | fn next(&mut self) -> Option<(&'a ProtobufValue, &'a ProtobufValue)> { 41 | match self.iter.next() { 42 | Some((k, v)) => Some((k as &ProtobufValue, v as &ProtobufValue)), 43 | None => None, 44 | } 45 | } 46 | } 47 | 48 | pub struct ReflectMapIter<'a> { 49 | imp: Box + 'a>, 50 | } 51 | 52 | impl<'a> Iterator for ReflectMapIter<'a> { 53 | type Item = (&'a ProtobufValue, &'a ProtobufValue); 54 | 55 | fn next(&mut self) -> Option<(&'a ProtobufValue, &'a ProtobufValue)> { 56 | self.imp.next() 57 | } 58 | } 59 | 60 | impl<'a> IntoIterator for &'a ReflectMap { 61 | type IntoIter = ReflectMapIter<'a>; 62 | type Item = (&'a ProtobufValue, &'a ProtobufValue); 63 | 64 | fn into_iter(self) -> Self::IntoIter { 65 | self.reflect_iter() 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /protobuf/src/reflect/optional.rs: -------------------------------------------------------------------------------- 1 | use std::mem; 2 | 3 | use super::value::ProtobufValue; 4 | 5 | use singular::*; 6 | 7 | pub trait ReflectOptional: 'static { 8 | fn to_option(&self) -> Option<&ProtobufValue>; 9 | 10 | fn set_value(&mut self, value: &ProtobufValue); 11 | } 12 | 13 | impl ReflectOptional for Option { 14 | fn to_option(&self) -> Option<&ProtobufValue> { 15 | self.as_ref().map(|v| v as &ProtobufValue) 16 | } 17 | 18 | fn set_value(&mut self, value: &ProtobufValue) { 19 | match value.as_any().downcast_ref::() { 20 | Some(v) => mem::replace(self, Some(v.clone())), 21 | None => panic!(), 22 | }; 23 | } 24 | } 25 | 26 | impl ReflectOptional for SingularField { 27 | fn to_option(&self) -> Option<&ProtobufValue> { 28 | self.as_ref().map(|v| v as &ProtobufValue) 29 | } 30 | 31 | fn set_value(&mut self, value: &ProtobufValue) { 32 | match value.as_any().downcast_ref::() { 33 | Some(v) => mem::replace(self, SingularField::some(v.clone())), 34 | None => panic!(), 35 | }; 36 | } 37 | } 38 | 39 | impl ReflectOptional for SingularPtrField { 40 | fn to_option(&self) -> Option<&ProtobufValue> { 41 | self.as_ref().map(|v| v as &ProtobufValue) 42 | } 43 | 44 | fn set_value(&mut self, value: &ProtobufValue) { 45 | match value.as_any().downcast_ref::() { 46 | Some(v) => mem::replace(self, SingularPtrField::some(v.clone())), 47 | None => panic!(), 48 | }; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /protobuf/src/reflect/value.rs: -------------------------------------------------------------------------------- 1 | use std::any::Any; 2 | 3 | #[cfg(feature = "bytes")] 4 | use bytes::Bytes; 5 | #[cfg(feature = "bytes")] 6 | use chars::Chars; 7 | 8 | use core::*; 9 | use super::*; 10 | 11 | pub trait ProtobufValue: Any + 'static { 12 | fn as_ref(&self) -> ProtobufValueRef; 13 | 14 | fn as_any(&self) -> &Any { 15 | unimplemented!() 16 | } 17 | 18 | fn is_non_zero(&self) -> bool { 19 | self.as_ref().is_non_zero() 20 | } 21 | 22 | fn as_ref_copy(&self) -> ProtobufValueRef<'static> 23 | //where Self : Copy // TODO 24 | { 25 | match self.as_ref() { 26 | ProtobufValueRef::Bool(v) => ProtobufValueRef::Bool(v), 27 | ProtobufValueRef::U32(v) => ProtobufValueRef::U32(v), 28 | ProtobufValueRef::U64(v) => ProtobufValueRef::U64(v), 29 | ProtobufValueRef::I32(v) => ProtobufValueRef::I32(v), 30 | ProtobufValueRef::I64(v) => ProtobufValueRef::I64(v), 31 | ProtobufValueRef::F32(v) => ProtobufValueRef::F32(v), 32 | ProtobufValueRef::F64(v) => ProtobufValueRef::F64(v), 33 | ProtobufValueRef::Enum(v) => ProtobufValueRef::Enum(v), 34 | ProtobufValueRef::String(..) | 35 | ProtobufValueRef::Bytes(..) | 36 | ProtobufValueRef::Message(..) => unreachable!(), 37 | } 38 | } 39 | } 40 | 41 | impl ProtobufValue for u32 { 42 | fn as_ref(&self) -> ProtobufValueRef { 43 | ProtobufValueRef::U32(*self) 44 | } 45 | } 46 | 47 | impl ProtobufValue for u64 { 48 | fn as_ref(&self) -> ProtobufValueRef { 49 | ProtobufValueRef::U64(*self) 50 | } 51 | } 52 | 53 | impl ProtobufValue for i32 { 54 | fn as_ref(&self) -> ProtobufValueRef { 55 | ProtobufValueRef::I32(*self) 56 | } 57 | } 58 | 59 | impl ProtobufValue for i64 { 60 | fn as_ref(&self) -> ProtobufValueRef { 61 | ProtobufValueRef::I64(*self) 62 | } 63 | } 64 | 65 | impl ProtobufValue for f32 { 66 | fn as_ref(&self) -> ProtobufValueRef { 67 | ProtobufValueRef::F32(*self) 68 | } 69 | } 70 | 71 | impl ProtobufValue for f64 { 72 | fn as_ref(&self) -> ProtobufValueRef { 73 | ProtobufValueRef::F64(*self) 74 | } 75 | } 76 | 77 | impl ProtobufValue for bool { 78 | fn as_ref(&self) -> ProtobufValueRef { 79 | ProtobufValueRef::Bool(*self) 80 | } 81 | } 82 | 83 | impl ProtobufValue for String { 84 | fn as_ref(&self) -> ProtobufValueRef { 85 | ProtobufValueRef::String(*&self) 86 | } 87 | } 88 | 89 | impl ProtobufValue for str { 90 | fn as_ref(&self) -> ProtobufValueRef { 91 | ProtobufValueRef::String(self) 92 | } 93 | } 94 | 95 | impl ProtobufValue for Vec { 96 | fn as_ref(&self) -> ProtobufValueRef { 97 | ProtobufValueRef::Bytes(*&self) 98 | } 99 | } 100 | 101 | #[cfg(feature = "bytes")] 102 | impl ProtobufValue for Bytes { 103 | fn as_ref(&self) -> ProtobufValueRef { 104 | ProtobufValueRef::Bytes(&*self) 105 | } 106 | } 107 | 108 | #[cfg(feature = "bytes")] 109 | impl ProtobufValue for Chars { 110 | fn as_ref(&self) -> ProtobufValueRef { 111 | ProtobufValueRef::String(&*self) 112 | } 113 | } 114 | 115 | // conflicting implementations, so generated code is used instead 116 | /* 117 | impl ProtobufValue for E { 118 | fn as_ref(&self) -> ProtobufValueRef { 119 | ProtobufValueRef::Enum(self.descriptor()) 120 | } 121 | } 122 | 123 | impl ProtobufValue for M { 124 | fn as_ref(&self) -> ProtobufValueRef { 125 | ProtobufValueRef::Message(self) 126 | } 127 | } 128 | */ 129 | 130 | 131 | pub enum ProtobufValueRef<'a> { 132 | U32(u32), 133 | U64(u64), 134 | I32(i32), 135 | I64(i64), 136 | F32(f32), 137 | F64(f64), 138 | Bool(bool), 139 | String(&'a str), 140 | Bytes(&'a [u8]), 141 | Enum(&'static EnumValueDescriptor), 142 | Message(&'a Message), 143 | } 144 | 145 | impl<'a> ProtobufValueRef<'a> { 146 | pub fn is_non_zero(&self) -> bool { 147 | match *self { 148 | ProtobufValueRef::U32(v) => v != 0, 149 | ProtobufValueRef::U64(v) => v != 0, 150 | ProtobufValueRef::I32(v) => v != 0, 151 | ProtobufValueRef::I64(v) => v != 0, 152 | ProtobufValueRef::F32(v) => v != 0., 153 | ProtobufValueRef::F64(v) => v != 0., 154 | ProtobufValueRef::Bool(v) => v, 155 | ProtobufValueRef::String(v) => !v.is_empty(), 156 | ProtobufValueRef::Bytes(v) => !v.is_empty(), 157 | ProtobufValueRef::Enum(v) => v.value() != 0, 158 | ProtobufValueRef::Message(_) => true, 159 | } 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /protobuf/src/rust.rs: -------------------------------------------------------------------------------- 1 | static RUST_KEYWORDS: &'static [&'static str] = &[ 2 | "as", 3 | "break", 4 | "crate", 5 | "else", 6 | "enum", 7 | "extern", 8 | "false", 9 | "fn", 10 | "for", 11 | "if", 12 | "impl", 13 | "in", 14 | "let", 15 | "loop", 16 | "match", 17 | "mod", 18 | "move", 19 | "mut", 20 | "pub", 21 | "ref", 22 | "return", 23 | "static", 24 | "self", 25 | "Self", 26 | "struct", 27 | "super", 28 | "true", 29 | "trait", 30 | "type", 31 | "unsafe", 32 | "use", 33 | "while", 34 | "continue", 35 | "box", 36 | "const", 37 | "where", 38 | "virtual", 39 | "proc", 40 | "alignof", 41 | "become", 42 | "offsetof", 43 | "priv", 44 | "pure", 45 | "sizeof", 46 | "typeof", 47 | "unsized", 48 | "yield", 49 | "do", 50 | "abstract", 51 | "final", 52 | "override", 53 | "macro", 54 | ]; 55 | 56 | pub fn is_rust_keyword(ident: &str) -> bool { 57 | RUST_KEYWORDS.contains(&ident) 58 | } 59 | 60 | fn hex_digit(value: u32) -> char { 61 | if value < 10 { 62 | (b'0' + value as u8) as char 63 | } else if value < 0x10 { 64 | (b'a' + value as u8 - 10) as char 65 | } else { 66 | unreachable!() 67 | } 68 | } 69 | 70 | pub fn quote_escape_str(s: &str) -> String { 71 | let mut buf = String::new(); 72 | buf.push('"'); 73 | buf.extend(s.chars().flat_map(|c| c.escape_default())); 74 | buf.push('"'); 75 | buf 76 | } 77 | 78 | pub fn quote_escape_bytes(bytes: &[u8]) -> String { 79 | let mut buf = String::new(); 80 | buf.push('b'); 81 | buf.push('"'); 82 | for &b in bytes { 83 | match b { 84 | b'\n' => buf.push_str(r"\n"), 85 | b'\r' => buf.push_str(r"\r"), 86 | b'\t' => buf.push_str(r"\t"), 87 | b'"' => buf.push_str("\\\""), 88 | b'\\' => buf.push_str(r"\\"), 89 | b'\x20'...b'\x7e' => buf.push(b as char), 90 | _ => { 91 | buf.push_str(r"\x"); 92 | buf.push(hex_digit((b as u32) >> 4)); 93 | buf.push(hex_digit((b as u32) & 0x0f)); 94 | } 95 | } 96 | } 97 | buf.push('"'); 98 | buf 99 | } 100 | 101 | #[cfg(test)] 102 | mod test { 103 | 104 | use super::*; 105 | 106 | #[test] 107 | fn test_quote_escape_bytes() { 108 | assert_eq!("b\"\"", quote_escape_bytes(b"")); 109 | assert_eq!("b\"xyZW\"", quote_escape_bytes(b"xyZW")); 110 | assert_eq!("b\"aa\\\"bb\"", quote_escape_bytes(b"aa\"bb")); 111 | assert_eq!("b\"aa\\r\\n\\tbb\"", quote_escape_bytes(b"aa\r\n\tbb")); 112 | assert_eq!( 113 | "b\"\\x00\\x01\\x12\\xfe\\xff\"", 114 | quote_escape_bytes(b"\x00\x01\x12\xfe\xff") 115 | ); 116 | } 117 | 118 | } 119 | -------------------------------------------------------------------------------- /protobuf/src/strx.rs: -------------------------------------------------------------------------------- 1 | pub fn remove_to<'s>(s: &'s str, c: char) -> &'s str { 2 | match s.rfind(c) { 3 | Some(pos) => &s[(pos + 1)..], 4 | None => s, 5 | } 6 | } 7 | 8 | pub fn remove_suffix<'s>(s: &'s str, suffix: &str) -> &'s str { 9 | if !s.ends_with(suffix) { 10 | s 11 | } else { 12 | &s[..(s.len() - suffix.len())] 13 | } 14 | } 15 | 16 | #[cfg(test)] 17 | mod test { 18 | 19 | use super::remove_to; 20 | use super::remove_suffix; 21 | 22 | #[test] 23 | fn test_remove_to() { 24 | assert_eq!("aaa", remove_to("aaa", '.')); 25 | assert_eq!("bbb", remove_to("aaa.bbb", '.')); 26 | assert_eq!("ccc", remove_to("aaa.bbb.ccc", '.')); 27 | } 28 | 29 | #[test] 30 | fn test_remove_suffix() { 31 | assert_eq!("bbb", remove_suffix("bbbaaa", "aaa")); 32 | assert_eq!("aaa", remove_suffix("aaa", "bbb")); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /protobuf/src/varint.rs: -------------------------------------------------------------------------------- 1 | /// Encode u64 as varint. 2 | /// Panics if buffer length is less than 10. 3 | #[inline] 4 | pub fn encode_varint64(mut value: u64, buf: &mut [u8]) -> usize { 5 | assert!(buf.len() >= 10); 6 | 7 | unsafe { 8 | let mut i = 0; 9 | while (value & !0x7F) > 0 { 10 | *buf.get_unchecked_mut(i) = ((value & 0x7F) | 0x80) as u8; 11 | value >>= 7; 12 | i += 1; 13 | } 14 | *buf.get_unchecked_mut(i) = value as u8; 15 | i + 1 16 | } 17 | } 18 | 19 | /// Encode u32 value as varint. 20 | /// Panics if buffer length is less than 5. 21 | #[inline] 22 | pub fn encode_varint32(mut value: u32, buf: &mut [u8]) -> usize { 23 | assert!(buf.len() >= 5); 24 | 25 | unsafe { 26 | let mut i = 0; 27 | while (value & !0x7F) > 0 { 28 | *buf.get_unchecked_mut(i) = ((value & 0x7F) | 0x80) as u8; 29 | value >>= 7; 30 | i += 1; 31 | } 32 | *buf.get_unchecked_mut(i) = value as u8; 33 | i + 1 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /protobuf/src/well_known_types/mod.rs: -------------------------------------------------------------------------------- 1 | // This file is generated. Do not edit 2 | 3 | mod any; 4 | mod api; 5 | mod duration; 6 | mod empty; 7 | mod field_mask; 8 | mod source_context; 9 | mod struct_pb; 10 | mod timestamp; 11 | mod type_pb; 12 | mod wrappers; 13 | 14 | pub use self::any::*; 15 | pub use self::api::*; 16 | pub use self::duration::*; 17 | pub use self::empty::*; 18 | pub use self::field_mask::*; 19 | pub use self::source_context::*; 20 | pub use self::struct_pb::*; 21 | pub use self::timestamp::*; 22 | pub use self::type_pb::*; 23 | pub use self::wrappers::*; 24 | -------------------------------------------------------------------------------- /protobuf/src/zigzag.rs: -------------------------------------------------------------------------------- 1 | // ZigZag endoging used for efficient transfer of signed integers 2 | // https://developers.google.com/protocol-buffers/docs/encoding#types 3 | 4 | pub fn decode_zig_zag_32(n: u32) -> i32 { 5 | ((n >> 1) as i32) ^ (-((n & 1) as i32)) 6 | } 7 | 8 | pub fn decode_zig_zag_64(n: u64) -> i64 { 9 | ((n >> 1) as i64) ^ (-((n & 1) as i64)) 10 | } 11 | 12 | pub fn encode_zig_zag_32(n: i32) -> u32 { 13 | ((n << 1) ^ (n >> 31)) as u32 14 | } 15 | 16 | pub fn encode_zig_zag_64(n: i64) -> u64 { 17 | ((n << 1) ^ (n >> 63)) as u64 18 | } 19 | 20 | #[cfg(test)] 21 | mod test { 22 | 23 | use super::decode_zig_zag_32; 24 | use super::decode_zig_zag_64; 25 | use super::encode_zig_zag_32; 26 | use super::encode_zig_zag_64; 27 | 28 | #[test] 29 | fn test_zig_zag() { 30 | fn test_zig_zag_pair_64(decoded: i64, encoded: u64) { 31 | assert_eq!(decoded, decode_zig_zag_64(encoded)); 32 | assert_eq!(encoded, encode_zig_zag_64(decoded)); 33 | } 34 | 35 | fn test_zig_zag_pair(decoded: i32, encoded: u32) { 36 | assert_eq!(decoded, decode_zig_zag_32(encoded)); 37 | assert_eq!(encoded, encode_zig_zag_32(decoded)); 38 | test_zig_zag_pair_64(decoded as i64, encoded as u64); 39 | } 40 | 41 | test_zig_zag_pair(0, 0); 42 | test_zig_zag_pair(-1, 1); 43 | test_zig_zag_pair(1, 2); 44 | test_zig_zag_pair(-2, 3); 45 | test_zig_zag_pair(2147483647, 4294967294); 46 | test_zig_zag_pair(-2147483648, 4294967295); 47 | test_zig_zag_pair_64(9223372036854775807, 18446744073709551614); 48 | test_zig_zag_pair_64(-9223372036854775808, 18446744073709551615); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /protoc-rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "protoc-rust" 3 | version = "1.6.0" 4 | authors = ["Stepan Koltsov "] 5 | license = "MIT/Apache-2.0" 6 | homepage = "https://github.com/stepancheg/rust-protobuf/tree/master/protoc-rust/" 7 | repository = "https://github.com/stepancheg/rust-protobuf/tree/master/protoc-rust/" 8 | description = """ 9 | protoc --rust_out=... available as API. protoc needs to be in $PATH, protoc-gen-run does not. 10 | """ 11 | 12 | [lib] 13 | doctest = false 14 | 15 | [dependencies] 16 | protoc = { path = "../protoc", version = "1.6.0" } 17 | protobuf = { path = "../protobuf", version = "1.6.0" } 18 | protobuf-codegen = { path = "../protobuf-codegen", version = "1.6.0" } 19 | tempdir = "0.3" 20 | -------------------------------------------------------------------------------- /protoc-rust/README.md: -------------------------------------------------------------------------------- 1 | # API to generate .rs files 2 | 3 | API to generate `.rs` files to be used e. g. [from build.rs](https://github.com/stepancheg/rust-protobuf/blob/master/protobuf-test/build.rs). 4 | 5 | Example code: 6 | 7 | ``` 8 | extern crate protoc_rust; 9 | 10 | protoc_rust::run(protoc_rust::Args { 11 | out_dir: "src/protos", 12 | input: &["protos/a.proto", "protos/b.proto"], 13 | includes: &["protos"], 14 | customize: Customize { 15 | ..Default::default() 16 | }, 17 | }).expect("protoc"); 18 | ``` 19 | 20 | And in `Cargo.toml`: 21 | 22 | ``` 23 | [build-dependencies] 24 | protoc-rust = "1.5" 25 | ``` 26 | 27 | Note this API requires `protoc` command present in `$PATH`. 28 | Although `protoc-gen-rust` command is not needed. 29 | 30 | The alternative is to use 31 | [pure-rust .proto parser and code generator](https://github.com/stepancheg/rust-protobuf/tree/master/protobuf-codegen-pure). 32 | -------------------------------------------------------------------------------- /protoc-rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate tempdir; 2 | 3 | extern crate protoc; 4 | extern crate protobuf; 5 | extern crate protobuf_codegen; 6 | 7 | use std::io; 8 | use std::io::Read; 9 | use std::fs; 10 | use std::path::Path; 11 | 12 | pub use protoc::Error; 13 | pub use protoc::Result; 14 | 15 | pub use protobuf_codegen::Customize; 16 | 17 | 18 | #[derive(Debug, Default)] 19 | pub struct Args<'a> { 20 | /// --lang_out= param 21 | pub out_dir: &'a str, 22 | /// -I args 23 | pub includes: &'a [&'a str], 24 | /// List of .proto files to compile 25 | pub input: &'a [&'a str], 26 | /// Customize code generation 27 | pub customize: Customize, 28 | } 29 | 30 | /// Like `protoc --rust_out=...` but without requiring `protoc-gen-rust` command in `$PATH`. 31 | pub fn run(args: Args) -> Result<()> { 32 | let protoc = protoc::Protoc::from_env_path(); 33 | protoc.check()?; 34 | 35 | let temp_dir = tempdir::TempDir::new("protoc-rust")?; 36 | let temp_file = temp_dir.path().join("descriptor.pbbin"); 37 | let temp_file = temp_file.to_str().expect("utf-8 file name"); 38 | 39 | protoc.write_descriptor_set(protoc::DescriptorSetOutArgs { 40 | out: temp_file, 41 | includes: args.includes, 42 | input: args.input, 43 | include_imports: true, 44 | })?; 45 | 46 | let mut fds = Vec::new(); 47 | let mut file = fs::File::open(temp_file)?; 48 | file.read_to_end(&mut fds)?; 49 | 50 | drop(file); 51 | drop(temp_dir); 52 | 53 | let fds: protobuf::descriptor::FileDescriptorSet = protobuf::parse_from_bytes(&fds) 54 | .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; 55 | 56 | let mut includes = args.includes; 57 | if includes.is_empty() { 58 | static DOT_SLICE: &'static [&'static str] = &["."]; 59 | includes = DOT_SLICE; 60 | } 61 | 62 | let mut files_to_generate = Vec::new(); 63 | 'outer: for file in args.input { 64 | for include in includes { 65 | if let Some(truncated) = remove_path_prefix(file, include) { 66 | files_to_generate.push(truncated.to_owned()); 67 | continue 'outer; 68 | } 69 | } 70 | 71 | return Err(Error::new( 72 | io::ErrorKind::Other, 73 | format!( 74 | "file {:?} is not found in includes {:?}", 75 | file, 76 | args.includes 77 | ), 78 | )); 79 | } 80 | 81 | protobuf_codegen::gen_and_write( 82 | fds.get_file(), 83 | &files_to_generate, 84 | &Path::new(&args.out_dir), 85 | &args.customize) 86 | } 87 | 88 | fn remove_dot_slash(path: &str) -> &str { 89 | if path == "." { 90 | "" 91 | } else if path.starts_with("./") || path.starts_with(".\\") { 92 | &path[2..] 93 | } else { 94 | path 95 | } 96 | } 97 | 98 | fn remove_path_prefix<'a>(mut path: &'a str, mut prefix: &str) -> Option<&'a str> { 99 | path = remove_dot_slash(path); 100 | prefix = remove_dot_slash(prefix); 101 | 102 | if prefix == "" { 103 | return Some(path); 104 | } 105 | 106 | if prefix.ends_with("/") || prefix.ends_with("\\") { 107 | prefix = &prefix[..prefix.len() - 1]; 108 | } 109 | 110 | if !path.starts_with(prefix) { 111 | return None; 112 | } 113 | 114 | if path.len() <= prefix.len() { 115 | return None; 116 | } 117 | 118 | if path.as_bytes()[prefix.len()] == b'/' || path.as_bytes()[prefix.len()] == b'\\' { 119 | return Some(&path[prefix.len() + 1..]); 120 | } else { 121 | return None; 122 | } 123 | } 124 | 125 | #[cfg(test)] 126 | mod test { 127 | #[test] 128 | fn remove_path_prefix() { 129 | assert_eq!( 130 | Some("abc.proto"), 131 | super::remove_path_prefix("xxx/abc.proto", "xxx") 132 | ); 133 | assert_eq!( 134 | Some("abc.proto"), 135 | super::remove_path_prefix("xxx/abc.proto", "xxx/") 136 | ); 137 | assert_eq!( 138 | Some("abc.proto"), 139 | super::remove_path_prefix("../xxx/abc.proto", "../xxx/") 140 | ); 141 | assert_eq!( 142 | Some("abc.proto"), 143 | super::remove_path_prefix("abc.proto", ".") 144 | ); 145 | assert_eq!( 146 | Some("abc.proto"), 147 | super::remove_path_prefix("abc.proto", "./") 148 | ); 149 | assert_eq!(None, super::remove_path_prefix("xxx/abc.proto", "yyy")); 150 | assert_eq!(None, super::remove_path_prefix("xxx/abc.proto", "yyy/")); 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /protoc/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "protoc" 3 | version = "1.6.0" 4 | authors = ["Stepan Koltsov "] 5 | license = "MIT/Apache-2.0" 6 | homepage = "https://github.com/stepancheg/rust-protobuf/tree/master/protoc/" 7 | repository = "https://github.com/stepancheg/rust-protobuf/tree/master/protoc/" 8 | description = """ 9 | Protobuf protoc command as API 10 | """ 11 | 12 | [lib] 13 | doctest = false 14 | 15 | [dependencies] 16 | log = "0.*" 17 | -------------------------------------------------------------------------------- /protoc/README.md: -------------------------------------------------------------------------------- 1 | # Protoc command launcher 2 | 3 | API to invoke `protoc` command from API (e. g. from `build.rs`), any 4 | 5 | Note, `protoc` command must be in `$PATH` along with `protoc-gen-LANG` command. 6 | 7 | Example of using `protoc` crate is in perftest's 8 | [build.rs](https://github.com/stepancheg/rust-protobuf/blob/master/perftest/build.rs). 9 | 10 | Note that to generate `rust` code from `.proto`, 11 | [protoc-rust](https://github.com/stepancheg/rust-protobuf/tree/master/protoc-rust) crate can be used, 12 | which does not require `protoc-gen-rust` present in `$PATH`. 13 | -------------------------------------------------------------------------------- /protoc/test-protoc/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "test-protoc" 3 | version = "0.0.0" 4 | authors = ["Stepan Koltsov "] 5 | 6 | [lib] 7 | test = false 8 | doctest = false 9 | 10 | [dependencies] 11 | protobuf = { path = "../../protobuf" } 12 | 13 | [features] 14 | default-features = [] 15 | # Feature to avoid recompilation of protobuf 16 | with-bytes = ["protobuf/with-bytes"] 17 | 18 | [build-dependencies] 19 | protoc = { path = ".." } 20 | -------------------------------------------------------------------------------- /protoc/test-protoc/build.rs: -------------------------------------------------------------------------------- 1 | extern crate protoc; 2 | 3 | fn main() { 4 | protoc::run(protoc::Args { 5 | lang: "rust", 6 | out_dir: "src", 7 | plugin: Some("../../target/debug/protoc-gen-rust"), 8 | input: &["src/data.proto"], 9 | ..Default::default() 10 | }).expect("protoc"); 11 | } 12 | -------------------------------------------------------------------------------- /protoc/test-protoc/src/.gitignore: -------------------------------------------------------------------------------- 1 | data.rs 2 | -------------------------------------------------------------------------------- /protoc/test-protoc/src/data.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | message FooBar { 4 | } 5 | -------------------------------------------------------------------------------- /protoc/test-protoc/src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate protobuf; 2 | 3 | mod data; 4 | 5 | pub use data::FooBar; 6 | -------------------------------------------------------------------------------- /protoc/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | cd $(dirname $0) 4 | 5 | ( 6 | echo "building protoc-gen-rust" 7 | cd ../protobuf-codegen 8 | cargo build --bin=protoc-gen-rust 9 | ) 10 | 11 | echo "cargo check in test-protoc" 12 | cd test-protoc 13 | exec cargo check --features=$RUST_PROTOBUF_FEATURES 14 | 15 | # vim: set ts=4 sw=4 et: 16 | --------------------------------------------------------------------------------