Trait preferences::Preferences
[−]
[src]
pub trait Preferences { fn save<S>(&self, path: S) -> Result<(), PreferencesError> where S: AsRef<str>; fn load<S>(&mut self, path: S) -> Result<(), PreferencesError> where S: AsRef<str>; fn save_to<W>(&self, writer: &mut W) -> Result<(), PreferencesError> where W: Write; fn load_from<R>(&mut self, reader: &mut R) -> Result<(), PreferencesError> where R: Read; }
Trait for types that can be saved & loaded as user data.
This type is automatically implemented for any type T
which implements both Encodable
and
Decodable
(from rustc-serialize
). However, you are encouraged to use the provided type,
PreferencesMap
.
The path
parameter of save(..)
and load(..)
should be a valid, relative file path. It is
highly recommended that you use the format
[company or author]/[application name]/[data description]
. For example, a game might use
the following paths for player options and save data, respectively:
fun-games-inc/awesome-game-2/options
fun-games-inc/awesome-game-2/saves
Required Methods
fn save<S>(&self, path: S) -> Result<(), PreferencesError> where S: AsRef<str>
Saves the current state of this object. Implementation is platform-dependent, but the data will be local to the active user. For more details, see the module documentation.
Failures
If a serialization or file I/O error occurs (e.g. permission denied), or if the provided
path
argument is invalid.
fn load<S>(&mut self, path: S) -> Result<(), PreferencesError> where S: AsRef<str>
Loads this object's state from previously saved user data with the same path
. This is
an instance method which completely overwrites the object's state with the serialized
data. Thus, it is recommended that you call this method immediately after instantiating
the preferences object.
Failures
If a deserialization or file I/O error occurs (e.g. permission denied), if the provided
path
argument is invalid, or if no user data exists at that path
.
fn save_to<W>(&self, writer: &mut W) -> Result<(), PreferencesError> where W: Write
Same as save
, but writes the serialized preferences to an arbitrary writer.
fn load_from<R>(&mut self, reader: &mut R) -> Result<(), PreferencesError> where R: Read
Same as load
, but reads the serialized preferences from an arbitrary writer.