-
Notifications
You must be signed in to change notification settings - Fork 69
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
Conditional compilation ignores serde_as(as = X) #355
Comments
EDIT(2023): Thanks to the #[cfg_attr(feature="serde_test", cfg_eval::cfg_eval, serde_as)]
#[derive(Default, Debug, Serialize, Deserialize)]
struct StructC {
#[cfg_attr(feature="serde_test", serde_as(as = "Vec<(_, _)>"))]
map : HashMap<(i32,i32), i32>
} The rest of the answer remains valid. It is more complicated but avoid the extra dependency. That is currently not supported, and I don't know of a good solution until support is added in Rust. The problem is that the outer As a workaround, you can apply the transformation steps of use serde_with::{As, Same};
#[cfg_attr(feature="serde_test" ,serde(with = "As::<Vec<(Same, Same)>>"))] Edit: To expand on the answer a bit: It would be possible to "understand" these cfg-attributes and process them inside the |
Thank you for the detailed answer, I expected that there was some proc-macro magic that I did not properly understand ! The workaround is a satisfying solution for my use case, until proper support is implemented. |
Glad I could help. I agree, this could be better documented. One variation you didn't mention, but which I think could be supported quite easily, is this: Having a conditional implementation of the serde traits and always use #[serde_as]
#[cfg_attr(feature = "...", derive(Serialize, Deserialize))]
struct StructD {
#[serde_as(as = "Vec<(_, _)>")]
map: HashMap<(i32, i32), i32>,
} |
I tried this variation when I was investigating the issue, but did not mention because indeed one of my goals was to limit dependencies. If you implement support for it, i think it might be useful to put it in the documentation as an alternative, as it would still save compilation time. |
* Move the list of transformations into its own file. * Make better use of multiple heading levels. * Explain that conditional compilation with serde_as is currently not working. Explain workaround. Closes #355
356: Restructure the serde_as documentation r=jonasbb a=jonasbb * Move the list of transformations into its own file. * Make better use of multiple heading levels. * Explain that conditional compilation with serde_as is currently not working. Explain workaround. Closes #355 Co-authored-by: Jonas Bushart <jonas@bushart.org>
Using conditional compilation appears to break the use of serde_as. Using a struct such as
StructC
below, I would expect it to be serialized to JSON without issue, as happens without using non conditional compilation.However, the attribute appears to have no effect, instead failing with the error
Error("key must be a string", line: 0, column: 0)
.Is this really a bug, or is there something I am not doing right ? I have also given a full example with tests that showcase the problem and what works.
lib.rs
cargo.toml
The text was updated successfully, but these errors were encountered: