如何在循环中将结构序列化为 io::Write
How to serialise structs into io::Write in a loop
我需要像这样在 Rust 中做一个简单的 read/process/write:
#[derive(serde::Deserialize)]
struct Incoming {
first: String,
last: String,
}
#[derive(serde::Serialize)]
struct Outgoing {
name: String,
}
// Keep the read/write traits as generic as possible
fn stream_things<R: std::io::Read, W: std::io::Write>(reader: R, writer: W) {
let incoming: Vec<Incoming> = serde_json::from_reader(reader).unwrap();
for a in incoming {
let b = Outgoing {
name: format!("{} {}", a.first, a.last),
};
serde_json::to_writer(writer, &b).unwrap();
}
}
fn main() {
stream_things(std::io::stdin(), std::io::stdout());
}
这无法编译,因为:
error[E0382]: use of moved value: `writer`
--> src/main.rs:20:31
|
13 | fn stream_things<R: std::io::Read, W: std::io::Write>(reader: R, writer: W) {
| -- ------ move occurs because `writer` has type `W`, which does not implement the `Copy` trait
| |
| help: consider further restricting this bound: `W: Copy +`
...
20 | serde_json::to_writer(writer, &b).unwrap();
| ^^^^^^ value moved here, in previous iteration of loop
在循环中写入 std::io::Write
的正确方法是什么?
还有如何正确地使用 serde 的 to_writer
?
参见plaground。
鉴于 W
是 io::Write
,那么 &mut W
也是 io::Write
:
impl<'_, W: Write + ?Sized> Write for &'_ mut W
所以编译如下:
fn stream_things<R: std::io::Read, W: std::io::Write>(reader: R, mut writer: W) {
let incoming: Vec<Incoming> = serde_json::from_reader(reader).unwrap();
for a in incoming {
let b = Outgoing {
name: format!("{} {}", a.first, a.last),
};
serde_json::to_writer(&mut writer, &b).unwrap();
}
}
我需要像这样在 Rust 中做一个简单的 read/process/write:
#[derive(serde::Deserialize)]
struct Incoming {
first: String,
last: String,
}
#[derive(serde::Serialize)]
struct Outgoing {
name: String,
}
// Keep the read/write traits as generic as possible
fn stream_things<R: std::io::Read, W: std::io::Write>(reader: R, writer: W) {
let incoming: Vec<Incoming> = serde_json::from_reader(reader).unwrap();
for a in incoming {
let b = Outgoing {
name: format!("{} {}", a.first, a.last),
};
serde_json::to_writer(writer, &b).unwrap();
}
}
fn main() {
stream_things(std::io::stdin(), std::io::stdout());
}
这无法编译,因为:
error[E0382]: use of moved value: `writer`
--> src/main.rs:20:31
|
13 | fn stream_things<R: std::io::Read, W: std::io::Write>(reader: R, writer: W) {
| -- ------ move occurs because `writer` has type `W`, which does not implement the `Copy` trait
| |
| help: consider further restricting this bound: `W: Copy +`
...
20 | serde_json::to_writer(writer, &b).unwrap();
| ^^^^^^ value moved here, in previous iteration of loop
在循环中写入 std::io::Write
的正确方法是什么?
还有如何正确地使用 serde 的 to_writer
?
参见plaground。
鉴于 W
是 io::Write
,那么 &mut W
也是 io::Write
:
impl<'_, W: Write + ?Sized> Write for &'_ mut W
所以编译如下:
fn stream_things<R: std::io::Read, W: std::io::Write>(reader: R, mut writer: W) {
let incoming: Vec<Incoming> = serde_json::from_reader(reader).unwrap();
for a in incoming {
let b = Outgoing {
name: format!("{} {}", a.first, a.last),
};
serde_json::to_writer(&mut writer, &b).unwrap();
}
}