What is the difference between write_all and flush in io::Write trait?

jhk999

I am talking about the std::io::Write. The definition flush reads like basically exactly the same as the definition of the write_all function.

I have attempted understanding the difference by checking a couple popular Write implementing libraries like TcpStream but typically I see they just return Ok(()) without doing anything.

Is it okay to just not implement flush if we aren't dealing with some underlying system call for flushing some internal buffer?

Jmb

write_all ensures that all data in the passed buffer is consumed (whereas write may consume only part of the data). To this effect, write_all will retry automatically until the whole data has been consumed (or an unrecoverable error occurs).

However this does not guarantee that the data has actually been written to disk (or to the underlying medium): it may have been buffered in the program (e.g. in a BufferedWriter) or by the OS. flush waits until all data that was previously consumed has been written to the underlying medium. This makes sense when writing to disk but may not make sense in other contexts, which is why some implementations of flush do nothing.

If you're writing a custom Write implementation, you can keep an empty flush method if "flushing" doesn't make sense for your target.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the difference between append and write methods of java.io.writer?

What's the difference between ResponseWriter.Write and io.WriteString?

The MPI_IO write_all subarray and the component number

What is the difference between Trait::<T> and <Trait<T>>?

What's the difference between `flush` and `sync_all`?

What is the difference between <T: Trait> Box<T> and &Trait / Box<Trait>?

What is the difference between SessionHandlerInterface::write and SessionUpdateTimestampHandlerInterface::updateTimestamp?

What is the difference between .send()" and .write() in ruby sockets?

SQLAlchemy: What's the difference between flush() and commit()?

What is the difference between commit and flush for IndexWriter in Lucene

What is the practical difference between flush and commit?

What is the difference between tick() and flush() in angular testing?

What is the difference between read+write and read+write+truncate?

What is the difference between a Decorator, Attribute, Aspect, and Trait?

What is the difference between trait and behavior in cakephp 3?

What is the Difference Between read() and recv() , and Between send() and write()?

What's the difference between `<T: Trait>` and `where T: Trait`?

What is the difference between &Trait and impl Trait when used as method arguments?

Use fmt::Write or io::Write trait for my writable type?

What's the difference between '_io' and 'io'?

What is the difference between ScriptManager.RegisterClientScriptBlock and Response.Write?

What's the difference between [Computed] and [Write(false)] attributes?

What is the difference between res.send and res.write in express?

What is the difference between the volatile modifier and Volatile.Read/Write?

What's the difference between the SharedAccessBlobPermissions values Add, Create and Write?

What is difference between vault write and vault kv put?

What's the difference between console.log and response.write?

What's the difference between Preferences.flush() and Preferences.sync()?

What is the difference between sleep() and usleep() in this ob_flush example