Struct io_partition::PartitionMutex [−][src]
A PartitionMutex
allow you to refer to a part of the file. It consume the input file.
As the input file is an Arc<Mutex<_>>
, multiple PartitionMutex
can be created by file, and PartitionMutex
can be cloned.
The input offset is the first byte that will be accessible. The user of the PartitionMutex
won't be able to seek before it, and it will be considered the offset 0 of the PartitionMutex
The input lenght is the number of byte that can be read with this PartitionMutex
. The last readable byte from the input file is input_offset + input_len
It is possible to lock the mutex with PartitionMutex::lock
. You need to take care when using it, or a panic may occur. Please read the documentation of the function.
Examples
use std::io::{Cursor, Read, Seek, SeekFrom}; use io_partition::PartitionMutex; use std::sync::{Mutex, Arc}; use std::thread; let mut some_value = (0..100).collect::<Vec<u8>>(); let some_file = Arc::new(Mutex::new(Cursor::new(some_value))); let mut first_partition = PartitionMutex::new(some_file.clone(), 10, 20).unwrap(); let mut second_partition = PartitionMutex::new(some_file.clone(), 40, 30).unwrap(); let mut buf = [0]; first_partition.seek(SeekFrom::Start(10)).unwrap(); second_partition.seek(SeekFrom::Start(5)).unwrap(); first_partition.read_exact(&mut buf).unwrap(); assert_eq!(buf, [20]); second_partition.read_exact(&mut buf).unwrap(); assert_eq!(buf, [45]); second_partition.seek(SeekFrom::Start(5)).unwrap(); let mut second_clone = second_partition.clone(); let handle = thread::spawn(move || { second_clone.seek(SeekFrom::Current(2)).unwrap(); let mut buf = [0]; second_clone.read_exact(&mut buf).unwrap(); buf[0] }); second_partition.seek(SeekFrom::End(-1)).unwrap(); second_partition.read_exact(&mut buf).unwrap(); assert_eq!(handle.join().unwrap(), 47); assert_eq!(buf[0], 69); first_partition.seek(SeekFrom::Start(2)).unwrap(); { let mut locked = first_partition.lock().unwrap(); let mut buffer = [0; 2]; locked.read_exact(&mut buffer); assert_eq!(&buffer, &[12, 13]); }
Implementations
impl<T: Read + Seek> PartitionMutex<T>
[src]
pub fn new(
file: Arc<Mutex<T>>,
start: u64,
lenght: u64
) -> Result<PartitionMutex<T>>
[src]
file: Arc<Mutex<T>>,
start: u64,
lenght: u64
) -> Result<PartitionMutex<T>>
Create new PartitionMutex
, with the specified input file, start and lenght
pub fn lock(&mut self) -> Result<PartitionMutexLock<'_, T>, LockPartitionError>
[src]
Lock this PartitionMutex
, in a similar fashion to Mutex::lock
.
If the same thread lock this PartitionMutex
, or any other structure that use the same file, without first checking it is free, it might panic or softlock.
Note that the seek/read implementation of PartitionMutex
will lock the file for the duration of those function execution.
you can use scope for this.
Trait Implementations
impl<T: Clone + Read + Seek> Clone for PartitionMutex<T>
[src]
fn clone(&self) -> PartitionMutex<T>ⓘNotable traits for PartitionMutex<T>
impl<T: Read + Seek> Read for PartitionMutex<T>impl<T: Read + Seek> Write for PartitionMutex<T>
[src]
Notable traits for PartitionMutex<T>
impl<T: Read + Seek> Read for PartitionMutex<T>impl<T: Read + Seek> Write for PartitionMutex<T>
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<T: Debug + Read + Seek> Debug for PartitionMutex<T>
[src]
impl<T: Read + Seek> Read for PartitionMutex<T>
[src]
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
[src]
pub fn read_vectored(
&mut self,
bufs: &mut [IoSliceMut<'_>]
) -> Result<usize, Error>
1.36.0[src]
&mut self,
bufs: &mut [IoSliceMut<'_>]
) -> Result<usize, Error>
pub fn is_read_vectored(&self) -> bool
[src]
pub unsafe fn initializer(&self) -> Initializer
[src]
pub fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
1.0.0[src]
pub fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
pub fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
pub fn by_ref(&mut self) -> &mut Self
1.0.0[src]
pub fn bytes(self) -> Bytes<Self>
1.0.0[src]
pub fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0[src]
R: Read,
pub fn take(self, limit: u64) -> Take<Self>
1.0.0[src]
impl<T: Read + Seek> Seek for PartitionMutex<T>
[src]
fn seek(&mut self, target: SeekFrom) -> Result<u64>
[src]
pub fn stream_len(&mut self) -> Result<u64, Error>
[src]
pub fn stream_position(&mut self) -> Result<u64, Error>
1.51.0[src]
impl<T: Read + Seek> Write for PartitionMutex<T>
[src]
fn write(&mut self, _: &[u8]) -> Result<usize>
[src]
Do not use this write function. It will always fail. It is just here because some library require this to have the Write
trait to make this work with this (rust_vfs)
fn flush(&mut self) -> Result<()>
[src]
Always suceed. It is useless to call it
pub fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>
1.36.0[src]
pub fn is_write_vectored(&self) -> bool
[src]
pub fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]
pub fn write_all_vectored(
&mut self,
bufs: &mut [IoSlice<'_>]
) -> Result<(), Error>
[src]
&mut self,
bufs: &mut [IoSlice<'_>]
) -> Result<(), Error>
pub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>
1.0.0[src]
pub fn by_ref(&mut self) -> &mut Self
1.0.0[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for PartitionMutex<T>
[src]
impl<T> Send for PartitionMutex<T> where
T: Send,
[src]
T: Send,
impl<T> Sync for PartitionMutex<T> where
T: Send,
[src]
T: Send,
impl<T> Unpin for PartitionMutex<T>
[src]
impl<T> UnwindSafe for PartitionMutex<T>
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,