-
Notifications
You must be signed in to change notification settings - Fork 27
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
feat req: skip some fields in the struct #29
Comments
This would be a great addition, as sometimes certain fields can be computed from others so storing every field is wasteful (if computing performance is not a factor here). How does it work in practice though? Do I have to provide a value for |
The semantic way to do this currently would be just to create a seperate struct. struct A {
xxxx: B,
yyyy: C,
}
#[derive(Encode, Decode)]
struct ARaw {
xxxx: B,
}
impl From<A> for ARaw {
fn from(val: A) -> Self {
ARaw { xxxx: val.xxxx }
}
} What benefits does creating a separate macro bring except boilerplate reduction? What if I don't want my yyyy to implement default? How would we handle this case? |
|
seams reasonable then |
This comment was marked as off-topic.
This comment was marked as off-topic.
As this issue gets more traffic, I want to mention something that I should have mentioned long before. The following code does work: use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct A {
xxxx: u8,
#[serde(skip)]
yyyy: u8,
}
fn main() {
let binary = bitcode::serialize(&A { xxxx: 42, yyyy: 43 }).unwrap();
let a2 = bitcode::deserialize::<A>(&binary).unwrap();
assert_eq!(a2, A { xxxx: 42, yyyy: 0 });
} This is because
This is entirely up to This issue remains open because |
@finnbear Yeah, to be more specific, I mistyped, I meant the |
@fadeevab Pretty sure This limitation is documented here. I guess we could make |
@finnbear I found a relevant serde issue skip_serializing_if is a footgun and an alive one: my comment in Feedback request: How to handle unsupported "self-describing-only" attributes. The issue could've been solved if someone had done a heavy lifting of adding a way to conditionally evade the |
In serde, we can use
#[serde(skip)]
on fields we don't want to serialize/deserialize.Here is an example:
In the case above, Type
C
should not deriveSerialize
andDeserialize
.The text was updated successfully, but these errors were encountered: