Compiling playground v0.0.1 (/playground)
error: lifetime may not live long enough
--> src/main.rs:44:5
|
38 | #[derive(Serialize, Deserialize)]
| ----------- lifetime `'de` defined here
39 | struct FooBarFourList<'a> {
| -- lifetime `'a` defined here
...
44 | foo_bar_six: Map<&'a str, FooBarFour<'a>>
| ^^^^^^^^^^^ requires that `'de` must outlive `'a`
|
= help: consider adding the following bound: `'de: 'a`
error: could not compile `playground` due to previous error
Except for very special cases, it's not possible to deserialize into borrowed data. Consider what would happen:
If your string contains escape characters. Then the deserializer can't just point into the string literal inside the serialized data, because it has to transform the escape sequences. So it has to allocate an owned string somewhere, but where would that go if your type only contains a borrow?
If deserialization happens from a stream rather than a slice, then there's nothing to borrow from, since data arrives in small chunks and might not even be available all at once.
I replaced all &str with Strings and removed most of the (now unnecessary) lifetime specifications. It’s compiling fine but I’m getting a serde parse error now
It's not that the TOML is invalid in itself. As it is evident from the error message, it just doesn't match your schema. Which is unsurprising, since you are serializing a FooBarFourList but you try to deserialize a FooBarFour. This runs correctly.
Awesome! I had recognised that the type verification worked right to left, but had not realised that it also worked left to right - i.e. the type of the LHS determines the schema that is used to validate the TOML. Lesson learned!
I'm not sure what you are getting at here, or what left and right mean in this context. There is only one type you were asking for, and it is FooBarFour. There is no way you can get a variable of type FooBarFour to hold a value of type FooBarFourList, if that's what you are asking – Rust is statically typed.
(And in deserialization, of course types have to drive the schema. The data to be deserialized is a dynamic tree of a small set of very general primitive values, such as strings, numbers, arrays and maps. There is no information in the TOML itself as to what exact user-defined static type it was serialized from/it can deserialize into. Thus the only way deserialization can get the type information is that the Deserialize impl of your own user-defined types knows what to ask for, and verify if the dynamic values happen to match their expectations.)