Replace Option with enum of presence

This commit is contained in:
Marcel Müller 2016-01-17 15:09:10 +01:00
parent cfc6e860b1
commit 6ec2b02792
No known key found for this signature in database
GPG key ID: DD4ED37D0CAC76E2

View file

@ -62,11 +62,17 @@ impl<ISI: IntoStoreId> IntoStoreId for (ISI, ISI) {
} }
} }
#[derive(PartialEq)]
enum StoreEntryPresence {
Present,
Borrowed
}
/// A store entry, depending on the option type it is either borrowed currently /// A store entry, depending on the option type it is either borrowed currently
/// or not. /// or not.
struct StoreEntry { struct StoreEntry {
file: File, file: File,
entry: Option<Entry>, entry: StoreEntryPresence
} }
@ -74,13 +80,11 @@ impl StoreEntry {
/// The entry is currently borrowed, meaning that some thread is currently /// The entry is currently borrowed, meaning that some thread is currently
/// mutating it /// mutating it
fn is_borrowed(&self) -> bool { fn is_borrowed(&self) -> bool {
self.entry.is_none() self.entry == StoreEntryPresence::Borrowed
} }
/// Set the entry to the given one. This will also overwrite the entry /// Flush the entry to disk
/// and flush it to disk
fn set_entry(&mut self, entry: Entry) -> Result<()> { fn set_entry(&mut self, entry: Entry) -> Result<()> {
self.entry = Some(entry);
unimplemented!() unimplemented!()
} }
@ -88,6 +92,11 @@ impl StoreEntry {
fn get_entry(&mut self) -> Result<Entry> { fn get_entry(&mut self) -> Result<Entry> {
unimplemented!() unimplemented!()
} }
/// We copy the entry
fn copy_entry(&mut self) -> Result<Entry> {
unimplemented!()
}
} }
/// The Store itself, through this object one can interact with IMAG's entries /// The Store itself, through this object one can interact with IMAG's entries
@ -118,6 +127,15 @@ impl Store {
/// Return the `FileLockEntry` and write to disk /// Return the `FileLockEntry` and write to disk
pub fn update<'a>(&'a self, entry: FileLockEntry<'a>) -> Result<()> { pub fn update<'a>(&'a self, entry: FileLockEntry<'a>) -> Result<()> {
self._update(&entry)
}
/// Internal method to write to the filesystem store.
///
/// # Assumptions
/// This method assumes that entry is dropped _right after_ the call, hence
/// it is not public.
fn _update<'a>(&'a self, entry: &FileLockEntry<'a>) -> Result<()> {
unimplemented!(); unimplemented!();
} }
@ -180,8 +198,7 @@ impl<'a> ::std::ops::DerefMut for FileLockEntry<'a> {
impl<'a> Drop for FileLockEntry<'a> { impl<'a> Drop for FileLockEntry<'a> {
fn drop(&mut self) { fn drop(&mut self) {
let mut map = self.store.entries.write().unwrap(); self.store._update(self).unwrap()
map.get_mut(&self.key).unwrap().set_entry(self.entry.clone());
} }
} }