printpdf icon indicating copy to clipboard operation
printpdf copied to clipboard

Why does `save` mandate a `BufWriter`?

Open griff opened this issue 4 years ago • 2 comments

In save: https://github.com/fschutt/printpdf/blob/097defa25d5d5a14520498043a30979fe4af8bba/src/types/pdf_document.rs#L343

It doesn´t seem to actually need it to be a BufWriter since it just calls save_to in lopdf which takes a Write.

griff avatar May 17 '21 15:05 griff

It's supposed to prevent you from passing in a File handle because then Rust would write the PDF character-by-character, requiring a syscall for every single character. I once benchmarked this crate and saving the file took the largest amount of time, that's why BufWriter is required.

Of course you could also use an io::Cursor<Vec<u8>> to write the file to memory first, then flush the memory to disk, but it's just to prevent the default (passing in a File handle directly).

fschutt avatar May 17 '21 20:05 fschutt