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>
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<()>
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.
Trait Implementations§
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>
impl<Row, W> !UnwindSafe for NpyWriter<Row, W>
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
Mutably borrows from an owned value. Read more