Lifetime issue again

Hi

Another lifetime issue

struct SensorBuffer<'a> {
    conn: &'a Connection,
    fast_insert_statement: &'a Statement<'a>
}

impl<'a> SensorBuffer<'a> {

    pub fn new(conn: &'a Connection) -> Self {
        SensorBuffer { conn: conn,
                       fast_insert_statement: &conn.prepare("COPY sensor_values_cleaned(ts, value, sensor_id) FROM STDIN (FORMAT binary)").unwrap()
        }
    }
 }

}

So I get the error

fast_insert_statement: &conn.prepare("COPY sensor_values_cleaned(ts, value, sensor_id) FROM STDIN (FORMAT binary)").unwrap() temporary value does not live long enough

and

note: borrowed value must be valid for the lifetime 'a as defined on the impl at 78:1...
--> src/elements/bmos_sensor_values_sink_element.rs:78:1
 |
78 | impl<'a> SensorBuffer<'a> {

Is conn here not defined to live as long as SensorBuffer ie new(conn: &'a Connection) ?

You probably want

struct SensorBuffer<'a> {
    conn: &'a Connection,
    fast_insert_statement: Statement<'a>, <-- a value here, not a reference
}

Otherwise you're trying to grab a reference to a temporary Statement with no owner.

2 Likes

That did it .Thanks.