Struct async_file_lock::FileLock[][src]

pub struct FileLock { /* fields omitted */ }

Locks a file asynchronously. Auto locks a file if any read or write methods are called. If Self::lock_exclusive or Self::lock_shared has been called then the file will stay locked. Can auto seek to specified location before doing any read/write operation.

Note 1: Do not attempt to have multiple file handles for the same file. Because locking is done per process basis. Note 2: Remember to open a file with specified read and/or write mode as write calls will just be ignored if the file is opened in read mode.

Implementations

impl FileLock[src]

pub async fn create(path: impl AsRef<Path>) -> Result<FileLock>[src]

Opens a file in read and write mode that is unlocked.

pub async fn open(path: impl AsRef<Path>) -> Result<FileLock>[src]

Attempts to open a file in read and write mode that is unlocked.

pub async fn new_tokio(tokio_file: File) -> FileLock[src]

Creates a new 'FileLock' from tokio::fs::File.

pub fn new_std(std_file: File) -> FileLock[src]

Creates a new 'FileLock' from std::fs::File.

pub fn lock_exclusive(&mut self) -> LockFuture<'_>

Notable traits for LockFuture<'a>

impl<'a> Future for LockFuture<'a> type Output = Result<()>;
[src]

Locks the file for reading and writing until Self::unlock is called.

pub fn try_lock_exclusive(&mut self) -> Result<()>[src]

Locks the file for reading and writing until Self::unlock is called. Returns an error if the file is currently locked.

pub fn lock_shared(&mut self) -> LockFuture<'_>

Notable traits for LockFuture<'a>

impl<'a> Future for LockFuture<'a> type Output = Result<()>;
[src]

Locks the file for reading until Self::unlock is called.

pub fn try_lock_shared(&mut self) -> Result<()>[src]

Locks the file for reading until Self::unlock is called. Returns an error if the file is currently locked.

pub fn unlock(&mut self) -> UnlockFuture<'_>

Notable traits for UnlockFuture<'a>

impl<'a> Future for UnlockFuture<'a> type Output = ();
[src]

Unlocks the file.

pub fn set_seeking_mode(&mut self, mode: SeekFrom)[src]

Sets auto seeking mode. File will always seek to specified location before doing any read/write operation.

pub fn seeking_mode(&self) -> SeekFrom[src]

pub async fn sync_all(&mut self) -> Result<()>[src]

Attempts to sync all OS-internal metadata to disk.

This function will attempt to ensure that all in-core data reaches the filesystem before returning.

Examples

use tokio::fs::File;
use tokio::prelude::*;

let mut file = File::create("foo.txt").await?;
file.write_all(b"hello, world!").await?;
file.sync_all().await?;

The write_all method is defined on the AsyncWriteExt trait.

pub async fn sync_data(&mut self) -> Result<()>[src]

This function is similar to sync_all, except that it may not synchronize file metadata to the filesystem.

This is intended for use cases that must synchronize content, but don't need the metadata on disk. The goal of this method is to reduce disk operations.

Note that some platforms may simply implement this in terms of sync_all.

Examples

use tokio::fs::File;
use tokio::prelude::*;

let mut file = File::create("foo.txt").await?;
file.write_all(b"hello, world!").await?;
file.sync_data().await?;

The write_all method is defined on the AsyncWriteExt trait.

pub fn get_ref(&self) -> (Option<&File>, Option<&File>)[src]

Gets a reference to the file.

If the file is locked it will be in the second element of a tuple as tokio::fs::File otherwise it will be in the first element as std::fs::File. It is inadvisable to directly read/write from/to the file.

pub fn get_mut(&mut self) -> (Option<&mut File>, Option<&mut File>)[src]

Gets a mutable reference to the file.

If the file is locked it will be in the second element of a tuple as tokio::fs::File otherwise it will be in the first element as std::fs::File. It is inadvisable to directly read/write from/to the file.

Trait Implementations

impl AsyncRead for FileLock[src]

impl AsyncSeek for FileLock[src]

impl AsyncWrite for FileLock[src]

impl Debug for FileLock[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<R> AsyncReadExt for R where
    R: AsyncRead + ?Sized
[src]

impl<S> AsyncSeekExt for S where
    S: AsyncSeek + ?Sized
[src]

impl<W> AsyncWriteExt for W where
    W: AsyncWrite + ?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, 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.