- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要一个 TcpStream
由一个 BufReader
共享和一个 BufWriter
,我在以下地方找到了解决方案:
If BufReader takes ownership of a stream, how can I read and write lines on it?
现在我想要它在它自己的数据结构中,但我只得到了部分答案:
Why can't I store a value and a reference to that value in the same struct?
use std::io::{BufReader, BufWriter};
use std::net::TcpStream;
pub struct BufTcpStream<'a> {
_socket: TcpStream,
input: BufReader<&'a TcpStream>;
output: BufWriter<&'a TcpStream>;
}
impl<'a> BufTcpStream<'a> {
pub fn new(socket: TcpStream) -> Self {
Self{
input : BufReader::new(&socket),
output: BufWriter::new(&socket),
_socket: socket,// <-- MOVE OF BORROWED VALUE HERE
}
}
}
为了解决这个问题,我必须确保 TcpStream
引用将在整个结构生命周期内保持有效,我使用了 Pin<Box<TcpStream>>
以确保它。
但编译器仍然提示借用值的移动socket
.为了消除这个障碍,我使用了 std::meme::transmute()
.
现在,我想知道的是:
use std::io::{BufReader, BufWriter};
use std::net::TcpStream;
use std::pin::Pin;
pub struct BufTcpStream<'a> {
_socket: Pin<Box<TcpStream>>,
input : BufReader<&'a TcpStream>,
output: BufWriter<&'a TcpStream>,
}
impl<'a> BufTcpStream<'a> {
pub fn new(socket: TcpStream) -> Self {
let pin = Box::pin(socket);
unsafe {
Self{
input : BufReader::new(std::mem::transmute(&*pin)),
output: BufWriter::new(std::mem::transmute(&*pin)),
_socket: pin,
}
}
}
pub fn reach(&mut self) -> (
&mut BufReader<&'a TcpStream>,
&mut BufWriter<&'a TcpStream>
) {
(&mut self.input, &mut self.output)
}
}
最佳答案
使用TcpStream::try_clone
获得第二个流:
The returned
TcpStream
is a reference to the same stream that this object references. Both handles will read and write the same stream of data, and options set on one stream will be propagated to the other stream.
然后您可以将一个包装在 reader 中,将一个包装在 writer 中:
use std::{
io::{self, BufReader, BufWriter},
net::TcpStream,
};
struct BufTcpStream {
input: BufReader<TcpStream>,
output: BufWriter<TcpStream>,
}
impl BufTcpStream {
fn new(stream: TcpStream) -> io::Result<Self> {
let input = BufReader::new(stream.try_clone()?);
let output = BufWriter::new(stream);
Ok(Self { input, output })
}
}
另见:
关于rust - 如何在数据结构中存储带有 BufReader 和 BufWriter 的 TcpStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58467659/
我正在尝试编写一个小的 Rust 程序,将/dev/ttyS0 上的行回显给发件人: fn echo_loop(device: &str) { let f = File::open(devic
有没有一种方法可以使用 Writer 并将写入此 writer 的内容取回?像 BufWriterReader 这样的东西?我试过这个,但没有编译: #[test] fn write_to_json_
BufWriter 有一个不可访问的私有(private)字段buf。我如何知道其中当前包含的数据的大小? 最佳答案 你不能(检查 the source to be sure )。内部 buf 没有以
看完 std::io::BufReader 文档,我不确定如何最好地传递 BufReader功能之间。允许多种排列,但哪种排列最好? 我有一个接受文件的函数: use std::{fs::File,
我用 Rust 编写了一个 echo 服务器和客户端。这是我的代码: 服务器: use std::net::{TcpListener, TcpStream}; use std::thread; use
我在写入文件时设置了 vim 以运行 Autoformat,但在对其进行编辑之前会定期忘记将扩展名添加到我的黑名单中。现在我无法保存编辑,因为自动格式化弄乱了缩进。有没有不运行 BufWrite 脚本
我想要一个 TcpStream由一个 BufReader 共享和一个 BufWriter ,我在以下地方找到了解决方案: If BufReader takes ownership of a strea
在标准库中,BufWriter声明如下: pub struct BufWriter { inner: Option, buf: Vec, // #30888: If the i
该用例由全世界所有 (vim) 开发人员共享(恕我直言):我们想在每个写入操作上更新一个标签和一个时间戳源文件。在 Barry Arthur 的帮助下,我的尝试是这样的: 每次写入更新的字符串示例 #
我是一名优秀的程序员,十分优秀!