You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mongo-rust-driver/src/read_prefs.rs

60 lines
1.5 KiB
Rust

10 years ago
use mongo_c_driver_wrapper::bindings;
pub enum ReadMode {
Primary,
Secondary,
PrimaryPreferred,
SecondaryPreferred,
Nearest
}
fn read_mode_value(read_mode: &ReadMode) -> bindings::mongoc_read_mode_t {
match read_mode {
&ReadMode::Primary => bindings::MONGOC_READ_PRIMARY,
&ReadMode::Secondary => bindings::MONGOC_READ_SECONDARY,
&ReadMode::PrimaryPreferred => bindings::MONGOC_READ_PRIMARY_PREFERRED,
&ReadMode::SecondaryPreferred => bindings::MONGOC_READ_SECONDARY_PREFERRED,
&ReadMode::Nearest => bindings::MONGOC_READ_NEAREST
}
}
pub struct ReadPrefs {
inner: *mut bindings::mongoc_read_prefs_t
}
impl ReadPrefs {
pub fn new(read_mode: &ReadMode) -> ReadPrefs {
let read_mode_value = read_mode_value(read_mode);
let inner = unsafe { bindings::mongoc_read_prefs_new(read_mode_value) };
assert!(!inner.is_null());
ReadPrefs { inner: inner }
}
pub fn default() -> ReadPrefs{
ReadPrefs::new(&ReadMode::Primary)
}
pub fn inner(&self) -> *const bindings::mongoc_read_prefs_t {
assert!(!self.inner.is_null());
self.inner
}
}
impl Drop for ReadPrefs {
fn drop(&mut self) {
assert!(!self.inner.is_null());
unsafe {
bindings::mongoc_read_prefs_destroy(self.inner);
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_read_prefs() {
let read_prefs = super::ReadPrefs::default();
assert!(!read_prefs.inner().is_null());
}
}