Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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

like image 801
Razakhel Avatar asked Mar 05 '18 13:03

Razakhel


People also ask

How to get the available buffer size of the buffer-writer?

In the main () function, we opened the "Demo.txt" file and then created a buffer-writer using bufio.NewWriter () function. Then we got the available buffer size using the Available () function and print the result on the console screen.

How do I change the size of a buffer?

So the buffer length property should be considered read only, and another way should be used to set the size of a buffer. One way to go about changing the buffer length would be to just use a method like buffer slice to create a new buffer of the desired size and then set that to the variable that is being used.

What is the length of a string in a buffer?

Buffers have a length property that contains the number of bytes in the buffer. For buffers containing UTF8 encoded strings, the buffer's length is equivalent to the length of the string. For example, if you read a text file from the file system using fs, the resulting buffer's length is the same as the number of characters in the text file.

Why do we need buffer length in NodeJS?

The reason why is because of the nature of Unicode. However in nodejs when working with buffers the buffer length property of a buffer can be used to get the amount of memory that the buffer is taking up at least. In addition if buffers are used the right way buffer length can be used as a way to get the actual data size of a string.


1 Answers

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 }
like image 110
Shepmaster Avatar answered Sep 25 '22 07:09

Shepmaster