rust - 对通过多个函数转发引用感到困惑
问题描述
我无法理解如何通过函数转发引用。以下场景似乎按预期编译:
trait Trait {}
struct ImplementsTrait {}
impl Trait for ImplementsTrait {}
fn foo(t: &mut Trait) {
// ... use the mutable reference
}
fn forward(t: &mut Trait) {
foo(t); // forward the type '&mut Trait' to foo
}
fn main() {
let mut t = ImplementsTrait{};
forward(&mut t); // need to pass as reference because Trait has no static size
}
但是,在使用capnp crate的 API 时,我得到了意想不到的行为:
fn parse_capnp(read: &mut BufRead) {
let reader = serialize_packed::read_message(read, message::ReaderOptions::new());
Ok(())
}
fn main() {
// ... ///
let mut br = BufReader::new(f);
parse_capnp(&mut br);
Ok(())
}
error[E0277]: the trait bound `std::io::BufRead: std::marker::Sized` is not satisfied
--> src/main.rs:18:16
|
18 | let reader = serialize_packed::read_message(read, message::ReaderOptions::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `std::io::BufRead` does not have a constant size known at compile-time
的签名read_message
是:
pub fn read_message<R>(
read: &mut R,
options: ReaderOptions
) -> Result<Reader<OwnedSegments>>
where
R: BufRead,
当它是 a并且期望 a时,它似乎read
是按值传递的。为我编译此代码段的唯一方法是将其更改为:&mut BufRead
read_message
&mut BufRead
fn parse_capnp(mut read: &mut BufRead) {
let reader = serialize_packed::read_message(&mut read, message::ReaderOptions::new());
Ok(())
}
我相信我在这里遗漏了一些简单的类型。对我来说,这似乎传递了 a &mut &mut BufRead
,这不是预期的类型,但可以编译。
有人可以澄清这两个例子的类型read
和类型吗?t
我查看了以下线程:
对于第一个线程,我想说与 C 样式指针的比较是错误的,因为 Rust 应用了取消引用规则。
解决方案
创建一个重现问题的最小、完整和可验证的示例是一个有用的步骤:
use std::io::BufRead;
pub fn read_message<R>(read: &mut R)
where
R: BufRead,
{}
fn parse_capnp(read: &mut BufRead) {
read_message(read);
}
fn main() {}
error[E0277]: the trait bound `std::io::BufRead: std::marker::Sized` is not satisfied
--> src/main.rs:9:5
|
9 | read_message(read);
| ^^^^^^^^^^^^ `std::io::BufRead` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `std::io::BufRead`
note: required by `read_message`
--> src/main.rs:3:1
|
3 | / pub fn read_message<R>(read: &mut R)
4 | | where
5 | | R: BufRead,
6 | | {}
| |__^
现有问题中很好地涵盖了错误消息:
TL;DR:不能保证特征对象具有大小,但泛型Sized
默认情况下具有特征绑定。
read
正在按值传递
是的,Rust 中的一切总是按值传递。有时,该值恰好是参考。
read_message
期待一个&mut BufRead
它不是。它期望实现 trait 的泛型类型BufRead
。这两个签名是不同的:
// Reference to a concrete type
pub fn read_message<R>(read: &mut R)
where
R: BufRead,
// Trait object
pub fn read_message<R>(read: &mut BufRead)
也可以看看:
a
&mut &mut BufRead
,这不是预期的类型
这是一个完美的cromulent类型。BufRead
为实现自身的任何类型的任何可变引用实现BufRead
:
impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B
此外,在这种情况下,您没有&mut &mut BufRead
,您有&mut &mut R
。您展示的类型的具体单态化实际上是一个&mut &mut Bufreader
.
您可以通过以下方式修复它:
更改
read_message
函数以接受未调整大小的类型。这很好,因为R
总是在指针后面:pub fn read_message<R>(read: &mut R) where R: ?Sized + BufRead,
更改
parse_capnp
函数以引用具体类型而不是特征对象:fn parse_capnp<R>(read: &mut R) where R: BufRead, { read_message(read); }
更改
parse_capnp
函数以采用具体类型而不是特征对象。然后,您需要自己参考它:fn parse_capnp<R>(mut read: R) where R: BufRead, { read_message(&mut read); }
推荐阅读
- python-3.x - 如何自动设置 MDCard 高度
- php - 遇到错误并且无法解决:count(): Argument #1 ($value) must be of type Countable|array, string given
- javascript - 使用 .htdacess 更改页面名称时 PHP GET 变量不起作用
- python - Python/Flask 初学者:如何格式化 db 值?
- typescript - Nextjs 如何在传递时声明 prop 类型?
- vba - Vba 通过使用 seach 函数找到“-”并仅提取数字修复错误
- python-3.x - 从列表中提取值,该列表落在熊猫数据框指定的间隔内
- node.js - 表达静态行为
- mongodb - 在 ubuntu 18.04 中重新安装 mongodb 时出错
- html - 如何使用 Bootstrap 居中和限制页面内容?