add Mmap::assume_init

to convert Mmap<MaybeUninit<T>> to Mmap<T>

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2021-11-22 09:05:13 +01:00
parent ffbf58cad3
commit 9da1062f82

View File

@ -1,6 +1,7 @@
//! Memory mapping helpers.
use std::convert::TryFrom;
use std::mem::MaybeUninit;
use std::os::unix::io::RawFd;
use std::{io, mem, ptr};
@ -87,3 +88,20 @@ impl<'a, T> IntoIterator for &'a Mmap<T> {
<&'a [T] as IntoIterator>::into_iter(self)
}
}
impl<T> Mmap<MaybeUninit<T>> {
/// Converts to `Mmap<T>`.
///
/// # Safety
///
/// It is up to the caller to ensure this is safe, see
/// [`MaybeUninit::assume_init`](std::mem::MaybeUninit::assume_init).
pub unsafe fn assume_init(self) -> Mmap<T> {
let out = Mmap {
data: self.data as *mut T,
len: self.len,
};
std::mem::forget(self);
out
}
}