[][src]Function quicksilver::saving::save

pub fn save<T: Serialize>(
    appname: &str,
    profile: &str,
    data: &T
) -> Result<(), SaveError>

Save some arbitrary data to the given profile using Serde

Different platforms may have different save locations: on the Web, data is saved in local storage, on the desktop, it is stored in some appropriate home-directory folder.

The appname should be some constant; this is used to name the file to place the save in on desktop platforms. The profile should allow multiple saves of the same game (save slots, numbered saves, different players) etc.

The example shows how to round-trip some data. Note that for load you must explicitly specify the type of the data, this is because the struct is not passed as a parameter to load so Rust cannot infer the type.

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Player {
    name: String,
    score: u32
}

let player1 = Player { name: "Bob".to_string(), score: 21 };
save("mygame", "player1", &player1).expect("Could not save Player 1");

let player2 = Player { name: "Alice".to_string(), score: 200 };
save("mygame", "player2", &player2).expect("Could not save Player 2");

// Now reload.
let player1 = load::<Player>("mygame", "player1").expect("Could not load Player 1");
let player2 = load::<Player>("mygame", "player2").expect("Could not load Player 2");