Struct io_partition::PartitionMutex[][src]

pub struct PartitionMutex<T: Read + Seek> { /* fields omitted */ }

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]

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]

impl<T: Debug + Read + Seek> Debug for PartitionMutex<T>[src]

impl<T: Read + Seek> Read for PartitionMutex<T>[src]

impl<T: Read + Seek> Seek for PartitionMutex<T>[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

Auto Trait Implementations

impl<T> RefUnwindSafe for PartitionMutex<T>[src]

impl<T> Send for PartitionMutex<T> where
    T: Send
[src]

impl<T> Sync for PartitionMutex<T> where
    T: Send
[src]

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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.