实际问题(避免A/B场景):
我在Arc对象后面有一个很大的缓冲区:
pub struct LargeData {
data: Arc<Vec<u8>>,
}
字符串
我需要将Read + Seek
版本作为函数的Box'ed返回值传递给网络API(不复制整个缓冲区)。
首先,我试着让Cursor引用Vec:
fn open_for_read(&self) -> io::Result<Box<dyn ReadAndSeek>> {
Ok(Box::new(Cursor::new(self.data.as_ref())))
}
型
但是编译器正确地警告这个游标实际上可能比所有ARC示例都存在。
我的第二个想法是将Cursor
和Arc
打包到新的结构中,并为其实现Read + Seek
struct ReaderData {
data: Arc<Vec<u8>>,
cursor: Cursor<Vec<u8>>,
}
impl ReaderData {
fn new(source: &LargeData) -> Self {
let data = source.data.clone();
Self {
data,
cursor: Cursor::new(data),
}
}
}
// Read + Seek implementation that only passes into cursor Read and Seek
型
但这一次我犯了更神秘的错误:
cursor: Cursor::new(data),
----------- ^^^^- help: try using a conversion method: `.to_vec()`
| |
| expected `Vec<u8>`, found `Arc<Vec<u8>>`
arguments to this function are incorrect
型
使用to_vec()
会复制vector,这是我想要避免的。
这是我放弃的地方。
我在这里错过了什么吗?我如何从Arc<Vec<u8>>
创建Read + Seek
对象?
1条答案
按热度按时间j91ykkif1#
Cursor
需要类型来实现AsRef<[u8]>
。不幸的是,Arc<Vec<u8>>
没有,但是你可以自己为LargeData
实现它:字符串
如果
LargeData
包含其他字段,则可以将Arc<Vec<u8>>
提取到不同的结构中。