I'm running into a serde issue with derive feature mixed with try_from annotation. My goal is to deserialize the FooWithStatic struct via the try_from implementation from RawFoo . I'm getting told the implementation of Deserialize is not general enough. I'm confused because RawFoo is all bound by the 'static lifetimeand my try_from implementation works just fine? It seems like this should be fine lifetime wise?
This looks like a "bug"/limitation in the derive implementation, although I am unsure if that can be easily fixed. One solution might be to allow borrow attributes with an empty value.
What happens is that the derive detects the string slice &'a str and automatically borrows. This bounds the 'de lifetime to outlive 'a. In your case 'a is 'static, so the implementation is actually impl Deserialize<'static> for FooWithStatic. This means that you can only call deserialize if the deserializer can borrow from 'static data, like a string literal or leaked strings.
But borrowing is not necessary in your case, since the TryFrom implementation converts the String into string literals, thus 'static. A more correct trait implementation for FooWithStatic might be
impl Deserialize<'de> for FooWithStatic
where
RawFoo: Deserialize<'de>,
however, this does not work if RawFoo is private and FooWithStatic is public.
This currently fails with error: at least one lifetime must be borrowed. This probably makes sense in the general case, as disabling the borrow just means that it is not possible to deserialize a string slice. However, in the special case of having a serde from/try_from it might not lead to too many misuses.
Apparently, having a &str field automatically puts a #[serde(borrow)] attribute on that field, and there seems to be no good way to disable that. I'm not 100% sure on whether or not that attribute having any effect when try_from is used anyways is a goo thing.
By the way, it's also possible to use the existing re-exports of str i. e. &'static std::primitive::str to "trick" serde here, so you don't even need a type synonym.