我正在尝试实现一个从 dns_parser 板条箱tokio_core::net::UdpCodec
创建一个。dns_parser::Packet
目前的实现如下所示:
pub struct MdnsCodec;
impl UdpCodec for MdnsCodec {
type In = dns_parser::Packet;
type Out = (SocketAddr, dns_parser::Builder);
fn decode(&mut self, addr: &SocketAddr, buf: &[u8]) -> io::Result<Self::In> {
Ok(dns_parser::Packet::parse(buf).unwrap())
}
fn encode(&mut self, (addr, builder): Self::Out, into: &mut Vec<u8>) -> SocketAddr {
let packet_data = builder.build().unwrap();
into.extend(&packet_data);
addr
}
}
dns_parser::Packet的定义是:
pub struct Packet<'a> {
pub header: Header,
pub questions: Vec<Question<'a>>,
pub answers: Vec<ResourceRecord<'a>>,
pub nameservers: Vec<ResourceRecord<'a>>,
pub additional: Vec<ResourceRecord<'a>>,
pub opt: Option<OptRecord<'a>>,
}
这无法编译:
error[E0106]: missing lifetime specifier
--> src/main.rs:18:15
|
18 | type In = dns_parser::Packet;
| ^^^^^^^^^^^^^^^^^^ expected lifetime parameter
error: aborting due to previous error
问题是我不知道要添加什么作为一生!我假设 Packet 需要与 buf 参数具有相同的生命周期。但我自己不知道如何正确表达这一点。
我已经将一个非工作示例上传到 github: https ://github.com/Fulkerson/mdnsfuturestest