rustnodejs-polars

Rust error[E0597]: does not live long enough


I am trying to simplify this nodejs-polars code.
I have path_or_buffer: Either<String, Buffer> as a function param. When trying to match:

let file = match path_or_buffer {
   Either::A(path) => Box::new(std::fs::File::open(path).unwrap()) as Box<dyn MmapBytesReader>,
   Either::B(buffer) => Box::new(Cursor::new(buffer.as_ref())) as Box<dyn MmapBytesReader>
}

I get: error[E0597]: buffer does not live long enough Thx


Solution

  • My solution turned to be rather simple, Box::new turned out to be the problem.

    fn mmap_reader_to_df<'a>(
        csv: impl MmapBytesReader + 'a,
        options: ReadCsvOptions,
    ) -> napi::Result<JsDataFrame> {...}
    

    then to call it:

    pub fn read_csv(
        path_or_buffer: Either<String, Buffer>,
        options: ReadCsvOptions,
    ) -> napi::Result<JsDataFrame> {
        match path_or_buffer {
            Either::A(path) => mmap_reader_to_df(std::fs::File::open(path).expect("unable to read file"), options),
            Either::B(buffer) => mmap_reader_to_df(Cursor::new(buffer.as_ref()), options),
        }
    }