Added fd-aliasing for Un/ManagedFD

Fortune for mapped-file's current commit: Great blessing − 大吉
master
Avril 2 years ago
parent 0a146fb14a
commit 6b5c791880
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -2,7 +2,7 @@
name = "mapped-file"
description = "Construct a memory mapping over any file object"
keywords = ["unix", "mmap", "generic", "file", "fd"]
version = "0.0.2"
version = "0.0.3"
edition = "2021"
repository="https://github.com/notflan/mapped-file"
license="MIT"

@ -24,6 +24,8 @@ impl Clone for ManagedFD {
}
}
//TODO: io::Read/io::Write impls for ManagedFD
impl ManagedFD
{
#[inline]
@ -32,6 +34,18 @@ impl ManagedFD
Self(UnmanagedFD::new_unchecked(fd))
}
/// Duplicate a file-descriptor, aliasing the open resource for the lifetime of the returned `ManagedFD`..
#[inline]
pub fn alias(file: &(impl AsRawFd + ?Sized)) -> io::Result<Self>
{
let r = unsafe { libc::dup(file.as_raw_fd()) };
if let Some(r) = UnmanagedFD::new_raw(r) {
Ok(Self(r))
} else {
Err(io::Error::last_os_error())
}
}
#[inline]
pub const fn take_raw(fd: RawFd) -> Self
{
@ -101,6 +115,3 @@ impl From<ManagedFD> for std::fs::File
}
}
}
//TODO: implement the rest of ManagedFD from `memfd` module in `utf8encode`

@ -234,4 +234,4 @@ impl From<MemoryFile> for std::fs::File
}
}
//TODO: implement `memfd` from `utf8encode`.
//TODO: io::Read/io::Write impls for MemoryFile

@ -79,4 +79,12 @@ impl AsRawFd for UnmanagedFD
}
}
impl From<UnmanagedFD> for ManagedFD {
#[inline]
fn from(from: UnmanagedFD) -> Self {
Self::take(unsafe { UnmanagedFD::new_unchecked( c_try!(libc::dup(from.get()) => if |x| x < 0; "dup(): failed to duplicate file descriptor {}", from.get()) ) })
}
}
//TODO: implement a full version of the temporary struct `UnmanagedFD` from `utf8encode`

Loading…
Cancel
Save