Skip to main content

MmapWriter

Struct MmapWriter 

Source
pub struct MmapWriter { /* private fields */ }
Expand description

Memory-mapped MRC writer (requires mmap feature). Memory-mapped MRC file writer.

Writes data directly into a memory-mapped region, letting the OS handle paging and flushing. This is efficient for large files and random-access modifications, but requires the mmap feature.

§Example

use mrc::{create, VoxelBlock};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut writer = create("output.mrc")
        .shape([512, 512, 256])
        .mode::<f32>()
        .finish_mmap()?;

    writer.write_block(&VoxelBlock::new(
        [0, 0, 0], [512, 512, 1],
        vec![0.0f32; 512 * 512],
    )?)?;
    Ok(())
}

For most use cases, prefer creating via WriterBuilder::finish_mmap.

Implementations§

Source§

impl MmapWriter

Source

pub fn shape(&self) -> VolumeShape

Volume dimensions for this writer.

Source

pub fn mode(&self) -> Mode

Voxel data mode for this writer.

Falls back to Mode::Float32 if the header mode value is not recognised.

Source

pub fn header(&self) -> &Header

Reference to the current header.

Modify header fields before calling finalize to change what gets written to disk.

Source

pub fn write_u8_block(&mut self, block: &VoxelBlock<u8>) -> Result<(), Error>

Write a block of u8 data by automatically widening to u16 (Mode 6).

The file must have been created with Mode::Uint16. Each u8 voxel is widened to u16 before writing, matching Python mrcfile’s auto-conversion behaviour for np.uint8 data.

§Errors

Returns Error::ModeMismatch if the file mode is not Uint16.

Source

pub fn write_block<T: Voxel>( &mut self, block: &VoxelBlock<T>, ) -> Result<(), Error>

Write a block of voxels to the memory-mapped file.

The type T must match the file’s voxel mode exactly. Supports arbitrary sub-blocks by scattering row-by-row when necessary.

Source

pub fn write_block_parallel<T: Voxel>( &mut self, block: &VoxelBlock<T>, ) -> Result<(), Error>

Write a block with parallel encoding to memory-mapped region.

For non-contiguous blocks (sub-XY slabs), this falls back to the serial write_block implementation.

Source

pub fn write_f16_from_f32( &mut self, block: &VoxelBlock<f32>, ) -> Result<(), Error>

Write an f32 block to a Float16 file.

Source

pub fn update_header_stats(&mut self) -> Result<(), Error>

Scan the written data block and update header statistics.

Unlike Writer::update_header_stats, this does not need to read from disk because the data is already accessible via the memory map.

Source§

impl MmapWriter

Source

pub fn finalize(&mut self) -> Result<(), Error>

Finalize the memory-mapped MRC file by writing the header.

This updates the first 1024 bytes of the memory map with the current header and flushes the mapping to disk. Must be called after all write_block calls.

§Errors

Returns Error::Io if the flush fails.

Trait Implementations§

Source§

impl Debug for MmapWriter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.