Trait disk::Bincode

source ·
pub unsafe trait Bincode: Serialize + DeserializeOwned {
    const OS_DIRECTORY: Dir;
    const PROJECT_DIRECTORY: &'static str;
    const SUB_DIRECTORIES: &'static str;
    const FILE: &'static str;
    const FILE_EXT: &'static str;
    const FILE_NAME: &'static str;
    const FILE_NAME_GZIP: &'static str;
    const FILE_NAME_TMP: &'static str;
    const FILE_NAME_GZIP_TMP: &'static str;
    const HEADER: [u8; 24];
    const VERSION: u8;
Show 34 methods // Provided methods fn mkdir() -> Result<(), Error> { ... } fn rm_rf() -> Result<(), Error> { ... } fn rm() -> Result<(), Error> { ... } fn exists() -> Result<bool, Error> { ... } fn project_dir() -> &'static str { ... } fn sub_dirs() -> &'static str { ... } fn file_name() -> &'static str { ... } fn file_ext() -> &'static str { ... } fn file() -> &'static str { ... } fn file_gzip() -> &'static str { ... } fn file_tmp() -> &'static str { ... } fn file_gzip_tmp() -> &'static str { ... } fn base_path() -> Result<PathBuf, Error> { ... } fn absolute_path() -> Result<PathBuf, Error> { ... } fn read_to_bytes() -> Result<Vec<u8>, Error> { ... } fn read_to_bytes_gzip() -> Result<Vec<u8>, Error> { ... } fn exists_gzip() -> Result<bool, Error> { ... } fn from_file() -> Result<Self, Error> { ... } fn from_file_gzip() -> Result<Self, Error> { ... } fn save(&self) -> Result<(), Error> { ... } fn save_gzip(&self) -> Result<(), Error> { ... } fn save_atomic(&self) -> Result<(), Error> { ... } fn save_atomic_gzip(&self) -> Result<(), Error> { ... } fn rm_atomic(&self) -> Result<(), Error> { ... } fn rm_atomic_gzip(&self) -> Result<(), Error> { ... } fn rm_tmp() -> Result<(), Error> { ... } fn absolute_path_gzip() -> Result<PathBuf, Error> { ... } fn into_writable_fmt(&self) -> Result<Vec<u8>, Error> { ... } fn from_bytes(bytes: &[u8]) -> Result<Self, Error> { ... } fn to_bytes(&self) -> Result<Vec<u8>, Error> { ... } fn header_version_bytes(&self) -> [u8; 25] { ... } fn file_header_to_string(&self) -> Result<String, Error> { ... } fn file_version() -> Result<u8, Error> { ... } fn file_bytes(range: Range<u16>) -> Result<Vec<u8>, Error> { ... }
}
Expand description

Bincode (binary) file format

Encoding

The encoding options used is:

bincode::DefaultOptions::new().with_varint_encoding();

File extension is .bin.

Safety

When manually implementing, you are promising that the PATH’s manually specified are correct.

Required Associated Constants§

source

const OS_DIRECTORY: Dir

Which OS directory it will be saved in.

source

const PROJECT_DIRECTORY: &'static str

What the main project directory will be.

source

const SUB_DIRECTORIES: &'static str

Optional sub directories in between the project directory and file.

source

const FILE: &'static str

What the raw file name will be (no extension).

source

const FILE_EXT: &'static str

What the file extension will be.

source

const FILE_NAME: &'static str

What the full filename + extension will be.

source

const FILE_NAME_GZIP: &'static str

What the gzip variant of the filename will be.

source

const FILE_NAME_TMP: &'static str

What the tmp variant of the filename will be.

source

const FILE_NAME_GZIP_TMP: &'static str

What the gzip + tmp variant of the filename will be.

source

const HEADER: [u8; 24]

A custom 24-byte length identifying header for your Bincode file.

This is combined with Self::VERSION to prefix your file with 25 bytes.

Note: Self::save_gzip() applies compression AFTER, meaning the entire file must be decompressed to get these headers.

source

const VERSION: u8

What the version byte will be (0-255).

Provided Methods§

source

fn mkdir() -> Result<(), Error>

Create the directories leading up-to the file.

This is not necessary when using any variant of Self::save() as the directories are created implicitly.

source

fn rm_rf() -> Result<(), Error>

Recursively remove this file’s project directory.

This deletes all directories starting from Self::PROJECT_DIRECTORY. For example:

disk::toml!(State, disk::Dir::Data, "MyProject", "sub_dir", "state");

This project’s file would be located at ~/.local/share/myproject. This is the PATH that gets removed recursively.

This is akin to running:

rm -rf ~/.local/share/myproject

The input to all disk macros are sanity checked. The worst you can do with this function is delete your project’s directory.

This function calls std::fs::remove_dir_all, which does not follow symlinks.

source

fn rm() -> Result<(), Error>

Try deleting the file.

This will return success if the file doesn’t exist or if deleted.

It will return failure if the file existed but could not be deleted or if any other error occurs.

source

fn exists() -> Result<bool, Error>

Check if the file exists.

  • true == The file exists.
  • false == The file does not exist.
  • anyhow::Error == There was an error, existance is unknown.
source

fn project_dir() -> &'static str

The main project directory.

You can also access this directly on your type:

disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);

assert!(Data::project_dir() == Data::PROJECT_DIRECTORY);
source

fn sub_dirs() -> &'static str

The directories after the main project directory, before the file. (the first directory specified in the SUB_DIRECTORIES constant).

