简体   繁体   English

rust 具有不同泛型类型的新动态特征变量?

[英]rust dyn trait variable new with different generic types?

I'm quite new to Rust.我对 Rust 很陌生。 Trying to create database connection with diesel-rs.尝试使用柴油机创建数据库连接。

Here's part of my code:这是我的代码的一部分:

use diesel::Connecction;
use diesel::mysql::MysqlConnection;
use diesel::sqlite::SqliteConnection;

let engine = "mysql";
let mysql_url = "mysql://username:password@localhost:3306/test";
let sqlite_url = "sqlite://sqlite.db";
let connection : Box<dyn Connection> = if engine == "mysql" {
  Box::new(MysqlConnection::establish(mysql_url).unwrap())
} else {
  Box::new(SqliteConnection::establish(sqlite_url).unwrap())
}

Here's the compiler error:这是编译器错误:

error[E0191]: the value of the associated types `Backend` (from trait `Connection`), `TransactionManager` (from trait `Connection`) must be specified
  --> src/quant/common/persistence/database.rs:11:25
   |
11 |     connection: Box<dyn Connection>,
   |                         ^^^^^^^^^^ help: specify the associated types: `Connection<Backend = Type, TransactionManager = Type>`

Is it possible to create different connection when program starts with different parameters?当程序以不同的参数启动时,是否可以创建不同的连接?

This use case is not supported.不支持此用例。 See this issue .看到这个问题 If you manage to do this with connection, you'll have to wrap the transaction and queries, and then likely some of the generated table!如果您设法通过连接来做到这一点,则必须包装事务和查询,然后可能还有一些生成的table! types, and down to a rabbit hole.类型,下到兔子洞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM