Ever since I am adding #[derive(Debug, serde::Serialize, serde::Deserialize)] to all of my structures I am getting this issue with this particular structure:
object_type : Vec<object_settings::radio_options::Fields>, then the error from the other structure pub mode : Vec<radio_options::Fields>, would go away.
Fields contains a &'static str, but your deserializer cannot provide one, since deserializers are not 'static. For example: you try to deserialize a String in json format into Fields.
fn from_json(s: &String) -> Fields {
// ...
}
How do you construct an instance of Field? You can't get a &'static str from a short-lived &String or String. At best you can have a predefined pool of valid name and error when encountered something not in the pool, but in this case you should use enum (most of time).
Anyway, the short, easy solution would be change name: &'static str to name: String so Field can own the name data itself.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Fields
{
pub name : &'static str,
pub status : bool,
}
let field = Fields{name: "name", status: true}
If I put field into json field and wrote to a file, and I tried to put the name structure field from the file to the field variable then it wouldn't work as &String is outlived?
But lifetime annotations are quite infectious and somewhat annoying to write. (and it makes dynamically changing name hard). So unless you find allocation Strings a performance bottleneck, just use a regular String.
As it is static it gets initlaised in the code with predefined names. the names must remain static and should not change so it doesn't need to get saved.
pub struct Fields
{
pub name : &'static str,
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub status : bool,
}
let field = Fields{name: "name", status: true}
But it isn't. When you're deserializing Fields from something, you're creating new Fields, not initialized to anything other then the data you're deserializing it from.
Cow<'static, str> is a type that can either be a borrowed &'static str or an owned String. So the deserializer will just always produce an owned String in the field. It can't do that for &'static str because that type can't hold an owned String