Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Can't use with borrowed values in a struct #94

Closed
kbknapp opened this issue Jun 15, 2018 · 5 comments
Closed

Can't use with borrowed values in a struct #94

kbknapp opened this issue Jun 15, 2018 · 5 comments

Comments

@kbknapp
Copy link

kbknapp commented Jun 15, 2018

Myself and @savish are working on porting clap to use serde for all the serialization and deserialization in clap v3. Currently, YAML is the most popular format to deserialize from for clap (because it's the only supported way in v2 😜 ). So we started with a YAML test.

When trying to derive Deserialize for clap::App we run into the error about the 'de lifetime:

15 |     let app: App = serde_yaml::from_str(&yml).unwrap();
   |                         ^^^^^^^^^^^^^^^^^^^^
   |
   = note: required because of the requirements on the impl of `for<'de> _IMPL_DESERIALIZE_FOR_Point::_serde::Deserialize<'de>` for `App<'_,'_>`
   = note: required because of the requirements on the impl of `_IMPL_DESERIALIZE_FOR_Point::_serde::de::DeserializeOwned` for `App<'_,'_>`
   = note: required by `serde_yaml::from_str`

However, if we do the exact same test with serde_json (using a JSON document of course) everything works. So no changes to the clap::App struct, just using serde_json instead of serde_yaml it all works.

Version of serde_yaml

0.7.5

May be related to #85?

@dtolnay
Copy link
Owner

dtolnay commented Jun 15, 2018

serde_json::from_str and serde_yaml::from_str have different signatures. YAML only supports deserializing owned data for now. I am not planning to work on this myself and I think it would be a fairly large amount of work, but I would happily accept a PR to support deserializing borrowed data.

@kbknapp
Copy link
Author

kbknapp commented Jun 16, 2018

Ah ok thanks! I didn't look at the implementation differences between the two. Perhaps once my workload lessens I can see what it would take to put this together.

@turboladen
Copy link

FWIW, I'm running into this as well. I've got a bit of a specialized port of Rails/Ruby's I18n YAML stuff for translating some things for a private crate. Since the data I'm after is really just a static lookup table (that's deserialized from YAML), I was hoping to generate a static struct containing the translations. Just thought I'd leave my use case here in case it helps give some clout to the ticket.

@dtolnay
Copy link
Owner

dtolnay commented Jan 22, 2019

I posted a request for implementation for a new backend: dtolnay/request-for-implementation#9.

jrforrest added a commit to jrforrest/sysunit-rust that referenced this issue Feb 4, 2019
This does a lot of string copying.  Turns out serde_yaml can't handle
deserialization of borrowed vals[1] so I implemented with
heap-allocated strings for now.

Need to do error handling on file missing, unit not found, failure to
read file and failure to deserialize file.

[1] dtolnay/serde-yaml#94
@dtolnay
Copy link
Owner

dtolnay commented Jul 28, 2022

As of 0.9, serde-yaml uses a new YAML backend, which supports this.

@dtolnay dtolnay closed this as completed Jul 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants