Rust:将借用的结构传递给借用的枚举?

Rust: Passing borrowed struct to a borrowed enum?

我正在尝试将借用的结构传递给借用的枚举。

#[derive(Copy, Clone)]
pub struct CustomerData {
    // Many fields about customers
}

#[derive(Copy, Clone)]
pub struct EmployeeData {
    // Many fields about employees
}

pub enum Person {
    Customer(CustomerData),
    Employee(EmployeeData)
}

fn do_something_with_customer(customer: &CustomerData) {
    let person = &Person::Customer(customer);

    // This would work, but this can be a large struct.
    // let person = &Person::Customer(customer.clone());

    general_method(person);
}

fn do_something_with_employee(employee: &EmployeeData) {
    let person = &Person::Employee(employee);

    // This would work, but this can be a large struct.
    // let person = &Person::Employee(employee.clone());

    general_method(person);
}

fn general_method(person: &Person) {

}

fn main() {
    let person = Person::Customer(CustomerData { });

    match &person {
        Person::Customer(data) => {
            do_something_with_customer(data);
        }
        Person::Employee(data) => {
            do_something_with_employee(data);
        }
    }
}

编译给出结果:

error[E0308]: mismatched types
  --> src/main.rs:19:36
   |
19 |     let person = &Person::Customer(customer);
   |                                    ^^^^^^^^
   |                                    |
   |                                    expected struct `CustomerData`, found reference
   |                                    help: consider dereferencing the borrow: `*customer`
   |
   = note: expected type `CustomerData`
              found type `&CustomerData`

error[E0308]: mismatched types
  --> src/main.rs:28:36
   |
28 |     let person = &Person::Employee(employee);
   |                                    ^^^^^^^^
   |                                    |
   |                                    expected struct `EmployeeData`, found reference
   |                                    help: consider dereferencing the borrow: `*employee`
   |
   = note: expected type `EmployeeData`
              found type `&EmployeeData`

我知道 Rust 编译器不允许我这样做,但我觉得我应该能够这样做,考虑到我将结构传递给的枚举也是借用的。

这个场景有pattern/workaround吗?也许使用 Rc 类型?对于这种情况,我不想在我的代码中乱扔垃圾。

use std::rc::Rc;

#[derive(Copy, Clone)]
pub struct CustomerData {
    // Many fields about customers
}

#[derive(Copy, Clone)]
pub struct EmployeeData {
    // Many fields about employees
}

pub enum Person {
    Customer(Rc<CustomerData>),
    Employee(Rc<EmployeeData>)
}

fn do_something_with_customer(customer: Rc<CustomerData>) {
    let person = &Person::Customer(customer);

    // This would work, but this can be a large struct.
    // let person = &Person::Customer(customer.clone());

    general_method(person);
}

fn do_something_with_employee(employee: Rc<EmployeeData>) {
    let person = &Person::Employee(employee);

    // This would work, but this can be a large struct.
    // let person = &Person::Employee(employee.clone());

    general_method(person);
}

fn general_method(person: &Person) {

}

fn main() {
    let person = Person::Customer(Rc::new(CustomerData { }));

    match &person {
        Person::Customer(data) => {
            do_something_with_customer(data.clone());
        }
        Person::Employee(data) => {
            do_something_with_employee(data.clone());
        }
    }
}

您错误地识别了问题,编译器在其错误注释中准确无误。

您像这样定义了您的枚举:

pub enum Person {
    Customer(CustomerData),
    Employee(EmployeeData)
}

但是你决定你的枚举成员应该是 Person::Customer(&CustomerData):

fn do_something_with_customer(customer: &CustomerData) {
    let person = &Person::Customer(customer);

引用不可传递。因为 &CustomerData 是引用并不意味着整个枚举将是对真实数据的引用(即 &Person::Customer(CustomerData))。

有两种方法可以修复它;显而易见的是查看 CustomerData 是否实现了 Copy。如果是这样,您可以取消引用(因此隐式复制):

fn do_something_with_customer(customer: &CustomerData) {
    let person = Person::Customer(*customer);

(这是编译器的建议,所以我很确定你的类型实现了 Copy

另一个选项是 #[derive(Clone)] 类型并调用 customer.clone()。同样,以额外分配为代价。

如果你真的想要枚举中的引用,你需要将枚举定义更改为:

pub enum Person<'a> {
    Customer(&'a CustomerData),
    Employee(&'a EmployeeData)
}

并处理对象 属性 现在是一个引用这一事实,以及所有相关的问题。