I used lazy_static
to construct a cassandra session, which is expected to be a parameter that is always connect(always used it). However, it has the following problems:
error[E0596]: cannot borrow data in a `&` reference as mutable
--> src/main.rs:745:29
|
745 | let data= cassandra_use(*SESSION, deviceid, epoch, feature);
| ^^^^^^^^ cannot borrow as mutable
The problematic code 1:
lazy_static! {
static ref SESSION: &'static mut CassSession = unsafe {
let cluster = create_cluster();
let mut session_new = &mut *cass_session_new();
let session_match = match connect_session(session_new, cluster) {
Ok(_) => {session_new}
_=> {
cass_cluster_free(cluster);
cass_session_free(session_new);
panic!();
}
};
session_match
};
}
The problematic code 2:
let data= cassandra_use(*SESSION, deviceid, epoch, feature);
I also tried to construct a single parameter like this:
lazy_static! {
static ref SESSION: &'static mut CassSession = unsafe {&mut *cass_session_new()};
}
But the problem is still the same:
error[E0596]: cannot borrow data in a `&` reference as mutable
--> src/main.rs:718:27
|
718 | match connect_session(*SESSION, cluster) {
| ^^^^^^^^ cannot borrow as mutable
The problematic code:
match connect_session(*SESSION, cluster) {
Ok(_) => {}
_ => {
cass_cluster_free(cluster);
cass_session_free(*SESSION);
panic!();
}
}
How can I fix the error?And lazy_static
must use the life parameter ('static
)?