-
I'm trying to test saving and loading a Slint struct by serde in Rust. I found a pull request about this topic and tested the code for my self. The Slint file has only a small test struct and a window to display it: @rust-attr(cfg_attr(feature="serde", derive(Serialize, Deserialize)))
export struct winData {
number: int,
text: string
}
export component mainWindow inherits Window {
in-out property <winData> data;
VerticalLayout {
padding: 10px;
Text {text: data.number;}
Text {text: data.text;}
}
} The rust code is creating the window and loading a sample file: use std::fs;
slint::slint!(import { mainWindow } from "./src/main.slint";);
fn main() {
let main_window = mainWindow::new().unwrap();
let fname = "../../data.json";
match fs::read_to_string(&fname) {
Err(_) => println!("Error open TMx devices file: {:?}",
&fname),
Ok(data) => {
match serde_json::from_str::<winData>(&data) {
Ok(res) => {
main_window.set_data(res);
},
Err(e) => {
println!("Error reading TMx devices config: \
{:?}", e);
}
}}};
main_window.run();
} The compilation of this project fails with the following error:
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
Since your crate probably doesn't have a |
Beta Was this translation helpful? Give feedback.
-
This is the point I started from and leads to the following compiler error:
|
Beta Was this translation helpful? Give feedback.
-
ah, you need to enable the |
Beta Was this translation helpful? Give feedback.
-
Can you please provide a small example how to do this? Edit:
When I try |
Beta Was this translation helpful? Give feedback.
-
In case someone else has the same challenge: The Afterwards the The activation of the serde feature will enable Serialization / Deserialization of the SharedString data type. |
Beta Was this translation helpful? Give feedback.
In case someone else has the same challenge:
The
serde
feature of Slint can be activated with the following dependency entry in theCargo.toml
file:i-slint-core = { version = "1.1", features = ["serde"] }
Afterwards the
serde
derive macros can be used in Slint files, as described in the documentation:@rust-attr(derive(serde::Serialize, serde::Deserialize))
The activation of the serde feature will enable Serialization / Deserialization of the SharedString data type.