Trait io_lifetimes::AsFilelike
source · [−]pub trait AsFilelike: AsFd {
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
fn as_filelike(&self) -> BorrowedFilelike<'_>
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();
fn as_filelike_view<Target: FilelikeViewType>(&self) -> FilelikeView<'_, Target>
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();