pub struct MappedFile { /* private fields */ }Expand description
A file’s bytes in array storage: on a Mac with a GPU, the file is memory-mapped and wrapped as GPU-visible memory without a copy, so arrays made from it are views whose pages are read when first touched (by the CPU or the GPU). Elsewhere the file is read into memory. The mapping lives as long as any array made from it.
use nuvec_core::{DType, MappedFile};
let path = std::env::temp_dir().join("nuvec-mapped-file-doc.bin");
let bytes: Vec<u8> = [1.0f32, 2.0, 3.0, 4.0].iter().flat_map(|v| v.to_le_bytes()).collect();
std::fs::write(&path, &bytes).unwrap();
let file = MappedFile::open(&path)?;
let a = file.array(4, DType::F32, &[3])?; // bytes 4..16, no copy
assert_eq!(a.to_vec::<f32>()?, vec![2.0, 3.0, 4.0]);Implementations§
Source§impl MappedFile
impl MappedFile
Sourcepub fn section(&self, offset: usize, len: usize) -> Result<Self>
pub fn section(&self, offset: usize, len: usize) -> Result<Self>
Bytes offset..offset + len as storage of their own: one copy into an aligned buffer,
for files whose arrays are not aligned to their element sizes (safetensors written
without header padding, as MLX writes them). Arrays made from the copy are views of
it, and the file’s mapping is not kept.
Trait Implementations§
Source§impl Clone for MappedFile
impl Clone for MappedFile
Auto Trait Implementations§
impl !RefUnwindSafe for MappedFile
impl !UnwindSafe for MappedFile
impl Freeze for MappedFile
impl Send for MappedFile
impl Sync for MappedFile
impl Unpin for MappedFile
impl UnsafeUnpin for MappedFile
Blanket Implementations§
impl<T> AutoreleaseSafe for Twhere
T: ?Sized,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more