Struct DataStore

Source
pub struct DataStore<T: DataObject + 'static> { /* private fields */ }
Expand description

Storage that allows lookup and access of DataObjects of a given type

Implementations§

Source§

impl<T: DataObject + 'static> DataStore<T>

Source

pub fn new(source_manager: Rc<RefCell<SourceManager>>) -> DataStore<T>

Constructs a new DataStore that gets its Sources from the given SourceManager

Examples found in repository?
examples/basic.rs (line 61)
40fn main()
41{
42    println!("Listing items in mypackage/text:");
43    
44    let mut source = FilesystemSource::new("examples", TrustLevel::TrustedSource);
45    let iter = source.iter_entries("mypackage", "text");
46    
47    for entry in iter
48    {
49        match entry
50        {
51            Ok(pathname) => println!("{}", pathname),
52            Err(error) => println!("Error: {}", error)
53        }
54    }
55    
56    println!("Loading files from mypackage/text...");
57    
58    let source_manager = Rc::new(RefCell::new(SourceManager::new()));
59    source_manager.borrow_mut().add_source(Box::new(source));
60    
61    let mut store = DataStore::<TextData>::new(source_manager);
62    
63    store.load_package("mypackage").expect("Failed to load package.");
64    
65    for name in &["emoji.txt", "missingfile.txt", "loremipsum.txt"]
66    {
67        match store.get_id("mypackage", name)
68        {
69            Ok(id) =>
70            {
71                println!("Contents of {}:", name);
72                match store.get(id)
73                {
74                    Some(data) => println!("{}", data.text),
75                    None => println!("(not found)"),
76                }
77            }
78            Err(error) =>
79            {
80                println!("Error for {}:\n{}", name, error);
81            }
82        }
83        
84        println!("");
85    }
86}
Source

pub fn load_package( &mut self, package_name: &str, ) -> Result<DataStoreOk, DataError>

Loads the package by the given name if it is not already loaded.

Examples found in repository?
examples/basic.rs (line 63)
40fn main()
41{
42    println!("Listing items in mypackage/text:");
43    
44    let mut source = FilesystemSource::new("examples", TrustLevel::TrustedSource);
45    let iter = source.iter_entries("mypackage", "text");
46    
47    for entry in iter
48    {
49        match entry
50        {
51            Ok(pathname) => println!("{}", pathname),
52            Err(error) => println!("Error: {}", error)
53        }
54    }
55    
56    println!("Loading files from mypackage/text...");
57    
58    let source_manager = Rc::new(RefCell::new(SourceManager::new()));
59    source_manager.borrow_mut().add_source(Box::new(source));
60    
61    let mut store = DataStore::<TextData>::new(source_manager);
62    
63    store.load_package("mypackage").expect("Failed to load package.");
64    
65    for name in &["emoji.txt", "missingfile.txt", "loremipsum.txt"]
66    {
67        match store.get_id("mypackage", name)
68        {
69            Ok(id) =>
70            {
71                println!("Contents of {}:", name);
72                match store.get(id)
73                {
74                    Some(data) => println!("{}", data.text),
75                    None => println!("(not found)"),
76                }
77            }
78            Err(error) =>
79            {
80                println!("Error for {}:\n{}", name, error);
81            }
82        }
83        
84        println!("");
85    }
86}
Source

pub fn get_id( &self, package_name: &str, pathname: &str, ) -> Result<DataId<T>, DataError>

Gets the numeric ID of the DataObject from the given package at the given pathname.

Examples found in repository?
examples/basic.rs (line 67)
40fn main()
41{
42    println!("Listing items in mypackage/text:");
43    
44    let mut source = FilesystemSource::new("examples", TrustLevel::TrustedSource);
45    let iter = source.iter_entries("mypackage", "text");
46    
47    for entry in iter
48    {
49        match entry
50        {
51            Ok(pathname) => println!("{}", pathname),
52            Err(error) => println!("Error: {}", error)
53        }
54    }
55    
56    println!("Loading files from mypackage/text...");
57    
58    let source_manager = Rc::new(RefCell::new(SourceManager::new()));
59    source_manager.borrow_mut().add_source(Box::new(source));
60    
61    let mut store = DataStore::<TextData>::new(source_manager);
62    
63    store.load_package("mypackage").expect("Failed to load package.");
64    
65    for name in &["emoji.txt", "missingfile.txt", "loremipsum.txt"]
66    {
67        match store.get_id("mypackage", name)
68        {
69            Ok(id) =>
70            {
71                println!("Contents of {}:", name);
72                match store.get(id)
73                {
74                    Some(data) => println!("{}", data.text),
75                    None => println!("(not found)"),
76                }
77            }
78            Err(error) =>
79            {
80                println!("Error for {}:\n{}", name, error);
81            }
82        }
83        
84        println!("");
85    }
86}
Source

