返回通用特征的 Rust 函数

标签 rust

我有两个实现相同特征的结构。我需要一个函数来返回任何这些结构的对象。

特质

pub trait DbfFile{
    fn new(fields: HashMap<String, FieldValue>) -> Self;
}

结构是

pub struct InsertableAttachable {
    pub f1: String,
    pub f2: String,
    pub f3: String,
    pub f4: i8,
}

impl DbfFile for InsertableAttachable{
    fn new(fields: HashMap<String, FieldValue, RandomState>) -> Self {
        InsertableAttachable {
            // fields
        }
    }
}



// Converting this to generic

impl InsertableAttachable{
    pub fn get_data_vector(mut iter: DatabaseRecordIterator) -> Vec<InsertableAttachable>{
        let mut db_data: Vec<InsertableAttachable> = vec!();
        while let Some(table_row) = iter.next() {
            let fields = table_row.fields;
            db_data.push(InsertableAttachable::new(fields));
        };
        db_data
    }
}

还有一个结构

pub struct InsertableAttached {
    pub f1: String,
    pub f2: i32,
    pub f3: i32,
}

impl DbfFile for InsertableAttached {
    fn new(fields: HashMap<String, FieldValue, RandomState>) -> Self {
        InsertableAttached {
           // fields
        }
    }

}

// Converting this to generic

impl InsertableAttached{
    pub fn get_data_vector(mut iter: DatabaseRecordIterator) -> Vec<InsertableAttached>{
        let mut db_data: Vec<InsertableAttached> = vec!();
        while let Some(table_row) = iter.next() {
            let fields = table_row.fields;
            db_data.push(InsertableAttached::new(fields));
        };
        db_data
    }
}

这里有像 this returns box / struct 这样的例子和 this one返回一个选项。

下面的函数有编译时错误

error[E0277]: the size for values of type `(dyn models::traits::DbfFile + 'static)` cannot be known at compilation time
  --> src/dbf.rs:75:1
   |
75 | / pub fn get_data_vector(&mut iter: DatabaseRecordIterator) -> Vec<DbfFile>{
76 | |     let mut db_data: Vec<DbfFile> = vec!();
77 | |     while let Some(table_row) = iter.next() {
78 | |         let fields = table_row.fields;
...  |
81 | |     db_data
82 | | }
   | |_^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `(dyn models::traits::DbfFile + 'static)`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: required by `std::vec::Vec`

error[E0038]: the trait `models::traits::DbfFile` cannot be made into an object
  --> src/dbf.rs:75:1
   |
75 | pub fn get_data_vector(&mut iter: DatabaseRecordIterator) -> Vec<DbfFile>{
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `models::traits::DbfFile` cannot be made into an object

最佳答案

您需要对结果进行装箱,以便能够通过动态调度返回异构集合:

pub fn get_data_vector(&mut iter: DatabaseRecordIterator) -> Vec<Box<dynDbfFile>>

但是您会收到以下错误:

| pub fn get_data_vector(&mut iter: DatabaseRecordIterator) -> Vec<Box<dyn DbfFile>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `DbfFile` cannot be made into an object
|

这样做的原因是您的函数返回 Self - 每个实现都不同。您需要将其更改为通用类型以使特征对象安全(允许动态分派(dispatch))。

另一种方法是通过枚举返回多种类型:

enum GetDataVectorResult {
    Attachable(InsertableAttachable),
    Attached(InsertableAttached),
}

pub fn get_data_vector(&mut iter: DatabaseRecordIterator) -> Vec<GetDataVectorResult>

关于返回通用特征的 Rust 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58019355/

相关文章:

html - 编写 HTML 解析器时借用的值不够长

multithreading - 在多核嵌入式 Rust 中,我可以使用静态 mut 进行单向数据共享吗?

rust - 如何在没有 for 循环的情况下使用 Itertools group_by 迭代器方法?

rust - 如何在 Rust 中定义递归类型别名?

rust - 如何将&str列表转换为静态&str列表?

rust - Rust 中的 unwrap 是什么,它的用途是什么?

rust - 为什么我们需要取消引用匹配中的可变引用来更改它?

rust - Cargo 在 GitHub Actions 中运行时无法下载依赖项

string - 如何检查字符串是否包含空格?

rust - 如何返回对已移动框内内容的引用?