-
Dears, I'd like to make an API that can work with either the Conn or the PooledConn struct, only using functions from the Queryable trait. Unfortunately Rust does not allow using trait-objects parameters for trait Queryable asit is not Object safe ! (I tried to understand why (I'm fairly new to Rust) I think it's because there is a What are my option? do I have to create a new type implementing Queryable that can delegate to either Conn or PooledConn? Thanks! Example: pub fn dump_table(conn: &mut Conn, schema: &String, table: &String) -> Result<()> {...} // Allowed
pub fn dump_table(conn: &mut dyn Queryable, schema: &String, table: &String) -> Result<()> {...} // the trait `mysql::prelude::Queryable` cannot be made into an object
pub fn dump_table(conn: Box<dyn Queryable>, schema: &String, table: &String) -> Result<()> {...} // the trait `mysql::prelude::Queryable` cannot be made into an object |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Just saw that PooledConn is just a wrapper for Conn, so I can only use Conn, sorry for the noise |
Beta Was this translation helpful? Give feedback.
-
Never tried dynamic dispatch, actually. Always using generics instead: fn by_id<T>(conn: &mut T, id: u64) -> crate::Result<Self>
where
T: Queryable,
{
\\ ...
} |
Beta Was this translation helpful? Give feedback.
Never tried dynamic dispatch, actually. Always using generics instead: