Struct npyz::NpyWriter

source ·
pub struct NpyWriter<Row: Serialize + ?Sized, W: Write> { /* private fields */ }
Expand description

Interface for writing an NPY file to a data stream.

To construct an instance of this, you must go through the WriterBuilder trait:

use npyz::WriterBuilder;

fn main() -> std::io::Result<()> {
    // Any io::Write is supported.  For this example we'll
    // use Vec<u8> to serialize in-memory.
    let mut out_buf = vec![];
    let mut writer = {
        npyz::WriteOptions::new()
            .default_dtype()
            .shape(&[2, 3])
            .writer(&mut out_buf)
            .begin_nd()?
    };

    writer.push(&100)?;
    writer.push(&101)?;
    writer.push(&102)?;
    // can write elements from iterators too
    writer.extend(vec![200, 201, 202])?;
    writer.finish()?;

    eprintln!("{:02x?}", out_buf);
    Ok(())
}

Implementations§

source§

impl<Row: AutoSerialize> NpyWriter<Row, BufWriter<File>>

source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>

👎Deprecated since 0.5.0: Doesn’t carry its weight. Use to_file_1d instead, or replicate the original behavior with Builder::new().default_dtype().begin_1d(std::io::BufWriter::new(std::fs::File::create(path)?))

Create a file, using the default format for the given type.

source§

impl<Row: Serialize> NpyWriter<Row, BufWriter<File>>

source

pub fn close(self) -> Result<()>

👎Deprecated since 0.5.0: use .finish() instead

Finish writing the file and close it. Alias for NpyWriter::finish.

If omitted, the file will be closed on drop automatically, ignoring any errors encountered during the process.

source§

impl<Row: Serialize + ?Sized, W: Write> NpyWriter<Row, W>

source

pub fn push(&mut self, row: &Row) -> Result<()>

Append a single row to the file

source

pub fn extend(&mut self, rows: impl IntoIterator<Item = Row>) -> Result<()>
where Row: Sized,

Write an iterator to the file

source

pub fn finish(self) -> Result<()>

Finish writing the file.

If no shape was provided, this will update the header to reflect the number of elements written. If a shape was provided and the number of inserted elements is incorrect, an error is returned.

This is automatically called on drop, but in that case, errors are ignored.

Trait Implementations§

source§

impl<Row: Serialize + ?Sized, W: Write> Drop for NpyWriter<Row, W>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<Row, W> !RefUnwindSafe for NpyWriter<Row, W>

§

impl<Row, W> !Send for NpyWriter<Row, W>

§

impl<Row, W> !Sync for NpyWriter<Row, W>

§

impl<Row: ?Sized, W> Unpin for NpyWriter<Row, W>
where W: Unpin, <Row as Serialize>::TypeWriter: Unpin,

§

impl<Row, W> !UnwindSafe for NpyWriter<Row, W>

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> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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.