gpt4 book ai didi

rust - 对通过多个函数转发引用感到困惑

转载 作者:行者123 更新时间:2023-11-29 08:13:57 24 4
gpt4 key购买 nike

我无法理解如何通过函数转发引用。以下场景似乎按预期编译:

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,

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(())
}

我相信我遗漏了一些关于这里类型的简单信息。对我来说,这似乎传递了一个 &mut &mut BufRead,这不是预期的类型,但可以编译。

有人可以为这两个示例明确说明 readt 的类型吗?

我查看了以下主题:

对于第一个线程,由于 Rust 应用的取消引用规则,我认为与 C 风格指针的比较是错误的。

最佳答案

创建 Minimal, Complete, and Verifiable example重现问题是一个有用的步骤:

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:不保证 trait 对象具有大小,但泛型默认具有 Sized trait 绑定(bind)。​​

read is getting passed by value

是的,Rust 中的所有内容都总是按值传递。不过,有时该值恰好是一个引用。

read_message is expecting a &mut BufRead

事实并非如此。它需要一个实现特征 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, which is not the expected type

这是一种完美的软糖类型。 BufReadimplemented for对实现 BufRead 本身的任何类型的任何可变引用:

impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B

此外,在这种情况下,您没有&mut &mut BufRead,您有&mut &mut R。您显示的类型的具体单态化实际上是 &mut &mut Bufreader


您可以通过以下方式修复它:

  1. 更改 read_message 函数以接受未确定大小的类型。这很好,因为 R 总是在指针后面:

    pub fn read_message<R>(read: &mut R)
    where
    R: ?Sized + BufRead,
  2. 更改 parse_capnp 函数以引用具体类型而不是特征对象:

    fn parse_capnp<R>(read: &mut R)
    where
    R: BufRead,
    {
    read_message(read);
    }
  3. 更改 parse_capnp 函数以采用具体类型而不是特征对象。然后您需要自己引用它:

    fn parse_capnp<R>(mut read: R)
    where
    R: BufRead,
    {
    read_message(&mut read);
    }

关于rust - 对通过多个函数转发引用感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50454094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com