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>>
impl<Row: AutoSerialize> NpyWriter<Row, BufWriter<File>>
Sourcepub 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)?))
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
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>>
impl<Row: Serialize> NpyWriter<Row, BufWriter<File>>
Sourcepub fn close(self) -> Result<()>
👎Deprecated since 0.5.0: use .finish() instead
pub fn close(self) -> Result<()>
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>
impl<Row: Serialize + ?Sized, W: Write> NpyWriter<Row, W>
Sourcepub fn extend(&mut self, rows: impl IntoIterator<Item = Row>) -> Result<()>where
Row: Sized,
pub fn extend(&mut self, rows: impl IntoIterator<Item = Row>) -> Result<()>where
Row: Sized,
Write an iterator to the file
Sourcepub fn finish(self) -> Result<()>
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.