diff --git a/examples/rados_striper.rs b/examples/rados_striper.rs index 4998c95..a66ac71 100644 --- a/examples/rados_striper.rs +++ b/examples/rados_striper.rs @@ -1,5 +1,8 @@ #[cfg(feature = "rados_striper")] -use {ceph::ceph as ceph_helpers, ceph::error::RadosError, nix::errno::Errno, std::env, std::str}; +use { + ceph::ceph as ceph_helpers, ceph::error::RadosError, nix::errno::Errno, std::env, std::str, + std::sync::Arc, +}; #[cfg(not(feature = "rados_striper"))] fn main() {} @@ -11,7 +14,7 @@ fn main() { let pool_name = "ceph-rust-test"; println!("Connecting to ceph"); - let cluster = ceph_helpers::connect_to_ceph(user_id, &config_file).unwrap(); + let cluster = Arc::new(ceph_helpers::connect_to_ceph(user_id, &config_file).unwrap()); println!("Creating pool {}", pool_name); match cluster.rados_create_pool(pool_name) { diff --git a/src/ceph.rs b/src/ceph.rs index 4001947..05c2040 100644 --- a/src/ceph.rs +++ b/src/ceph.rs @@ -32,6 +32,7 @@ use crate::rados_striper::*; use crate::status::*; use std::ffi::{CStr, CString}; use std::marker::PhantomData; +use std::sync::Arc; use std::{ptr, str}; use crate::utils::*; @@ -333,6 +334,7 @@ impl Iterator for XAttr { /// Owns a ioctx handle pub struct IoCtx { ioctx: rados_ioctx_t, + _rados: Arc, } unsafe impl Send for IoCtx {} @@ -480,7 +482,7 @@ impl Rados { /// Create an io context. The io context allows you to perform operations /// within a particular pool. /// For more details see rados_ioctx_t. - pub fn get_rados_ioctx(&self, pool_name: &str) -> RadosResult { + pub fn get_rados_ioctx(self: &Arc, pool_name: &str) -> RadosResult { self.conn_guard()?; let pool_name_str = CString::new(pool_name)?; unsafe { @@ -489,14 +491,17 @@ impl Rados { if ret_code < 0 { return Err(ret_code.into()); } - Ok(IoCtx { ioctx }) + Ok(IoCtx { + ioctx, + _rados: self.clone(), + }) } } /// Create an io context. The io context allows you to perform operations /// within a particular pool. /// For more details see rados_ioctx_t. - pub fn get_rados_ioctx2(&self, pool_id: i64) -> RadosResult { + pub fn get_rados_ioctx2(self: &Arc, pool_id: i64) -> RadosResult { self.conn_guard()?; unsafe { let mut ioctx: rados_ioctx_t = ptr::null_mut(); @@ -504,7 +509,10 @@ impl Rados { if ret_code < 0 { return Err(ret_code.into()); } - Ok(IoCtx { ioctx }) + Ok(IoCtx { + ioctx, + _rados: self.clone(), + }) } } }