对通过多个功能转发引用感到困惑

问题描述 投票:1回答:1

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

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风格指针的比较是错误的。

rust
1个回答
3
投票

创建一个可以重现问题的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:不保证特征对象具有大小,但是泛型具有默认绑定的Sized特征。

read越过价值

是的,Rust中的所有内容总是按值传递。有时这个值恰好是一个参考。

read_message期待一个&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)

也可以看看:

一个&mut &mut BufRead,这不是预期的类型

这是一种完美的类型。 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); }
© www.soinside.com 2019 - 2024. All rights reserved.