首页 > 解决方案 > 对通过多个函数转发引用感到困惑

问题描述

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

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 BufReadread_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 应用了取消引用规则。

标签: 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.


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

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

推荐阅读