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

Consider adding support for #[serde(default = $expr)] #216

Closed
erickt opened this issue Jan 19, 2016 · 1 comment
Closed

Consider adding support for #[serde(default = $expr)] #216

erickt opened this issue Jan 19, 2016 · 1 comment
Milestone

Comments

@erickt
Copy link
Member

erickt commented Jan 19, 2016

There is a bit of a dance that happens when deserializing a struct, and one of the fields is missing. serde_codegen effectively does:

  • Was the field attributed with #[serde(default)]? If so, set the value Default::default(), assuming that the field type actually implements Default.
  • Otherwise, call the Deserializer's missing_field() method. If the format has some built-in support for a value for a missing field, such as JSON considering a missing value as a null value, try to deserialize from that value, or return an error.

This unfortunately means that as a consumer, you may have to newtype yourself an implementation of Default. Instead, it might be better if we optionally allowed for #[serde(default = $expr)], where if the value is not present, we evaluate some expression. This would cut down on some of the boilerplate necessary for using serde_codegen.

Open question: If we do implement this, is this expression being evaluated in the same scope as Deserialize::deserialize? On one level it'd be potentially interesting that it'd be able to access one of the other values to implement itself, but I consider the serde_codegen generated bodies to not be a stable interface. So I'm leaning towards us putting the $expr into a sub-function to keep it from accessing the internal deserialization state.

cc #198, which has a similar idea for specifying alternative serializers/deserializers.

@erickt
Copy link
Member Author

erickt commented Feb 21, 2016

This was implemented in #238!

@erickt erickt closed this as completed Feb 21, 2016
rubdos pushed a commit to rubdos/serde that referenced this issue Jun 20, 2017
This makes it possible to build `traits` without `std`. For this a new
trait `BasicFloat` was introduced, implementing some basic functionality
that works with `core`. Most notably this is lacking functions like
`cos`, `sin`, etc.

`Float` is not available without `std`.

Refs serde-rs#216.
rubdos pushed a commit to rubdos/serde that referenced this issue Jun 20, 2017
traits: Introduce std feature

This makes it possible to build `traits` without `std`. For this a new
trait `BasicFloat` was introduced, implementing some basic functionality
that works with `core`. Most notably this is lacking functions like
`cos`, `sin`, etc.

`Float` is not available without `std`.

Refs serde-rs#216.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants