If BufReader takes ownership of a stream, how can I read and write lines on it?

谁说胖子不能爱 提交于 2019-11-26 17:49:27

问题


I want to read a line from a TCPStream, write another line to it, and then repeat. The issue is that BufReader::new takes ownership of my TCPStream variable:

let stream = ...; // TCPStream

let reader = BufReader::new(stream); // moved its value

// can't use stream here anymore

What is a simple solution to this?


回答1:


Solution: use references.

let mut stream = ...;
let reader = BufReader::new(&stream);
let writer = BufWriter::new(&stream);

Explanation

If we take a closer look at BufReader::new, we see that it takes an argument inner of type R, where R is just any type that implements Read:

impl<R: Read> BufReader<R> {
    pub fn new(inner: R) -> BufReader<R> { ... }
}

We then take a look at Read and see this implementation:

impl<'a> Read for &'a TcpStream

So we can just pass a reference to the new function, like so:

let reader = BufReader::new(&stream);

We will do the same for BufWriter and Write and will see that indeed there is this implementation:

impl<'a> Write for &'a TcpStream

So we can, again, use an immutable reference to create the BufWriter.




回答2:


You have several possibilities:

  • do not use a BufReader
  • temporarily borrow the stream from the BufReader using get_mut
  • discard the BufReader entirely and recover the stream using into_inner

I would personally advise not using a BufReader unless you really need to bufferize the input; for a single line it does not seem worth it.

Otherwise, if you are done with the buffering, you can recover the underlying stream and if you are not you can temporarily buffer it.

Caution: Do be aware that the BufReader bufferizes reads, so when you borrow/recover the inner stream you short-circuit the buffered data; for reading it's an issue, in your case (writing) it should be fine.



来源:https://stackoverflow.com/questions/35869078/if-bufreader-takes-ownership-of-a-stream-how-can-i-read-and-write-lines-on-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!