Skip to main content

Writer

Struct Writer 

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

MRC file writer and its builder. MRC file writer using standard file I/O.

For most use cases, prefer creating via WriterBuilder or the create convenience function.

The writer maintains an open file handle and writes data blocks directly to disk. Call finalize when done to ensure the header is correctly rewritten.

To write to an in-memory buffer instead of a file, use from_writer with a std::io::Cursor:

use mrc::{Header, Writer};
use std::io::Cursor;

let buffer: Vec<u8> = Vec::new();
let header = Header::new();
let mut writer = Writer::from_writer(Cursor::new(buffer), header, &[])?;
// ... write blocks, then finalize
writer.finalize()?;

Implementations§

Source§

impl Writer

Source

pub fn from_writer<W: Read + Write + Seek + 'static>( writer: W, header: Header, ext_header: &[u8], ) -> Result<Self, Error>

Create a writer that writes to an arbitrary std::io::Read + std::io::Write + std::io::Seek target.

This enables writing directly to in-memory buffers:

use mrc::{Header, Writer};
use std::io::Cursor;

let header = Header::new();
let mut writer = Writer::from_writer(Cursor::new(Vec::new()), header, &[])?;
// ... write blocks, then finalize
writer.finalize()?;
Source

pub fn from_writer_mmap<P: AsRef<Path>>( path: P, header: Header, ext_header: &[u8], ) -> Result<Self, Error>

Create a memory-mapped writer from a Header directly.

Like from_writer but uses a memory-mapped file instead of a file handle. Requires the mmap feature.

The file is truncated to the exact size needed for the header, extended header, and voxel data.

§Examples
use mrc::{Header, Writer};
let header = Header::new();
let mut writer = Writer::from_writer_mmap("output.mrc", header, &[])?;
Source

pub fn from_writer_gzip<P: AsRef<Path>>( path: P, header: Header, ext_header: &[u8], compression: Compression, ) -> Result<Self, Error>

Create a gzip-compressed writer from a Header directly.

Like from_writer but buffers the entire file in memory and gzip-compresses it on finalize. Requires the gzip feature.

§Examples
use mrc::{Header, Writer, Compression};
let header = Header::new();
let mut writer = Writer::from_writer_gzip("output.mrc.gz", header, &[], Compression::Balanced)?;
Source

pub fn shape(&self) -> VolumeShape

Volume dimensions for this writer.

§Examples
use mrc::create;
let mut writer = create("output.mrc")
    .shape([64, 64, 64])
    .mode::<f32>()
    .finish()?;
assert_eq!(writer.shape().nx, 64);
Source

pub fn mode(&self) -> Mode

Voxel data mode for this writer.

§Examples
use mrc::create;
let mut writer = create("output.mrc")
    .shape([64, 64, 64])
    .mode::<f32>()
    .finish()?;
assert_eq!(writer.mode(), mrc::Mode::Float32);
Source

pub fn header(&self) -> &Header

Read-only reference to the current header.

For mutable access, use header_mut. Modify header fields before calling finalize to change what gets written to disk.

§Examples
use mrc::create;
let mut writer = create("output.mrc")
    .shape([64, 64, 64])
    .mode::<f32>()
    .finish()?;
let h = writer.header();
assert_eq!(h.nx, 64);
Source

pub fn header_mut(&mut self) -> &mut Header

Mutable reference to the current header.

Allows modifying header fields (e.g. labels, density statistics) between writing blocks and calling finalize.

§Examples
use mrc::create;
let mut writer = create("output.mrc")
    .shape([64, 64, 64])
    .mode::<f32>()
    .finish()?;
writer.header_mut().ispg = 1;
Source

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

Write a block of voxels to the file.

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

§Examples
use mrc::{create, VoxelBlock};
let mut writer = create("output.mrc")
    .shape([64, 64, 1])
    .mode::<f32>()
    .finish()?;
let block = VoxelBlock::new([0, 0, 0], [64, 64, 1], vec![0.0f32; 64 * 64])?;
writer.write_block(&block)?;
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.

§Examples
use mrc::{create, VoxelBlock};
let mut writer = create("output.mrc")
    .shape([64, 64, 1])
    .mode::<u16>()
    .finish()?;
let block = VoxelBlock::new([0, 0, 0], [64, 64, 1], vec![0u8; 64 * 64])?;
writer.write_u8_block(&block)?;
Source

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

Write a block with automatic type conversion to the file’s mode.

The caller provides data as f32 and it is converted to the file’s on-disk mode. Supported conversions:

File modeConversion
Int8f32i8 (clamped, SIMD)
Int16f32i16 (clamped, SIMD)
Uint16f32u16 (clamped, SIMD)
Float32f32f32 (pass-through)
Float16f32f16 (SIMD, requires f16 feature)

Complex modes and Packed4Bit are not convertible from real f32 data. Use write_block with the matching complex type instead.

§Examples
use mrc::{create, VoxelBlock};
let mut writer = create("output.mrc")
    .shape([64, 64, 1])
    .mode::<u16>()
    .finish()?;
let block = VoxelBlock::new([0, 0, 0], [64, 64, 1], vec![0.0f32; 64 * 64])?;
writer.write_block_as(&block)?;
Source

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

Write a block with parallel encoding and sequential file I/O.

Encoding is performed in parallel using all available cores. File writes are performed sequentially to ensure cross-platform compatibility.

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

§Examples
use mrc::{create, VoxelBlock};
let mut writer = create("output.mrc")
    .shape([64, 64, 1])
    .mode::<f32>()
    .finish()?;
let block = VoxelBlock::new([0, 0, 0], [64, 64, 1], vec![0.0f32; 64 * 64])?;
writer.write_block_parallel(&block)?;
Source

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

Write a block of u8 data (0–15 per voxel) by packing to 4-bit (Mode 101).

The file must have been created with Mode::Packed4Bit. Each u8 value is checked to be in the range 0–15; values exceeding 15 produce an error.

§Examples
use mrc::{create, VoxelBlock};
let mut writer = create("output.mrc")
    .shape([64, 64, 1])
    .mode_raw(101)  // Packed4Bit
    .finish()?;
let data = vec![0u8; 64 * 64];
let block = VoxelBlock::new([0, 0, 0], [64, 64, 1], data)?;
writer.write_u4_block(&block)?;
Source

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

Finalize the MRC file by rewriting the header.

§Examples
use mrc::{create, VoxelBlock};
let mut writer = create("output.mrc")
    .shape([64, 64, 1])
    .mode::<f32>()
    .finish()?;
let block = VoxelBlock::new([0, 0, 0], [64, 64, 1], vec![0.0f32; 64 * 64])?;
writer.write_block(&block)?;
writer.finalize()?;
Source

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

Scan the written data block and update header statistics.

§Examples
use mrc::{create, VoxelBlock};
let mut writer = create("output.mrc")
    .shape([64, 64, 1])
    .mode::<f32>()
    .finish()?;
let block = VoxelBlock::new([0, 0, 0], [64, 64, 1], vec![0.0f32; 64 * 64])?;
writer.write_block(&block)?;
writer.update_header_stats()?;
writer.finalize()?;

Trait Implementations§

Source§

impl Debug for Writer

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Writer

§

impl !Send for Writer

§

impl !Sync for Writer

§

impl !UnwindSafe for Writer

§

impl Freeze for Writer

§

impl Unpin for Writer

§

impl UnsafeUnpin for Writer

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more