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

Conditionally using Serde (cargo feature) #288

Closed
hgrecco opened this issue Apr 17, 2016 · 6 comments
Closed

Conditionally using Serde (cargo feature) #288

hgrecco opened this issue Apr 17, 2016 · 6 comments
Labels

Comments

@hgrecco
Copy link

hgrecco commented Apr 17, 2016

I am trying to use serde as optional feature defined in cargo. Most of the time is quite easy because I can just use cfg to conditional compile certain functions or modules. But I am having trouble to conditionally compile Serialize, Deserialize within a derive call. The only way I have found is repeating the struct twice but is not convenient. Is there any convenient way to achieve it?

@hgrecco hgrecco changed the title Using cargo f Conditionally using Serde (cargo feature) Apr 17, 2016
@dtolnay
Copy link
Member

dtolnay commented Apr 17, 2016

Does cfg_attr do what you want?

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
struct X { ... }

Currently there is a bug (#148) where this does not work if you use a build.rs to run the code generator, but it should work if you use #![plugin(serde_macros)].

@hgrecco
Copy link
Author

hgrecco commented Apr 17, 2016

That should do it. What would be the right way if I want to derive Debug in all cases?

@hgrecco
Copy link
Author

hgrecco commented Apr 17, 2016

Additionally, have you thought about creating a no op crate? In that way I could just change the extern part. It would make thing much cleaner

@dtolnay
Copy link
Member

dtolnay commented Apr 17, 2016

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
struct X { ... }

Do you mean a no-op crate specifically for derive(Serialize, Deserialize) which marks the attribute as used but ignores it? We could make one, but it would not replace cfg_attr for people who use it to (1) work with stable Rust which does not support compiler plugins, or (2) avoid a dependency.

@hgrecco
Copy link
Author

hgrecco commented Apr 17, 2016

Thanks for taking the time to answer. Indeed I was thinking about a crate that allows you to do:

#[cfg(feature="serde")]
extern crate serde;
#[cfg(not(feature="serde"))]
extern crate serde_noop;

#[derive(Debug,Serialize, Deserialize)]
struct Point {
    x: i32,
    y: i32,
}

Might not seem like a big win. But when you have lots of structs, it is certainly helpful and keeps the code cleaner. Not sure if you can do something like that works in stable rust.

Regarding the the dependency, I think that an extra light weight dependency is not such a big deal. In particular with Cargo that makes things really simple.

@dtolnay
Copy link
Member

dtolnay commented Apr 18, 2016

I filed #289 to track your idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants