Skip to content

Commit

Permalink
remove lifetime in io writers
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jul 29, 2021
1 parent 0f4cd66 commit fd649b3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
10 changes: 5 additions & 5 deletions polars/polars-io/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ use std::path::PathBuf;
use std::sync::Arc;

/// Write a DataFrame to csv.
pub struct CsvWriter<'a, W: Write> {
pub struct CsvWriter<W: Write> {
/// File or Stream handler
buffer: &'a mut W,
buffer: W,
/// Builds an Arrow CSV Writer
writer_builder: write::WriterBuilder,
/// arrow specific options
options: write::SerializeOptions,
}

impl<'a, W> SerWriter<'a, W> for CsvWriter<'a, W>
impl<W> SerWriter<W> for CsvWriter<W>
where
W: Write,
{
fn new(buffer: &'a mut W) -> Self {
fn new(buffer: W) -> Self {
CsvWriter {
buffer,
writer_builder: write::WriterBuilder::new(),
Expand All @@ -87,7 +87,7 @@ where
}
}

impl<'a, W> CsvWriter<'a, W>
impl<W> CsvWriter<W>
where
W: Write,
{
Expand Down
12 changes: 6 additions & 6 deletions polars/polars-io/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,21 @@ where
/// }
///
/// ```
pub struct IpcWriter<'a, W> {
writer: &'a mut W,
pub struct IpcWriter<W> {
writer: W,
}

impl<'a, W> SerWriter<'a, W> for IpcWriter<'a, W>
impl<W> SerWriter<W> for IpcWriter<W>
where
W: Write,
{
fn new(writer: &'a mut W) -> Self {
fn new(writer: W) -> Self {
IpcWriter { writer }
}

fn finish(self, df: &DataFrame) -> Result<()> {
fn finish(mut self, df: &DataFrame) -> Result<()> {
let df = to_arrow_compatible_df(df);
let mut ipc_writer = write::FileWriter::try_new(self.writer, &df.schema().to_arrow())?;
let mut ipc_writer = write::FileWriter::try_new(&mut self.writer, &df.schema().to_arrow())?;

let iter = df.iter_record_batches();

Expand Down
27 changes: 27 additions & 0 deletions polars/polars-io/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,36 @@ pub use arrow::{
record_batch::RecordBatch,
};
use polars_core::prelude::*;
use std::io::Write;
use std::io::{Read, Seek};
use std::sync::Arc;

// Write a DataFrame to JSON
pub struct JsonWriter<W: Write> {
/// File or Stream handler
buffer: W,
}

impl<W> SerWriter<W> for JsonWriter<W>
where
W: Write,
{
fn new(buffer: W) -> Self {
JsonWriter { buffer }
}

fn finish(self, df: &DataFrame) -> Result<()> {
let df = to_arrow_compatible_df(df);
let mut json_writer = arrow::io::json::LineDelimitedWriter::new(self.buffer);

let batches = df.as_record_batches()?;
json_writer.write_batches(&batches)?;
json_writer.finish()?;

Ok(())
}
}

impl<R: Read> ArrowReader for ArrowJsonReader<R> {
fn next_record_batch(&mut self) -> ArrowResult<Option<RecordBatch>> {
self.next()
Expand Down
4 changes: 2 additions & 2 deletions polars/polars-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ where
fn finish(self) -> Result<DataFrame>;
}

pub trait SerWriter<'a, W>
pub trait SerWriter<W>
where
W: Write,
{
fn new(writer: &'a mut W) -> Self;
fn new(writer: W) -> Self;
fn finish(self, df: &DataFrame) -> Result<()>;
}

Expand Down

0 comments on commit fd649b3

Please sign in to comment.