[][src]Function gestalt::save

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

Save some arbitrary data to the given location 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 data in on desktop platforms. The profile should allow different things to save for the same app, such as save for different players in a game.

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 gestalt::{Location, save, load};
use serde::{Serialize, Deserialize};

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

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

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

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