diff options
Diffstat (limited to 'src/directory/mod.rs')
-rw-r--r-- | src/directory/mod.rs | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/src/directory/mod.rs b/src/directory/mod.rs index 70fa013..294beb9 100644 --- a/src/directory/mod.rs +++ b/src/directory/mod.rs @@ -9,6 +9,7 @@ mod mmap_directory; mod directory; mod directory_lock; +mod footer; mod managed_directory; mod ram_directory; mod read_only_source; @@ -24,18 +25,49 @@ pub use self::ram_directory::RAMDirectory; pub use self::read_only_source::ReadOnlySource; pub(crate) use self::watch_event_router::WatchCallbackList; pub use self::watch_event_router::{WatchCallback, WatchHandle}; -use std::io::{BufWriter, Write}; +use std::io::{self, BufWriter, Write}; #[cfg(feature = "mmap")] pub use self::mmap_directory::MmapDirectory; pub use self::managed_directory::ManagedDirectory; +/// Struct used to prevent from calling [`terminate_ref`](trait.TerminatingWrite#method.terminate_ref) directly +pub struct AntiCallToken(()); + +/// Trait used to indicate when no more write need to be done on a writer +pub trait TerminatingWrite: Write { + /// Indicate that the writer will no longer be used. Internally call terminate_ref. + fn terminate(mut self) -> io::Result<()> + where + Self: Sized, + { + self.terminate_ref(AntiCallToken(())) + } + + /// You should implement this function to define custom behavior. + /// This function should flush any buffer it may hold. + fn terminate_ref(&mut self, _: AntiCallToken) -> io::Result<()>; +} + +impl<W: TerminatingWrite + ?Sized> TerminatingWrite for Box<W> { + fn terminate_ref(&mut self, token: AntiCallToken) -> io::Result<()> { + self.as_mut().terminate_ref(token) + } +} + +impl<W: TerminatingWrite> TerminatingWrite for BufWriter<W> { + fn terminate_ref(&mut self, a: AntiCallToken) -> io::Result<()> { + self.flush()?; + self.get_mut().terminate_ref(a) + } +} + /// Write object for Directory. /// /// `WritePtr` are required to implement both Write /// and Seek. -pub type WritePtr = BufWriter<Box<dyn Write>>; +pub type WritePtr = BufWriter<Box<dyn TerminatingWrite>>; #[cfg(test)] mod tests; |