当Rust结构包含终身特征时会发生什么?

时间:2018-09-20 14:41:22

标签: rust traits lifetime

好,所以我是Rust的新手,我正在尝试Rocket。该Web框架传递了一个Form<MyStruct>,我想将MyStruct转移到我自己的自定义结构中。

struct Consumer<T> {
    d: T,
}

impl<T> Consumer<T> {
   fn new(form: Form<T>) -> Self {
       Consumer { d: form.into_inner() }
   }
}

那当然不行,我明白了:

the trait `rocket::request::FromForm<'_>` is not implemented for `T`

下次尝试:

impl<T> Consumer<T> where T: FromForm {
    fn new(form: Form<T>) -> Self {
        Consumer { d: form.into_inner }
    }
}

哦,哦:

impl<T> Consumer<T> where T: FromForm {
                             ^^^^^^^^ expected lifetime parameter

所以现在我发现自己完全无法解决此问题!我能想到的最好的是:

impl<'f, T> Consumer<T> where T: FromForm<'f> {
    fn new(form: Form<T>) -> Self {
        Consumer { d: form.into_inner }
    }
}

但这会导致此错误:

51 |           fn new(form: Form<T>) -> Self {                                                                                      
   |  _________^                                                                                                                    
52 | |             Consumer { d: form.into_inner }                                                                                  
53 | |         }                                                                                                                    
   | |_________^ lifetime mismatch
  = note: expected type `rocket::request::FromForm<'_>`                                                                            
             found type `rocket::request::FromForm<'f>`

可验证的示例:https://hastebin.com/eqihaqodux.makefile

1 个答案:

答案 0 :(得分:5)

SELECT PA.RegCode UHID, PA.Specialisation, A.CREATEDATE ADM_ORDER, PA.CREATEDATE ADM_DATE, (CAST((DATEDIFF(minute, A.CREATEDATE, PA.CREATEDATE))/60 AS VARCHAR)+' H:' + CAST((DATEDIFF(minute,A.CREATEDATE, PA.CREATEDATE))%60 AS VARCHAR)+' M') AS DURATION FROM (SELECT M.IPID, M.PatientID, A.CREATEDATE FROM v_dbPatientAdvice A INNER JOIN v_dbPatientMonitoring M ON M.MonitorID = A.MonitorID WHERE A.FollowUpType = 2 ) A INNER JOIN (SELECT PA.PatientID, PA.CREATEDATE, PA.RegCode, S.Specialisation, PA.PatientType, PA.AdmissionID, pad.AdmissionID, pad.AdmTypeID -- New SELECT columns FROM v_dbPatientAdmissions PA INNER JOIN v_dbSpecializations S ON S.SpecialiseID = PA.SpecialiseID INNER JOIN V_DbPatientAdmissionAddition pad on pad.AdmissionID=pa.AdmissionID -- New JOIN WHERE pad.AdmTypeID in (3,37,42,43,44,51) -- new WHERE ) PA ON A.PatientID = PA.PatientID AND PA.PatientType = 2 AND PA.CREATEDATE > A.CREATEDATE AND PA.CREATEDATE < DATEADD(DAY, 1, A.CREATEDATE) WHERE A.CREATEDATE > '2017-01-01' AND A.CREATEDATE < DATEADD(DAY, 1, '2018-09-20') 也有一个生存期参数。如果将其与Form的生命周期相关联,那么您将前进一点:

FromForm

通常,如果您返回一个依赖于另一个对象中数据的对象,则需要像这样将它们的生命周期链接在一起。

这时,您将看到另一个错误,该错误可以方便地为您提供修复它所需的所有信息:

impl<'f, T> Consumer<T> where T: FromForm<'f> {
    fn new(form: Form<'f, T>) -> Self {
        Consumer(form.into_inner())
    }

    fn get(&self) -> &T {
        &self.0
    }
}

into_inner method on Form要求其类型参数error[E0310]: the parameter type `T` may not live long enough --> src/main.rs:50:17 | 48 | impl<'f, T> Consumer<T> where T: FromForm<'f> { | - help: consider adding an explicit lifetime bound `T: 'static`... 49 | fn new(form: Form<'f, T>) -> Self { 50 | Consumer(form.into_inner()) | ^^^^^^^^^^ | note: ...so that the type `T` will meet its required lifetime bounds --> src/main.rs:50:17 | 50 | Consumer(form.into_inner()) | ^^^^^^^^^^ 的生存期为T,并且错误消息建议添加此约束。

有了这些更改,它将编译:

'static