rsmgclient
rsmgclient copied to clipboard
Place mutex on Connection
I would like to use a connection to MemGraph in rust as a state in different functions, however when I tried to place a mutex on a connection to share it safely, the compiler indicated that `Sync` is not implemented for `*mut rsmgclient::bindings::mg_session. Is there any other way to safely share a MemGraph connection across multiple threads? Thank you!
you can create a wrapper and force it to be send, it's just dangerous if they are not properly handling the raw pointer inside the library
struct SendSync<T>(T);
unsafe impl<T> Send for SendSync<T> {}
unsafe impl<T> Sync for SendSync<T> {}
impl<T> SendSync<T> {
fn new(value: T) -> Self {
Self(value)
}
}
impl<T> core::ops::Deref for SendSync<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
let connection = SendSync::new(connection);
Hi @cts660, did the above tip help you? Did you manage to safely share a MemGraph connection across multiple threads?