-
-
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
Conditionally using Serde (cargo feature) #288
Comments
That should do it. What would be the right way if I want to derive Debug in all cases? |
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 |
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
struct X { ... } Do you mean a no-op crate specifically for |
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. |
I filed #289 to track your idea. |
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 compileSerialize, Deserialize
within aderive
call. The only way I have found is repeating the struct twice but is not convenient. Is there any convenient way to achieve it?The text was updated successfully, but these errors were encountered: