admin 管理员组

文章数量: 887007

rust存取一个含有borrowed域的结构体

直接存取不可以:

use std::io;use serde::{Serialize, Deserialize};extern crate bincode;#[derive(Debug, Serialize, Deserialize)]
struct A<'a> {a: &'a [u8]
}fn main() {let a: &[u8] = &[2, 3, 5, 7, 11, 13];let b = A{a};let mut v = Vec::new();bincode::serialize_into(&mut v, &b).unwrap();let mut reader = io::Cursor::new(&v);let c = bincode::deserialize_from::<_, A>(&mut reader).unwrap();println!("{:?}", c);
}

会报错:

error: implementation of `Deserialize` is not general enough--> src/main.rs:19:13|
19 |     let c = bincode::deserialize_from::<_, A>(&mut reader).unwrap();|             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Deserialize` is not general enough|= note: `A<'_>` must implement `Deserialize<'0>`, for any lifetime `'0`...= note: ...but `A<'_>` actually implements `Deserialize<'1>`, for some specific lifetime `'1`error: could not compile `serde-borrowed` due to previous error

这是因为BufWriter里的缓存的所有权不是调用者的,可能会被释放,这样构造含有borrowed域的结构体时就不能安全地借用读出来的数据了。

可以用std::borrow::Cow实现在serialize的时候borrow,但是在deserialize的时候create,从而解决这个问题:

use std::{io, borrow::Cow};use serde::{Serialize, Deserialize};extern crate bincode;#[derive(Debug, Serialize, Deserialize)]
struct A<'a> {a: Cow<'a, [u8]>,
}fn main() {let a: &[u8] = &[2, 3, 5, 7, 11, 13];let b = A{a: Cow::Borrowed(a)};let mut v = Vec::new();bincode::serialize_into(&mut v, &b).unwrap();let mut reader = io::Cursor::new(&v);let c = bincode::deserialize_from::<_, A>(&mut reader).unwrap();if let Cow::Borrowed(_) = c.a {panic!();}println!("{:?}", c);
}

此外,bincode有forward_read_*系列函数,好像也可以通过转移所有权解决这个问题:
.3.1/bincode/de/read/trait.BincodeRead.html
但是我不会。

参考文献

How to deserialize maybe-borrowed maybe-copied data?

本文标签: rust存取一个含有borrowed域的结构体