Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dyn trait v2.7 #432

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions proto/rustproto.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ extend google.protobuf.FileOptions {

// When true, will only generate codes that works with lite runtime.
optional bool lite_runtime_all = 17035;
// When true, will generate dyn Trait for 2018 edition.
optional bool dyn_trait_all = 17036;
}

extend google.protobuf.MessageOptions {
Expand Down
13 changes: 13 additions & 0 deletions protobuf-codegen/src/customize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct Customize {
/// Used internally to generate protos bundled in protobuf crate
/// like `descriptor.proto`
pub inside_protobuf: Option<bool>,
/// Enable dyn Trait generation for 2018 edition
pub dyn_trait: Option<bool>,

// When adding more options please keep in sync with `parse_from_parameter` below.
/// Make sure `Customize` is always used with `..Default::default()`
Expand Down Expand Up @@ -71,6 +73,9 @@ impl Customize {
if let Some(v) = that.inside_protobuf {
self.inside_protobuf = Some(v);
}
if let Some(v) = that.dyn_trait {
self.dyn_trait = Some(v);
}
}

/// Update unset fields of self with fields from other customize
Expand Down Expand Up @@ -115,6 +120,8 @@ impl Customize {
r.lite_runtime = Some(parse_bool(v)?);
} else if n == "inside_protobuf" {
r.inside_protobuf = Some(parse_bool(v)?);
} else if n == "dyn_trait" {
r.dyn_trait = Some(parse_bool(v)?);
} else {
return Err(CustomizeParseParameterError::UnknownOptionName(
n.to_owned(),
Expand All @@ -135,6 +142,7 @@ pub fn customize_from_rustproto_for_message(source: &MessageOptions) -> Customiz
let serde_derive_cfg = rustproto::exts::serde_derive_cfg.get(source);
let lite_runtime = None;
let inside_protobuf = None;
let dyn_trait = None;
Customize {
expose_oneof,
expose_fields,
Expand All @@ -145,6 +153,7 @@ pub fn customize_from_rustproto_for_message(source: &MessageOptions) -> Customiz
serde_derive_cfg,
lite_runtime,
inside_protobuf,
dyn_trait,
_future_options: (),
}
}
Expand All @@ -160,6 +169,7 @@ pub fn customize_from_rustproto_for_field(source: &FieldOptions) -> Customize {
let serde_derive_cfg = None;
let lite_runtime = None;
let inside_protobuf = None;
let dyn_trait = None;
Customize {
expose_oneof,
expose_fields,
Expand All @@ -170,6 +180,7 @@ pub fn customize_from_rustproto_for_field(source: &FieldOptions) -> Customize {
serde_derive_cfg,
lite_runtime,
inside_protobuf,
dyn_trait,
_future_options: (),
}
}
Expand All @@ -184,6 +195,7 @@ pub fn customize_from_rustproto_for_file(source: &FileOptions) -> Customize {
let serde_derive_cfg = rustproto::exts::serde_derive_cfg_all.get(source);
let lite_runtime = rustproto::exts::lite_runtime_all.get(source);
let inside_protobuf = None;
let dyn_trait = rustproto::exts::dyn_trait_all.get(source);
Customize {
expose_oneof,
expose_fields,
Expand All @@ -194,6 +206,7 @@ pub fn customize_from_rustproto_for_file(source: &FileOptions) -> Customize {
serde_derive_cfg,
lite_runtime,
inside_protobuf,
dyn_trait,
_future_options: (),
}
}
18 changes: 13 additions & 5 deletions protobuf-codegen/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct MessageGen<'a> {
type_name: String,
pub fields: Vec<FieldGen<'a>>,
pub lite_runtime: bool,
pub dyn_trait: bool,
customize: Customize,
}

Expand All @@ -45,12 +46,14 @@ impl<'a> MessageGen<'a> {
.get_optimize_for()
== FileOptions_OptimizeMode::LITE_RUNTIME
});
let dyn_trait = customize.dyn_trait.unwrap_or_else(|| false);
MessageGen {
message: message,
root_scope: root_scope,
type_name: message.rust_name(),
fields: fields,
lite_runtime,
dyn_trait,
customize,
}
}
Expand Down Expand Up @@ -359,14 +362,19 @@ impl<'a> MessageGen<'a> {
w.write_line("");
self.write_unknown_fields(w);
w.write_line("");
w.def_fn("as_any(&self) -> &::std::any::Any", |w| {
w.write_line("self as &::std::any::Any");
let any = if self.dyn_trait {
"dyn ::std::any::Any"
} else {
"::std::any::Any"
};
w.def_fn(&format!("as_any(&self) -> &{}", any), |w| {
w.write_line(&format!("self as &{}", any));
});
w.def_fn("as_any_mut(&mut self) -> &mut ::std::any::Any", |w| {
w.write_line("self as &mut ::std::any::Any");
w.def_fn(&format!("as_any_mut(&mut self) -> &mut {}", any), |w| {
w.write_line(&format!("self as &mut {}", any));
});
w.def_fn(
"into_any(self: Box<Self>) -> ::std::boxed::Box<::std::any::Any>",
&format!("into_any(self: Box<Self>) -> ::std::boxed::Box<{}>", any),
|w| {
w.write_line("self");
},
Expand Down
2 changes: 1 addition & 1 deletion protobuf/src/descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file is generated by rust-protobuf 2.7.0-pre. Do not edit
// This file is generated by rust-protobuf 2.7.0. Do not edit
// @generated

// https://github.com/Manishearth/rust-clippy/issues/702
Expand Down
2 changes: 1 addition & 1 deletion protobuf/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file is generated by rust-protobuf 2.7.0-pre. Do not edit
// This file is generated by rust-protobuf 2.7.0. Do not edit
// @generated

// https://github.com/Manishearth/rust-clippy/issues/702
Expand Down
Loading