You can also access this directly on your type:

disk::toml!(Data, disk::Dir::Cache, "MyProject", "sub_directory", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);

assert!(Data::sub_dirs() == Data::SUB_DIRECTORIES);
source

fn file_name() -> &'static str

The filename + extension associated with this struct.

You can also access this directly on your type:

disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);

assert!(Data::file_name() == Data::FILE_NAME);
source

fn file_ext() -> &'static str

The extension associated with this struct.

You can also access this directly on your type:

disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);

assert!(Data::file_ext() == Data::FILE_EXT);
source

fn file() -> &'static str

The file associated with this struct WITHOUT the extension.

You can also access this directly on your type:

disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);

assert!(Data::file() == Data::FILE);
source

fn file_gzip() -> &'static str

The gzip variant of the filename + extension associated with this struct.

You can also access this directly on your type:

disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);

assert!(Data::file_gzip() == Data::FILE_NAME_GZIP);
source

fn file_tmp() -> &'static str

The tmp variant of the filename + extension associated with this struct.

You can also access this directly on your type:

disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);

assert!(Data::file_tmp() == Data::FILE_NAME_TMP);
source

fn file_gzip_tmp() -> &'static str

The gzip + tmp variant of the filename + extension associated with this struct.

You can also access this directly on your type:

disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);

assert!(Data::file_gzip_tmp() == Data::FILE_NAME_GZIP_TMP);
source

fn base_path() -> Result<PathBuf, Error>

The base path associated with this struct (PATH leading up to the file).

source

fn absolute_path() -> Result<PathBuf, Error>

The absolute PATH of the file associated with this struct.

source

fn read_to_bytes() -> Result<Vec<u8>, Error>

Read the file directly as bytes.

source

fn read_to_bytes_gzip() -> Result<Vec<u8>, Error>

Read the file directly as bytes, and attempt gzip decompression.

This assumes the file is suffixed with .gz, for example:

config.json    // What `.read_to_bytes()` will look for
config.json.gz // What `.read_to_bytes_gzip()` will look for
source

fn exists_gzip() -> Result<bool, Error>

Same as Self::exists() but checks if the gzip file exists.

  • Self::exists() checks for file.toml.
  • Self::exists_gzip() checks for file.toml.gz.
source

fn from_file() -> Result<Self, Error>

Read the file directly as bytes and turn into a Rust structure.

source

fn from_file_gzip() -> Result<Self, Error>

Read the file directly as bytes, decompress with gzip and turn into a Rust structure.

source

fn save(&self) -> Result<(), Error>

Try saving as a file.

Calling this will automatically create the directories leading up to the file.

source

fn save_gzip(&self) -> Result<(), Error>

Try saving as a compressed file using gzip.

This will suffix the file with .gz, for example:

config.json    // Normal file name with `.save()`
config.json.gz // File name when using `.save_gzip()`

Calling this will automatically create the directories leading up to the file.

source

fn save_atomic(&self) -> Result<(), Error>

Note: This may not truely be atomic on Windows.

Try saving to a TEMPORARY file first, then renaming it to the associated file.

This lowers the chance for data corruption on interrupt.

The temporary file is removed if the rename fails.

The temporary file name is: file_name + extension + .tmp, for example:

config.toml     // <- Real file
config.toml.tmp // <- Temporary version

Already existing .tmp files will be overwritten.

Calling this will automatically create the directories leading up to the file.

source

fn save_atomic_gzip(&self) -> Result<(), Error>

source

fn rm_atomic(&self) -> Result<(), Error>

Note: This may not truely be atomic on Windows.

Rename the associated file before attempting to delete it.

This lowers the chance for data corruption on interrupt.

The temporary file name is: file_name + extension + .tmp, for example:

config.toml     // <- Real file
config.toml.tmp // <- Temporary version

Already existing .tmp files will be overwritten.

source

fn rm_atomic_gzip(&self) -> Result<(), Error>

Same as Self::rm_atomic() but looks for the .gz extension.

source

fn rm_tmp() -> Result<(), Error>

Try deleting any leftover .tmp files from Self::save_atomic() or Self::save_atomic_gzip()

This will return success if the files don’t exist or if deleted.

It will return failure if files existed but could not be deleted or if any other error occurs.

source

fn absolute_path_gzip() -> Result<PathBuf, Error>

The absolute PATH of the file associated with this struct WITH the .gz extension.

source

fn into_writable_fmt(&self) -> Result<Vec<u8>, Error>

Turn Self into bytes that can be written to disk.

source

fn from_bytes(bytes: &[u8]) -> Result<Self, Error>

source

fn to_bytes(&self) -> Result<Vec<u8>, Error>

source

fn header_version_bytes(&self) -> [u8; 25]

Return the 25 bytes header bytes.

First 24 bytes are the Self::HEADER bytes.

Last byte is Self::VERSION.

source

fn file_header_to_string(&self) -> Result<String, Error>

Read the associated file and attempt to convert the first 24 bytes to a String.

This is useful if your Self::HEADER should be bytes representing a UTF-8 string.

source

fn file_version() -> Result<u8, Error>

Reads the first 24 bytes of the associated file and matches it against Self::HEADER.

If the bytes match, the next byte should be our Self::VERSION and is returned.

Note: This only works on a non-compressed version.

source

fn file_bytes(range: Range<u16>) -> Result<Vec<u8>, Error>

Reads a range of bytes of the associated file of Self.

Implementors§