io_lifetimes

Trait AsFilelike

Source
pub trait AsFilelike: AsFd {
    // Required methods
    fn as_filelike(&self) -> BorrowedFilelike<'_>;
    fn as_filelike_view<Target: FilelikeViewType>(
        &self,
    ) -> FilelikeView<'_, Target>;
}
Expand description

A portable trait to borrow a reference from an underlying filelike object.

This is a portability abstraction over Unix-like AsFd and Windows’ AsHandle. It also provides the as_filelike_view convenience function providing typed views.

Required Methods§

Source

fn as_filelike(&self) -> BorrowedFilelike<'_>

Borrows the reference.

§Example
use std::fs::File;
use io_lifetimes::{AsFilelike, BorrowedFilelike};

let mut f = File::open("foo.txt")?;
let borrowed_filelike: BorrowedFilelike<'_> = f.as_filelike();
Source

fn as_filelike_view<Target: FilelikeViewType>(&self) -> FilelikeView<'_, Target>

Return a borrowing view of a resource which dereferences to a &Target.

Note that Read or Write require &mut Target, but in some cases, such as File, Read and Write are implemented for &Target in addition to Target, and you can get a &mut &Target by doing &* on the resuting view, like this:

let v = f.as_filelike_view::<std::fs::File>();
(&*v).read(&mut buf).unwrap();

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: AsFd> AsFilelike for T