pub struct MmapArray<T: Copy + Send + Sync + 'static> { /* private fields */ }Expand description
A generic memory-mapped array backed by a file.
MmapArray<T> provides zero-copy access to array data stored in a file,
using the operating system’s virtual memory system to page data in and out
as needed.
§Type Requirements
T must be Copy + Send + Sync + 'static to ensure safe memory-mapped access.
The type must also have a fixed, well-defined memory layout (no padding concerns
for single elements).
§Thread Safety
MmapArray is Send + Sync and can be shared across threads. However,
concurrent mutation (with ReadWrite mode) requires external synchronization.
Implementations§
Source§impl<T: Copy + Send + Sync + 'static> MmapArray<T>
impl<T: Copy + Send + Sync + 'static> MmapArray<T>
Sourcepub fn from_slice(data: &[T], path: &Path, mode: MmapMode) -> CoreResult<Self>
pub fn from_slice(data: &[T], path: &Path, mode: MmapMode) -> CoreResult<Self>
Create a new MmapArray from a slice of data, writing it to the specified path.
This creates a new file (or truncates an existing one) and writes the header and data to it.
Sourcepub fn open(path: &Path, mode: MmapMode) -> CoreResult<Self>
pub fn open(path: &Path, mode: MmapMode) -> CoreResult<Self>
Open an existing MmapArray file.
The file must have been created by MmapArray::from_slice or
MmapArray::from_ndarray. The type parameter T must match the
type used when the file was created.
Sourcepub fn from_ndarray(
array: &Array1<T>,
path: &Path,
mode: MmapMode,
) -> CoreResult<Self>
pub fn from_ndarray( array: &Array1<T>, path: &Path, mode: MmapMode, ) -> CoreResult<Self>
Create a new MmapArray from an ndarray Array1.
Sourcepub fn data_size_bytes(&self) -> usize
pub fn data_size_bytes(&self) -> usize
Get the size in bytes of the data portion (excluding header)
Sourcepub fn file_size_bytes(&self) -> usize
pub fn file_size_bytes(&self) -> usize
Get the total file size in bytes (header + data)
Sourcepub fn as_slice(&self) -> CoreResult<&[T]>
pub fn as_slice(&self) -> CoreResult<&[T]>
Get a zero-copy immutable view of the data as a slice.
For COW mode, if the buffer has been materialized (i.e., a write occurred), this returns the COW buffer instead of the mmap data.
Sourcepub fn as_slice_mut(&mut self) -> CoreResult<&mut [T]>
pub fn as_slice_mut(&mut self) -> CoreResult<&mut [T]>
Get a zero-copy mutable view of the data as a mutable slice.
§Errors
Returns an error if the array is in ReadOnly mode.
For COW mode, the first call to this method materializes the COW buffer by copying the mapped data. Subsequent calls return the COW buffer directly.
Sourcepub fn get(&self, index: usize) -> CoreResult<T>
pub fn get(&self, index: usize) -> CoreResult<T>
Get a single element by index.
Sourcepub fn set(&mut self, index: usize, value: T) -> CoreResult<()>
pub fn set(&mut self, index: usize, value: T) -> CoreResult<()>
Set a single element by index.
§Errors
Returns an error if the array is in ReadOnly mode or the index is out of bounds.
Sourcepub fn to_ndarray(&self) -> CoreResult<Array1<T>>
pub fn to_ndarray(&self) -> CoreResult<Array1<T>>
Convert to an ndarray Array1 by copying the data.
Sourcepub fn as_ndarray_view(&self) -> CoreResult<ArrayView1<'_, T>>
pub fn as_ndarray_view(&self) -> CoreResult<ArrayView1<'_, T>>
Convert to an ndarray ArrayView1 (zero-copy).
The returned view borrows from this MmapArray.
Sourcepub fn flush(&self) -> CoreResult<()>
pub fn flush(&self) -> CoreResult<()>
Flush changes to disk (only meaningful for ReadWrite mode).
Sourcepub fn flush_async(&self) -> CoreResult<()>
pub fn flush_async(&self) -> CoreResult<()>
Flush changes asynchronously (non-blocking).
Sourcepub fn view(&self, start: usize, len: usize) -> CoreResult<&[T]>
pub fn view(&self, start: usize, len: usize) -> CoreResult<&[T]>
Create a read-only view of a subrange of the array (zero-copy).
Sourcepub fn iter(&self) -> CoreResult<Iter<'_, T>>
pub fn iter(&self) -> CoreResult<Iter<'_, T>>
Iterate over the elements of the array.
Sourcepub fn as_ptr(&self) -> CoreResult<*const T>
pub fn as_ptr(&self) -> CoreResult<*const T>
Get raw pointer to the data (for advanced use).
§Safety
The returned pointer is only valid while self is alive and the
underlying memory map is not remapped.
Sourcepub fn map<U, F>(&self, f: F) -> CoreResult<Vec<U>>where
F: Fn(T) -> U,
pub fn map<U, F>(&self, f: F) -> CoreResult<Vec<U>>where
F: Fn(T) -> U,
Apply a function to each element and return a new Vec.
Sourcepub fn chunked_iter(
&self,
chunk_size: usize,
) -> CoreResult<MmapChunkIter<'_, T>>
pub fn chunked_iter( &self, chunk_size: usize, ) -> CoreResult<MmapChunkIter<'_, T>>
Apply a function to chunks of elements. Useful for processing large arrays without loading everything at once.
Trait Implementations§
impl<T: Copy + Send + Sync + 'static> Send for MmapArray<T>
impl<T: Copy + Send + Sync + 'static> Sync for MmapArray<T>
Auto Trait Implementations§
impl<T> !Freeze for MmapArray<T>
impl<T> RefUnwindSafe for MmapArray<T>where
T: RefUnwindSafe,
impl<T> Unpin for MmapArray<T>where
T: Unpin,
impl<T> UnsafeUnpin for MmapArray<T>
impl<T> UnwindSafe for MmapArray<T>where
T: UnwindSafe,
Blanket Implementations§
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
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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>
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>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
Source§fn lossless_try_into(self) -> Option<Dst>
fn lossless_try_into(self) -> Option<Dst>
Source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
Source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Source§impl<T> StrictAs for T
impl<T> StrictAs for T
Source§fn strict_as<Dst>(self) -> Dstwhere
T: StrictCast<Dst>,
fn strict_as<Dst>(self) -> Dstwhere
T: StrictCast<Dst>,
Source§impl<Src, Dst> StrictCastFrom<Src> for Dstwhere
Src: StrictCast<Dst>,
impl<Src, Dst> StrictCastFrom<Src> for Dstwhere
Src: StrictCast<Dst>,
Source§fn strict_cast_from(src: Src) -> Dst
fn strict_cast_from(src: Src) -> Dst
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.