use std::{ io::{Read, Seek, Write}, sync::{Arc, Mutex}, }; use super::VFile; pub struct Shared { inner: Arc>, } impl Clone for Shared { fn clone(&self) -> Self { Self { inner: self.inner.clone(), } } } impl Shared { pub fn new(v: V) -> Self { Self { inner: Arc::new(Mutex::new(v)), } } pub fn try_into_inner(self) -> Result { Arc::try_unwrap(self.inner) .map(|x| x.into_inner().unwrap()) .map_err(|e| Self { inner: e }) } } impl Shared { pub fn into_inner(self) -> V { Arc::try_unwrap(self.inner) .map(|x| x.into_inner().unwrap()) .unwrap_or_else(|x| x.lock().unwrap().clone()) } } impl Write for Shared { fn write(&mut self, buf: &[u8]) -> std::io::Result { self.inner.lock().unwrap().write(buf) } fn flush(&mut self) -> std::io::Result<()> { self.inner.lock().unwrap().flush() } } impl Read for Shared { fn read(&mut self, buf: &mut [u8]) -> std::io::Result { self.inner.lock().unwrap().read(buf) } } impl Seek for Shared { fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result { self.inner.lock().unwrap().seek(pos) } } impl VFile for Shared {}