-
I am new to Diesel, and I want to create a method returning a vector of models sorted by a method parameter. My current code looks like this: pub enum Order {
DATE,
LENGTH,
DURATION
}
// ...
pub fn get_tracks(&self, order: Order) -> Vec<Track> {
let order_arg = match order {
DATE => {
start_date_col.asc()
},
LENGTH => {
length_col.asc()
},
DURATION => {
duration_col.asc()
}
};
tracks_table
.order(order_arg)
.load::<Track>(&self.conn)
.unwrap()
} The But this fails to compile because Rust says the matches are not the same type:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
As the compiler message says each match arm returns a different type. That's expected as each column type is it's own distinct type. See the Schema in depth guide for details about what is produced by diesel's Now there are multiple ways to "solve" your problem:
|
Beta Was this translation helpful? Give feedback.
As the compiler message says each match arm returns a different type. That's expected as each column type is it's own distinct type. See the Schema in depth guide for details about what is produced by diesel's
table!
macro internally.Now there are multiple ways to "solve" your problem:
match
statement, use.into_boxed()
to produce a stable query type and then apply the order in each match armBoxableExpression
to construct a compatible common trait object based on your expressions.