rust

Is there a way to get a BufWriter's buffer length?


A BufWriter has a private field buf which is inaccessible. How can I know the size of the data currently contained in it?


Solution

  • You cannot (check the source to be sure). The inner buf is not exposed in any fashion, presumably to allow for the implementation to change without breaking compatibility.


    Well, you can, but it's terrible and hacky. You can print the BufWriter with the debugging formatter:

    use std::io::prelude::*;
    use std::io::{self, BufWriter};
    
    fn main() {
        let mut b = BufWriter::new(io::sink());
        b.write_all(b"hello, world").expect("Unable to write");
        println!("{:?}", b)
    }
    
    BufWriter { writer: Sink { .. }, buffer: 12/8192 }