Skip to content

Commit

Permalink
Remove MessageStatic trait
Browse files Browse the repository at this point in the history
`MessageStatic` is an artifact of very old days of Rust.  It is not
needed today, because function can have `Self : Sized` constraints
today.

This change is backward incompatible.

Closes issue #214
  • Loading branch information
stepancheg committed Dec 26, 2017
1 parent e929979 commit fba4412
Show file tree
Hide file tree
Showing 21 changed files with 84 additions and 210 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]

- [Drop `MessageStatic` trait](https:/stepancheg/rust-protobuf/issues/214)
- [`protobuf-codegen` is a separate crate](https:/stepancheg/rust-protobuf/pull/261)
- [Drop old reflection
accessors](https:/stepancheg/rust-protobuf/commit/7a03aee4e67bdd25ae6c403f37386707a0ab5eb9).
Expand Down
8 changes: 3 additions & 5 deletions perftest/vs-cxx/perftest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ extern crate protobuf;
extern crate rand;
extern crate time;

use std::default::Default;
use std::fs::File;
use std::path::Path;

Expand All @@ -11,7 +10,6 @@ use rand::StdRng;
use rand::SeedableRng;

use protobuf::Message;
use protobuf::MessageStatic;
use perftest_data::PerftestData;

mod perftest_data;
Expand All @@ -36,7 +34,7 @@ struct TestRunner {
}

