@@ -55,6 +55,14 @@ pub fn spooled_tempfile(max_size: usize) -> SpooledTempFile {
5555 SpooledTempFile :: new ( max_size)
5656}
5757
58+ /// Write a cursor into a temporary file, returning the temporary file.
59+ fn cursor_to_tempfile ( cursor : & Cursor < Vec < u8 > > ) -> io:: Result < File > {
60+ let mut file = tempfile ( ) ?;
61+ file. write_all ( cursor. get_ref ( ) ) ?;
62+ file. seek ( SeekFrom :: Start ( cursor. position ( ) ) ) ?;
63+ Ok ( file)
64+ }
65+
5866impl SpooledTempFile {
5967 #[ must_use]
6068 pub fn new ( max_size : usize ) -> SpooledTempFile {
@@ -76,17 +84,13 @@ impl SpooledTempFile {
7684 /// Rolls over to a file on disk, regardless of current size. Does nothing
7785 /// if already rolled over.
7886 pub fn roll ( & mut self ) -> io:: Result < ( ) > {
79- if !self . is_rolled ( ) {
80- let mut file = tempfile ( ) ?;
81- if let SpooledData :: InMemory ( cursor) = & mut self . inner {
82- file. write_all ( cursor. get_ref ( ) ) ?;
83- file. seek ( SeekFrom :: Start ( cursor. position ( ) ) ) ?;
84- }
85- self . inner = SpooledData :: OnDisk ( file) ;
87+ if let SpooledData :: InMemory ( cursor) = & mut self . inner {
88+ self . inner = SpooledData :: OnDisk ( cursor_to_tempfile ( cursor) ?) ;
8689 }
8790 Ok ( ( ) )
8891 }
8992
93+ /// Truncate the file to the specified size.
9094 pub fn set_len ( & mut self , size : u64 ) -> Result < ( ) , io:: Error > {
9195 if size > self . max_size as u64 {
9296 self . roll ( ) ?; // does nothing if already rolled over
@@ -105,6 +109,14 @@ impl SpooledTempFile {
105109 pub fn into_inner ( self ) -> SpooledData {
106110 self . inner
107111 }
112+
113+ /// Convert into a regular unnamed temporary file, writing it to disk if necessary.
114+ pub fn into_file ( self ) -> io:: Result < File > {
115+ match self . inner {
116+ SpooledData :: InMemory ( cursor) => cursor_to_tempfile ( & cursor) ,
117+ SpooledData :: OnDisk ( file) => Ok ( file) ,
118+ }
119+ }
108120}
109121
110122impl Read for SpooledTempFile {
0 commit comments