pub fn get_id_mut( &mut self, package_name: &str, pathname: &str, ) -> Result<DataId<T>, DataError>

Gets the numeric ID of the DataObject from the given package at the given pathname. If the package is not loaded, it will be loaded automatically.

Source

pub fn get(&self, id: DataId<T>) -> Option<&T>

Returns a reference to the DataObject by the given DataId, if one exists. Otherwise returns None.

Examples found in repository?
examples/basic.rs (line 72)
40fn main()
41{
42    println!("Listing items in mypackage/text:");
43    
44    let mut source = FilesystemSource::new("examples", TrustLevel::TrustedSource);
45    let iter = source.iter_entries("mypackage", "text");
46    
47    for entry in iter
48    {
49        match entry
50        {
51            Ok(pathname) => println!("{}", pathname),
52            Err(error) => println!("Error: {}", error)
53        }
54    }
55    
56    println!("Loading files from mypackage/text...");
57    
58    let source_manager = Rc::new(RefCell::new(SourceManager::new()));
59    source_manager.borrow_mut().add_source(Box::new(source));
60    
61    let mut store = DataStore::<TextData>::new(source_manager);
62    
63    store.load_package("mypackage").expect("Failed to load package.");
64    
65    for name in &["emoji.txt", "missingfile.txt", "loremipsum.txt"]
66    {
67        match store.get_id("mypackage", name)
68        {
69            Ok(id) =>
70            {
71                println!("Contents of {}:", name);
72                match store.get(id)
73                {
74                    Some(data) => println!("{}", data.text),
75                    None => println!("(not found)"),
76                }
77            }
78            Err(error) =>
79            {
80                println!("Error for {}:\n{}", name, error);
81            }
82        }
83        
84        println!("");
85    }
86}
Source

pub fn get_mut(&mut self, id: DataId<T>) -> Option<&mut T>

Returns a mutable reference to the DataObject by the given DataId, if one exists. Otherwise returns None.

Source

pub fn list_package_contents(&self, package_name: &str) -> Vec<String>

Returns a string list of the names of each DataObject in the given package. The package will need to have been loaded or this will return an empty list. Please do not call this repeatedly.

Source

pub fn unload_package(&mut self, package_name: &str) -> bool

Unloads the given package from memory. Any DataObjects will be dropped, but pathname-id mappings will be retained in memory so that existing references will not be invalidated. Returns true if the package was loaded.

Source

pub fn unload_all(&mut self)

Unloads all packages from memory. Any DataObjects will be dropped, but pathname-id mappings will be retained in memory so that existing references will not be invalidated. Returns true if the package was loaded.

Source

pub fn unload_unused(&mut self)

Unloads all packages that do not have an active PackageUseToken.

Source

pub fn package_loaded(&self, package_name: &str) -> bool

Returns true if the given package is loaded.

Source

pub fn package_use_token( &self, package_name: &str, ) -> Option<PackageUseToken<T>>

Returns a PackageUseToken that can be used to keep the given package from being unloaded automatically.

Source

pub fn save( &mut self, package_name: &str, pathname: &str, source_id: SourceId, ) -> Result<(), DataError>

Saves the indicated item using the Source given by SourceId.

Source

pub fn reprepare(&mut self, id: DataId<T>) -> bool

Marks the DataObject by the given DataId as needing to be “reprepared”. Use this, for example, when data has been changed and it needs to be reflected in a backend via the PreparedStore. Returns true if the data was marked for reprepare, otherwise false.

Source

pub fn reload( &mut self, package_name: &str, pathname: &str, ) -> Result<(), DataError>

Reloads an already-loaded resource by the given name.

Source

pub fn load_direct( &self, package_name: &str, pathname: &str, source_id: SourceId, ) -> Result<T, DataError>

Loads a single DataObject from the given source and returns it. This function does not index, cache, or track the loaded object in any way. Loading the object through here will result in a separate copy in memory even if it was previously loaded through the usual package loading mechanism. For most cases you should use load_package() or get_id_mut() instead.

Auto Trait Implementations§

§

impl<T> Freeze for DataStore<T>

§

impl<T> !RefUnwindSafe for DataStore<T>

§

impl<T> !Send for DataStore<T>

§

impl<T> !Sync for DataStore<T>

§

impl<T> Unpin for DataStore<T>
where T: Unpin,

§

impl<T> !UnwindSafe for DataStore<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.