impl TestRunner {
fn run_test<M : Message + MessageStatic>(&self, name: &str, data: &[M]) {
fn run_test<M : Message + Clone + PartialEq>(&self, name: &str, data: &[M]) {
assert!(data.len() > 0, "empty string for test: {}", name);

let mut rng: StdRng = SeedableRng::from_seed(&[10, 20, 30, 40][..]);
Expand Down Expand Up @@ -76,7 +74,7 @@ impl TestRunner {
random_data.len() as u64,
|| {
let mut coded_input_stream = protobuf::CodedInputStream::from_bytes(&buf);
let mut msg: M = Default::default();
let mut msg: M = Message::new();
let mut count = 0;
while !coded_input_stream.eof().unwrap() {
msg.clear();
Expand All @@ -90,7 +88,7 @@ impl TestRunner {
assert_eq!(random_data.len(), merged);
}

fn test<M : Message + MessageStatic>(&mut self, name: &str, data: &[M]) {
fn test<M : Message + Clone + PartialEq>(&mut self, name: &str, data: &[M]) {
if self.selected.as_ref().map(|s| *s == name).unwrap_or(true) {
self.run_test(name, data);
self.any_matched = true;
Expand Down
10 changes: 2 additions & 8 deletions protobuf-codegen/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,9 @@ impl<'a> MessageGen<'a> {
});
w.write_line("");
w.def_fn("descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor", |w| {
w.write_line("::protobuf::MessageStatic::descriptor_static(None::<Self>)");
w.write_line("::protobuf::Message::descriptor_static(None::<Self>)");
});
});
}

fn write_impl_message_static(&self, w: &mut CodeWriter) {
w.impl_for_block("::protobuf::MessageStatic", &self.type_name, |w| {
w.write_line("");
w.def_fn(&format!("new() -> {}", self.type_name), |w| {
w.write_line(&format!("{}::new()", self.type_name));
});
Expand Down Expand Up @@ -429,8 +425,6 @@ impl<'a> MessageGen<'a> {
w.write_line("");
self.write_impl_message(w);
w.write_line("");
self.write_impl_message_static(w);
w.write_line("");
self.write_impl_clear(w);
if !self.lite_runtime {
w.write_line("");
Expand Down
10 changes: 5 additions & 5 deletions protobuf-test/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ use protobuf::hex::decode_hex;

use protobuf::*;

pub fn test_serialize_deserialize_length_delimited<M : Message + MessageStatic>(msg: &M) {
pub fn test_serialize_deserialize_length_delimited<M : Message + PartialEq>(msg: &M) {
let serialized_bytes = msg.write_length_delimited_to_bytes().unwrap();
let parsed = parse_length_delimited_from_bytes::<M>(&serialized_bytes).unwrap();
assert!(*msg == parsed);
}

pub fn test_serialize_deserialize_no_hex<M : Message + MessageStatic>(msg: &M) {
pub fn test_serialize_deserialize_no_hex<M : Message + PartialEq>(msg: &M) {
let serialized_bytes = msg.write_to_bytes().unwrap();
let parsed = parse_from_bytes::<M>(&serialized_bytes).unwrap();
assert!(*msg == parsed);
}

pub fn test_serialize_deserialize<M : Message + MessageStatic>(hex: &str, msg: &M) {
pub fn test_serialize_deserialize<M : Message + PartialEq>(hex: &str, msg: &M) {
let expected_bytes = decode_hex(hex);
let expected_hex = encode_hex(&expected_bytes);
let serialized = msg.write_to_bytes().unwrap();
Expand All @@ -34,13 +34,13 @@ pub fn test_serialize_deserialize<M : Message + MessageStatic>(hex: &str, msg: &
test_serialize_deserialize_length_delimited(msg);
}

pub fn test_deserialize<M : Message + MessageStatic>(hex: &str, msg: &M) {
pub fn test_deserialize<M : Message + PartialEq>(hex: &str, msg: &M) {
let bytes = decode_hex(hex);
let parsed = parse_from_bytes::<M>(&bytes).unwrap();
assert!(*msg == parsed);
}

pub fn test_serialize<M : Message + MessageStatic>(hex: &str, msg: &M) {
pub fn test_serialize<M : Message>(hex: &str, msg: &M) {
let hex = encode_hex(&decode_hex(hex));

let serialized = msg.write_to_bytes().unwrap();
Expand Down
35 changes: 14 additions & 21 deletions protobuf/src/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::any::Any;
use std::any::TypeId;
use std::default::Default;
use std::fmt;
use std::io::Read;
use std::io::Write;
Expand All @@ -23,11 +22,8 @@ use error::ProtobufResult;


/// Trait implemented for all generated structs for protobuf messages.
/// Also, generated messages implement `Clone + Default + PartialEq`
pub trait Message: fmt::Debug + Clear + Any + Send + Sync {
// All generated Message types also implement MessageStatic.
// However, rust doesn't allow these types to be extended by
// Message.

/// Message descriptor for this message, used for reflection.
fn descriptor(&self) -> &'static MessageDescriptor;

Expand Down Expand Up @@ -171,25 +167,22 @@ pub trait Message: fmt::Debug + Clear + Any + Send + Sync {
// impl<M : Message> fmt::Debug for M {
// ...
// }
}

/// Not-object safe functions of the message.
/// TODO: move functons to `Message` trait with `Self` bounds.
pub trait MessageStatic: Message + Clone + Default + PartialEq {
/// Create an empty message object.
fn new() -> Self;
fn new() -> Self where Self : Sized;

/// Get message descriptor for message type.
// http://stackoverflow.com/q/20342436/15018
fn descriptor_static(_: Option<Self>) -> &'static MessageDescriptor {
fn descriptor_static(_: Option<Self>) -> &'static MessageDescriptor
where Self : Sized
{
panic!(
"descriptor_static is not implemented for message, \
LITE_RUNTIME must be used"
);
}
}


}

pub fn message_down_cast<'a, M : Message + 'a>(m: &'a Message) -> &'a M {
m.as_any().downcast_ref::<M>().unwrap()
Expand Down Expand Up @@ -228,28 +221,28 @@ pub trait ProtobufEnum: Eq + Sized + Copy + 'static {
}

/// Parse message from stream.
pub fn parse_from<M : Message + MessageStatic>(is: &mut CodedInputStream) -> ProtobufResult<M> {
let mut r: M = MessageStatic::new();
pub fn parse_from<M : Message>(is: &mut CodedInputStream) -> ProtobufResult<M> {
let mut r: M = Message::new();
r.merge_from(is)?;
r.check_initialized()?;
Ok(r)
}

/// Parse message from reader.
/// Parse stops on EOF or when error encountered.
pub fn parse_from_reader<M : Message + MessageStatic>(reader: &mut Read) -> ProtobufResult<M> {
pub fn parse_from_reader<M : Message>(reader: &mut Read) -> ProtobufResult<M> {
reader.with_coded_input_stream(|is| parse_from::<M>(is))
}

/// Parse message from byte array.
pub fn parse_from_bytes<M : Message + MessageStatic>(bytes: &[u8]) -> ProtobufResult<M> {
pub fn parse_from_bytes<M : Message>(bytes: &[u8]) -> ProtobufResult<M> {
bytes.with_coded_input_stream(|is| parse_from::<M>(is))
}

/// Parse message from `Bytes` object.
/// Resulting message may share references to the passed bytes object.
#[cfg(feature = "bytes")]
pub fn parse_from_carllerche_bytes<M : Message + MessageStatic>(
pub fn parse_from_carllerche_bytes<M : Message>(
bytes: &Bytes,
) -> ProtobufResult<M> {
// Call trait explicitly to avoid accidental construction from `&[u8]`
Expand All @@ -259,14 +252,14 @@ pub fn parse_from_carllerche_bytes<M : Message + MessageStatic>(
/// Parse length-delimited message from stream.
///
/// Read varint length first, and read messages of that length then.
pub fn parse_length_delimited_from<M : Message + MessageStatic>(
pub fn parse_length_delimited_from<M : Message>(
is: &mut CodedInputStream,
) -> ProtobufResult<M> {
is.read_message::<M>()
}

/// Parse length-delimited message from `Read`.
pub fn parse_length_delimited_from_reader<M : Message + MessageStatic>(
pub fn parse_length_delimited_from_reader<M : Message>(
r: &mut Read,
) -> ProtobufResult<M> {
// TODO: wrong: we may read length first, and then read exact number of bytes needed
Expand All @@ -275,7 +268,7 @@ pub fn parse_length_delimited_from_reader<M : Message + MessageStatic>(

/// Parse length-delimited message from bytes.
// TODO: currently it's not possible to know how many bytes read from slice.
pub fn parse_length_delimited_from_bytes<M : Message + MessageStatic>(
pub fn parse_length_delimited_from_bytes<M : Message>(
bytes: &[u8],
) -> ProtobufResult<M> {
bytes.with_coded_input_stream(|is| is.read_message::<M>())
Expand Down
Loading

0 comments on commit fba4412

Please sign in to comment.