-
-
Notifications
You must be signed in to change notification settings - Fork 774
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
Serialized newtype structs in serde #526
Comments
See https://serde.rs/impl-serialize.html#serializing-a-struct and The Book for the different types of structs. |
You also need to set up Serde to generate a Serialize implementation for your struct. As an example see how Bincode does this, and it is documented at https://serde.rs/codegen.html. In Cargo.toml add: [dev-dependencies]
serde_macros = "0.8" In tests/tests.rs add: #![feature(plugin, custom_derive)]
#![plugin(serde_macros)]
#[test]
fn serialize_newtype_struct() {
#[derive(Serialize, Deserialize)]
struct Meters(i32);
let km = Meters(1000);
assert_eq!(
term_to_binary(&km).unwrap(),
// ... some result there
);
}
Also this setup requires that your tests are run with a nightly compiler only (which is good enough for Bincode). There is a Rust PR rust-lang/rust#35957 which will allow it to work on stable so we are not that far away, but if you want it to work on a stable compiler now, there is a more complicated setup using a build script - see serde-yaml as an example. EDIT: never mind about the crossed out part. You can put this in src/lib.rs and keep the tests where they are. #![cfg_attr(test, feature(plugin, custom_derive))]
#![cfg_attr(test, plugin(serde_macros))] |
So, I have separated my module into two different:
And in
In the test.rs of bert_tests crate: #![cfg_attr(not(feature = "with-syntex"), feature(custom_derive, plugin))]
#![cfg_attr(not(feature = "with-syntex"), plugin(serde_macros))]
extern crate serde;
extern crate bert;
#[cfg(feature = "with-syntex")]
include!(concat!(env!("OUT_DIR"), "/test.rs"));
#[cfg(not(feature = "with-syntex"))]
include!("test.rs.in"); But in the result I'd taken an error from the Rust compiler:
|
Running the tests with serde_macros (the default) requires a nightly compiler.
Running them without serde_macros on a stable compiler:
In bert_tests/Cargo.toml you can set stable to be the default by doing this, although typically it will compile faster on nightly so I would recommend keeping the default as nightly. [features]
default = ["with-syntex"] # instead of "serde-macros" Then on stable:
And on nightly:
|
I don't understand this clearly, because I'm new in Rust lang. Could you say, whether I'm doing it right? Maybe I'm doing it wrong:
test.rs: #![cfg_attr(not(feature = "with-syntex"), feature(custom_derive, plugin))]
#![cfg_attr(not(feature = "with-syntex"), plugin(serde_macros))]
extern crate serde;
extern crate bert;
#[cfg(feature = "with-syntex")]
include!(concat!(env!("OUT_DIR"), "/test.rs"));
#[cfg(not(feature = "with-syntex"))]
include!("test.rs.in"); Cargo.toml [features]
default = ["with-syntex"]
with-syntex = ["syntex", "serde_codegen"]
[build-dependencies]
syntex = { version = "*", optional = true }
serde_codegen = { version = "0.8.0", optional = true }
[dependencies]
bert = { path = "../bert" }
serde = "0.8.3"
serde_macros = { version = "0.8.0", optional = true }
[[test]]
name = "test"
path = "tests/test.rs"
#![cfg_attr(test, feature(plugin, custom_derive))]
#![cfg_attr(test, plugin(serde_macros))]
extern crate byteorder;
extern crate num;
extern crate serde;
pub use errors::{Error};
pub use serializers::{Serializer, term_to_binary, to_vec, to_writer};
pub use types::{
BERT_LABEL, EXT_VERSION,
BertTag, BertNil, BertTime, BertRegex
};
mod serializers;
mod types;
mod errors;
$ rustup run stable cargo test and nightly branch: $ rustup run nightly cargo test --no-default-features --features serde_macros
If all these steps are right, so... Compiler warn me, that some of functionality are private, not public:
|
See Crates and Modules: Exporting a Public Interface. In |
I've fixed this issue. Just add the |
You shouldn't have both |
I've got it! Thanks for the useful tip. 😃 |
Currently I'm trying to implement serializing newtype structs. So, we have the next code:
And simple test for this stuff:
term_to_binary
function it's no more than a little wrapper, which let so use BERT serializer more easier way. It has the next signature:So, how can I invoke the
serialize_newtype_struct
method with passed into term_to_binary newtype struct from test?The text was updated successfully, but these errors were encountered: