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>
impl<T: DataObject + 'static> DataStore<T>
Sourcepub fn new(source_manager: Rc<RefCell<SourceManager>>) -> DataStore<T>
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?
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}
Sourcepub fn load_package(
&mut self,
package_name: &str,
) -> Result<DataStoreOk, DataError>
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?
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}
Sourcepub fn get_id(
&self,
package_name: &str,
pathname: &str,
) -> Result<DataId<T>, DataError>
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?
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}
Sourcepub fn get_id_mut(
&mut self,
package_name: &str,
pathname: &str,
) -> Result<DataId<T>, DataError>
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.
Sourcepub fn get(&self, id: DataId<T>) -> Option<&T>
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?
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}
Sourcepub fn get_mut(&mut self, id: DataId<T>) -> Option<&mut T>
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.
Sourcepub fn list_package_contents(&self, package_name: &str) -> Vec<String>
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.
Sourcepub fn unload_package(&mut self, package_name: &str) -> bool
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.
Sourcepub fn unload_all(&mut self)
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.
Sourcepub fn unload_unused(&mut self)
pub fn unload_unused(&mut self)
Unloads all packages that do not have an active PackageUseToken.
Sourcepub fn package_loaded(&self, package_name: &str) -> bool
pub fn package_loaded(&self, package_name: &str) -> bool
Returns true if the given package is loaded.
Sourcepub fn package_use_token(
&self,
package_name: &str,
) -> Option<PackageUseToken<T>>
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.
Sourcepub fn save(
&mut self,
package_name: &str,
pathname: &str,
source_id: SourceId,
) -> Result<(), DataError>
pub fn save( &mut self, package_name: &str, pathname: &str, source_id: SourceId, ) -> Result<(), DataError>
Sourcepub fn reprepare(&mut self, id: DataId<T>) -> bool
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.
Sourcepub fn reload(
&mut self,
package_name: &str,
pathname: &str,
) -> Result<(), DataError>
pub fn reload( &mut self, package_name: &str, pathname: &str, ) -> Result<(), DataError>
Reloads an already-loaded resource by the given name.
Sourcepub fn load_direct(
&self,
package_name: &str,
pathname: &str,
source_id: SourceId,
) -> Result<T, DataError>
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> 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
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.