-
|
Hello, I'm trying to convert a Model into an ActiveModel and I'd like to understand the nuances of doing so inline versus from within a called function. If I convert "inline", I call: let active_model: foo::ActiveModel = foo_model.into();If I try to convert a Model into an ActiveModel in a function, then I need to call something more like: let mut active_model: foo::ActiveModel = foo_model.clone().into_active_model();The following code snippet does not compile - as it is a fragment of my larger codebase - but it is abstracted in a way that if I toggle the let mut scrape = scrapes::ActiveModel {
..Default::default()
};
let scrape: scrapes::Model = scrape.insert(&db_connection).await.expect("Failure");
let shall_call_function = false;
let mut scrape: scrapes::ActiveModel = match shall_call_function {
true => scrape.into(),
false => {
get_active_model(
&scrape,
)
.await
.unwrap()
}
};
let dt = Local::now();
scrape.finished_at = Set(Some(dt.into()));
let scrape: scrapes::Model = scrape.update(&db_connection).await.expect("Failure");
let close_result = db_connection.close().await;
Ok(())
}
// {{{ async fn get_active_model
async fn get_active_model(
scrape: &scrapes::Model,
) -> Result<scrapes::ActiveModel, ()> {
let mut scrape: scrapes::ActiveModel = scrape.clone().into_active_model();
Ok(scrape)
}Why does Thanks for any help! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
If I remember correctly the trait Inline, the compiler can move the owned value into |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help @sinder38 ! I see that I could do: or and that the resulting code is (probably?) the same. So the |
Beta Was this translation helpful? Give feedback.
If I remember correctly the trait
Into<ActiveModel>is only implemented for ownedscrapes::Model, not&scrapes::Model. That's itInline, the compiler can move the owned value into
into(). In your function, you pass a reference, so no matching impl exist and you need.clone()first.