pub struct File<'a> { /* private fields */ }
Expand description
The root of a GVDB file
§Examples
Load a GResource file from disk
use std::path::PathBuf;
use serde::Deserialize;
use gvdb::read::File;
let path = PathBuf::from("test-data/test3.gresource");
let file = File::from_file(&path).unwrap();
let table = file.hash_table().unwrap();
#[derive(serde::Deserialize, zvariant::Type, zvariant::OwnedValue)]
struct SvgData {
size: u32,
flags: u32,
content: Vec<u8>
}
let svg: SvgData = table
.get("/gvdb/rs/test/online-symbolic.svg")
.unwrap();
let svg_str = std::str::from_utf8(&svg.content).unwrap();
println!("{}", svg_str);
Query the root hash table
use gvdb::read::File;
fn query_hash_table(file: File) {
let table = file.hash_table().unwrap();
let mut keys = table.keys();
assert_eq!(keys.len(), 2);
assert_matches!(keys.next().unwrap().as_deref(), Ok("string"));
assert_matches!(keys.next().unwrap().as_deref(), Ok("table"));
let str_value: String = table.get("string").unwrap();
assert_eq!(str_value, "test string");
let mut items = table.values().collect::<Result<Vec<_>, _>>().unwrap();
assert_eq!(items.len(), 2);
assert_eq!(String::try_from(&items[0]).unwrap(), "test string");
let sub_table = table.get_hash_table("table").unwrap();
let mut sub_table_keys = sub_table.keys().collect::<Result<Vec<_>, _>>().unwrap();
assert_eq!(sub_table_keys.len(), 1);
assert_eq!(sub_table_keys[0], "int");
let int_value: u32 = sub_table.get("int").unwrap();
assert_eq!(int_value, 42);
}
Implementations§
Source§impl<'a> File<'a>
impl<'a> File<'a>
Sourcepub fn hash_table(&self) -> Result<HashTable<'_, '_>>
pub fn hash_table(&self) -> Result<HashTable<'_, '_>>
Returns the root hash table of the file
Sourcepub fn from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self>
pub fn from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self>
Interpret a slice of bytes as a GVDB file
Sourcepub fn from_file(filename: &Path) -> Result<Self>
pub fn from_file(filename: &Path) -> Result<Self>
Open a file and interpret the data as GVDB
let path = std::path::PathBuf::from("test-data/test3.gresource");
let file = gvdb::read::File::from_file(&path).unwrap();
Sourcepub unsafe fn from_file_mmap(filename: &Path) -> Result<Self>
pub unsafe fn from_file_mmap(filename: &Path) -> Result<Self>
Open a file and mmap
it into memory.
§Safety
This is marked unsafe as the file could be modified on-disk while the mmap is active. This will cause undefined behavior. You must make sure to employ your own locking and to reload the file yourself when any modification occurs.
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for File<'a>
impl<'a> RefUnwindSafe for File<'a>
impl<'a> Send for File<'a>
impl<'a> Sync for File<'a>
impl<'a> Unpin for File<'a>
impl<'a> UnwindSafe for File<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more