Skip to content

Commit

Permalink
Trying to improve doctests with cfg doctest
Browse files Browse the repository at this point in the history
Unfortunately, `#[cfg(doctest)]` does not seem to work:
rust-lang/rust#67295
  • Loading branch information
stepancheg committed Mar 25, 2020
1 parent 401730e commit 8c933da
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 2 deletions.
6 changes: 6 additions & 0 deletions proto/doctest_pb.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Messages used in doctests

syntax = "proto3";

message MyMessage {
}
10 changes: 8 additions & 2 deletions protobuf/regenerate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ protoc \
-I../proto \
../proto/google/protobuf/*.proto \
../proto/google/protobuf/compiler/* \
../proto/rustproto.proto
../proto/rustproto.proto \
../proto/doctest_pb.proto \

mv tmp-generated/descriptor.rs tmp-generated/plugin.rs tmp-generated/rustproto.rs src/
mv \
tmp-generated/descriptor.rs \
tmp-generated/plugin.rs \
tmp-generated/rustproto.rs \
tmp-generated/doctest_pb.rs \
src/
mv tmp-generated/*.rs src/well_known_types/
(
cd src/well_known_types
Expand Down
147 changes: 147 additions & 0 deletions protobuf/src/doctest_pb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// This file is generated by rust-protobuf 3.0.0-pre. Do not edit
// .proto file is parsed by protoc --rust-out=...
// @generated

// https://github.com/rust-lang/rust-clippy/issues/702
#![allow(unknown_lints)]
#![allow(clippy::all)]

#![cfg_attr(rustfmt, rustfmt_skip)]

#![allow(box_pointers)]
#![allow(dead_code)]
#![allow(missing_docs)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(trivial_casts)]
#![allow(unused_imports)]
#![allow(unused_results)]

//! Generated file from `doctest_pb.proto`

#[derive(PartialEq,Clone,Default)]
#[cfg_attr(serde, derive(Serialize, Deserialize))]
pub struct MyMessage {
// special fields
#[cfg_attr(serde, serde(skip))]
pub unknown_fields: crate::UnknownFields,
#[cfg_attr(serde, serde(skip))]
pub cached_size: crate::rt::CachedSize,
}

impl<'a> ::std::default::Default for &'a MyMessage {
fn default() -> &'a MyMessage {
<MyMessage as crate::Message>::default_instance()
}
}

impl MyMessage {
pub fn new() -> MyMessage {
::std::default::Default::default()
}
}

impl crate::Message for MyMessage {
fn is_initialized(&self) -> bool {
true
}

fn merge_from(&mut self, is: &mut crate::CodedInputStream<'_>) -> crate::ProtobufResult<()> {
while !is.eof()? {
let (field_number, wire_type) = is.read_tag_unpack()?;
match field_number {
_ => {
crate::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
},
};
}
::std::result::Result::Ok(())
}

// Compute sizes of nested messages
#[allow(unused_variables)]
fn compute_size(&self) -> u32 {
let mut my_size = 0;
my_size += crate::rt::unknown_fields_size(self.get_unknown_fields());
self.cached_size.set(my_size);
my_size
}

fn write_to_with_cached_sizes(&self, os: &mut crate::CodedOutputStream<'_>) -> crate::ProtobufResult<()> {
os.write_unknown_fields(self.get_unknown_fields())?;
::std::result::Result::Ok(())
}

fn get_cached_size(&self) -> u32 {
self.cached_size.get()
}

fn get_unknown_fields(&self) -> &crate::UnknownFields {
&self.unknown_fields
}

fn mut_unknown_fields(&mut self) -> &mut crate::UnknownFields {
&mut self.unknown_fields
}

fn descriptor(&self) -> &'static crate::reflect::MessageDescriptor {
Self::descriptor_static()
}

fn new() -> MyMessage {
MyMessage::new()
}

fn descriptor_static() -> &'static crate::reflect::MessageDescriptor {
static descriptor: crate::rt::Lazy<crate::reflect::MessageDescriptor> = crate::rt::Lazy::INIT;
descriptor.get(|| {
let fields = ::std::vec::Vec::new();
crate::reflect::MessageDescriptor::new::<MyMessage>(
"MyMessage",
fields,
file_descriptor_proto()
)
})
}

fn default_instance() -> &'static MyMessage {
static instance: crate::rt::Lazy<MyMessage> = crate::rt::Lazy::INIT;
instance.get(MyMessage::new)
}
}

impl crate::Clear for MyMessage {
fn clear(&mut self) {
self.unknown_fields.clear();
}
}

impl ::std::fmt::Debug for MyMessage {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
crate::text_format::fmt(self, f)
}
}

impl crate::reflect::ProtobufValue for MyMessage {
}

static file_descriptor_proto_data: &'static [u8] = b"\
\n\x10doctest_pb.proto\"\x0b\n\tMyMessageJG\n\x06\x12\x04\x02\0\x05\x01\
\n%\n\x01\x0c\x12\x03\x02\0\x122\x1b\x20Messages\x20used\x20in\x20doctes\
ts\n\n\n\n\x02\x04\0\x12\x04\x04\0\x05\x01\n\n\n\x03\x04\0\x01\x12\x03\
\x04\x08\x11b\x06proto3\
";

static file_descriptor_proto_lazy: crate::rt::Lazy<crate::descriptor::FileDescriptorProto> = crate::rt::Lazy::INIT;

fn parse_descriptor_proto() -> crate::descriptor::FileDescriptorProto {
crate::parse_from_bytes(file_descriptor_proto_data).unwrap()
}

/// `FileDescriptorProto` object which was a source for this generated file
pub fn file_descriptor_proto() -> &'static crate::descriptor::FileDescriptorProto {
file_descriptor_proto_lazy.get(|| {
parse_descriptor_proto()
})
}
4 changes: 4 additions & 0 deletions protobuf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ mod misc;
mod buf_read_iter;
mod buf_read_or_reader;

// TODO: does not work: https://github.com/rust-lang/rust/issues/67295
#[cfg(doctest)]
mod doctest_pb;

/// This symbol is in generated `version.rs`, include here for IDE
#[cfg(never)]
pub const VERSION: &str = "";
Expand Down

0 comments on commit 8c933da

Please sign in to comment.