From 97d22cc2415b63b48af661bfa166fe70b561f581 Mon Sep 17 00:00:00 2001 From: Thijs Cadier Date: Sun, 24 Jan 2021 22:08:46 +0100 Subject: [PATCH 1/3] Fix warnings on latest stable --- mongoc-sys/src/lib.rs | 2 +- src/bsonc.rs | 8 +++----- src/client.rs | 10 ++++----- src/collection.rs | 48 +++++++++++++++++++++---------------------- src/database.rs | 8 ++++---- src/error.rs | 13 +----------- src/lib.rs | 4 ++-- 7 files changed, 40 insertions(+), 53 deletions(-) diff --git a/mongoc-sys/src/lib.rs b/mongoc-sys/src/lib.rs index cf4640c..7ee2495 100644 --- a/mongoc-sys/src/lib.rs +++ b/mongoc-sys/src/lib.rs @@ -102,7 +102,7 @@ pub mod bindings { pub fn mongoc_database_get_collection(database: *mut mongoc_database_t, name: *const ::libc::c_char) -> *mut mongoc_collection_t; pub fn mongoc_database_get_name(database: *mut mongoc_database_t) -> *const ::libc::c_char; pub fn mongoc_database_destroy(database: *mut mongoc_database_t) -> (); - pub fn mongoc_database_has_collection(database: *mut mongoc_database_t, name: *const ::libc::c_char, error: *mut bson_error_t) -> ::libc::int32_t; + pub fn mongoc_database_has_collection(database: *mut mongoc_database_t, name: *const ::libc::c_char, error: *mut bson_error_t) -> i32; } // Client diff --git a/src/bsonc.rs b/src/bsonc.rs index 179b681..8820663 100644 --- a/src/bsonc.rs +++ b/src/bsonc.rs @@ -25,7 +25,7 @@ impl Bsonc { pub fn from_document(document: &bson::Document) -> Result { let mut buffer = Vec::new(); - try!(bson::encode_document(&mut buffer, document)); + bson::encode_document(&mut buffer, document)?; let inner = unsafe { bindings::bson_new_from_data( @@ -60,8 +60,7 @@ impl Bsonc { slice::from_raw_parts(data_ptr, data_len) }; - let document = try!(bson::decode_document(&mut slice)); - Ok(document) + Ok(bson::decode_document(&mut slice)?) } /// Decode a bson from the C side to a document with lossy UTF-8 decoding @@ -82,8 +81,7 @@ impl Bsonc { slice::from_raw_parts(data_ptr, data_len) }; - let document = try!(bson::decode_document_utf8_lossy(&mut slice)); - Ok(document) + Ok(bson::decode_document_utf8_lossy(&mut slice)?) } pub fn as_json(&self) -> String { diff --git a/src/client.rs b/src/client.rs index 433ef93..bfbe158 100644 --- a/src/client.rs +++ b/src/client.rs @@ -142,11 +142,11 @@ impl SslOptions { crl_file: Option, weak_cert_validation: bool ) -> io::Result { - let pem_file_cstring = try!(Self::cstring_from_path(&pem_file)); + let pem_file_cstring = Self::cstring_from_path(&pem_file)?; let pem_password_cstring = Self::cstring_from_string(&pem_password); - let ca_file_cstring = try!(Self::cstring_from_path(&ca_file)); - let ca_dir_cstring = try!(Self::cstring_from_path(&ca_dir)); - let crl_file_cstring = try!(Self::cstring_from_path(&crl_file)); + let ca_file_cstring = Self::cstring_from_path(&ca_file)?; + let ca_dir_cstring = Self::cstring_from_path(&ca_dir)?; + let crl_file_cstring = Self::cstring_from_path(&crl_file)?; let ssl_options = bindings::mongoc_ssl_opt_t { pem_file: match pem_file_cstring { @@ -192,7 +192,7 @@ impl SslOptions { fn cstring_from_path(path: &Option) -> io::Result> { match path { &Some(ref p) => { - try!(File::open(p.as_path())); + File::open(p.as_path())?; Ok(Some(CString::new(p.to_string_lossy().into_owned()).unwrap())) }, &None => Ok(None) diff --git a/src/collection.rs b/src/collection.rs index 729551f..2ce86f2 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -247,10 +247,10 @@ impl<'a> Collection<'a> { bindings::mongoc_collection_aggregate( self.inner, options.query_flags.flags(), - try!(Bsonc::from_document(pipeline)).inner(), + Bsonc::from_document(pipeline)?.inner(), match options.options { Some(ref o) => { - try!(Bsonc::from_document(o)).inner() + Bsonc::from_document(o)?.inner() }, None => ptr::null() }, @@ -292,7 +292,7 @@ impl<'a> Collection<'a> { options.skip, options.limit, options.batch_size, - try!(Bsonc::from_document(&command)).inner(), + Bsonc::from_document(&command)?.inner(), match fields_bsonc { Some(ref f) => f.inner(), None => ptr::null() @@ -331,7 +331,7 @@ impl<'a> Collection<'a> { let success = unsafe { bindings::mongoc_collection_command_simple( self.inner, - try!(Bsonc::from_document(&command)).inner(), + Bsonc::from_document(&command)?.inner(), match read_prefs { Some(ref prefs) => prefs.inner(), None => ptr::null() @@ -365,7 +365,7 @@ impl<'a> Collection<'a> { let default_options = CountOptions::default(); let options = options.unwrap_or(&default_options); let opts_bsonc = match options.opts { - Some(ref o) => Some(try!(Bsonc::from_document(o))), + Some(ref o) => Some(Bsonc::from_document(o)?), None => None }; @@ -374,7 +374,7 @@ impl<'a> Collection<'a> { bindings::mongoc_collection_count_with_opts( self.inner, options.query_flags.flags(), - try!(Bsonc::from_document(query)).inner(), + Bsonc::from_document(query)?.inner(), options.skip as i64, options.limit as i64, match opts_bsonc { @@ -458,7 +458,7 @@ impl<'a> Collection<'a> { options.skip, options.limit, options.batch_size, - try!(Bsonc::from_document(query)).inner(), + Bsonc::from_document(query)?.inner(), match fields_bsonc { Some(ref f) => f.inner(), None => ptr::null() @@ -505,13 +505,13 @@ impl<'a> Collection<'a> { // them around long enough. let sort_bsonc = match options.sort { Some(ref doc) => { - Some(try!(Bsonc::from_document(doc))) + Some(Bsonc::from_document(doc)?) }, None => None }; let update_bsonc = match operation { FindAndModifyOperation::Update(ref doc) | FindAndModifyOperation::Upsert(ref doc) => { - Some(try!(Bsonc::from_document(doc))) + Some(Bsonc::from_document(doc)?) }, FindAndModifyOperation::Remove => None }; @@ -519,7 +519,7 @@ impl<'a> Collection<'a> { let success = unsafe { bindings::mongoc_collection_find_and_modify( self.inner, - try!(Bsonc::from_document(&query)).inner(), + Bsonc::from_document(&query)?.inner(), match sort_bsonc { Some(ref s) => s.inner(), None => ptr::null() @@ -582,7 +582,7 @@ impl<'a> Collection<'a> { bindings::mongoc_collection_insert( self.inner, options.insert_flags.flags(), - try!(Bsonc::from_document(&document)).inner(), + Bsonc::from_document(&document)?.inner(), options.write_concern.inner(), error.mut_inner() ) @@ -613,7 +613,7 @@ impl<'a> Collection<'a> { bindings::mongoc_collection_remove( self.inner, options.remove_flags.flags(), - try!(Bsonc::from_document(&selector)).inner(), + Bsonc::from_document(&selector)?.inner(), options.write_concern.inner(), error.mut_inner() ) @@ -642,7 +642,7 @@ impl<'a> Collection<'a> { let success = unsafe { bindings::mongoc_collection_save( self.inner, - try!(Bsonc::from_document(&document)).inner(), + Bsonc::from_document(&document)?.inner(), write_concern.inner(), error.mut_inner() ) @@ -673,8 +673,8 @@ impl<'a> Collection<'a> { bindings::mongoc_collection_update( self.inner, options.update_flags.flags(), - try!(Bsonc::from_document(&selector)).inner(), - try!(Bsonc::from_document(&update)).inner(), + Bsonc::from_document(&selector)?.inner(), + Bsonc::from_document(&update)?.inner(), options.write_concern.inner(), error.mut_inner() ) @@ -757,7 +757,7 @@ impl<'a>BulkOperation<'a> { unsafe { bindings::mongoc_bulk_operation_insert( self.inner, - try!(Bsonc::from_document(&document)).inner() + Bsonc::from_document(&document)?.inner() ) } Ok(()) @@ -773,7 +773,7 @@ impl<'a>BulkOperation<'a> { unsafe { bindings::mongoc_bulk_operation_remove( self.inner, - try!(Bsonc::from_document(&selector)).inner() + Bsonc::from_document(&selector)?.inner() ) } Ok(()) @@ -789,7 +789,7 @@ impl<'a>BulkOperation<'a> { unsafe { bindings::mongoc_bulk_operation_remove_one( self.inner, - try!(Bsonc::from_document(&selector)).inner() + Bsonc::from_document(&selector)?.inner() ) } Ok(()) @@ -807,8 +807,8 @@ impl<'a>BulkOperation<'a> { unsafe { bindings::mongoc_bulk_operation_replace_one( self.inner, - try!(Bsonc::from_document(&selector)).inner(), - try!(Bsonc::from_document(&document)).inner(), + Bsonc::from_document(&selector)?.inner(), + Bsonc::from_document(&document)?.inner(), upsert as u8 ) } @@ -830,8 +830,8 @@ impl<'a>BulkOperation<'a> { unsafe { bindings::mongoc_bulk_operation_update_one( self.inner, - try!(Bsonc::from_document(&selector)).inner(), - try!(Bsonc::from_document(&document)).inner(), + Bsonc::from_document(&selector)?.inner(), + Bsonc::from_document(&document)?.inner(), upsert as u8 ) } @@ -853,8 +853,8 @@ impl<'a>BulkOperation<'a> { unsafe { bindings::mongoc_bulk_operation_update( self.inner, - try!(Bsonc::from_document(&selector)).inner(), - try!(Bsonc::from_document(&document)).inner(), + Bsonc::from_document(&selector)?.inner(), + Bsonc::from_document(&document)?.inner(), upsert as u8 ) } diff --git a/src/database.rs b/src/database.rs index 19c0f9f..a777899 100644 --- a/src/database.rs +++ b/src/database.rs @@ -79,7 +79,7 @@ impl<'a> Database<'a> { options.skip, options.limit, options.batch_size, - try!(Bsonc::from_document(&command)).inner(), + Bsonc::from_document(&command)?.inner(), match fields_bsonc { Some(ref f) => f.inner(), None => ptr::null() @@ -135,7 +135,7 @@ impl<'a> Database<'a> { let success = unsafe { bindings::mongoc_database_command_simple( self.inner, - try!(Bsonc::from_document(&command)).inner(), + Bsonc::from_document(&command)?.inner(), match read_prefs { Some(ref prefs) => prefs.inner(), None => ptr::null() @@ -166,7 +166,7 @@ impl<'a> Database<'a> { let mut error = BsoncError::empty(); let name_cstring = CString::new(name).unwrap(); let options_bsonc = match options { - Some(o) => Some(try!(Bsonc::from_document(o))), + Some(o) => Some(Bsonc::from_document(o)?), None => None }; @@ -263,4 +263,4 @@ fn test_get_coll_name_from_doc() { assert_eq!("cursor_items", get_coll_name_from_doc(&command).unwrap()); let command = doc! {"error": "cursor_items"}; assert!(get_coll_name_from_doc(&command).is_err()); -} \ No newline at end of file +} diff --git a/src/error.rs b/src/error.rs index ec78e0f..b3bae36 100644 --- a/src/error.rs +++ b/src/error.rs @@ -51,18 +51,7 @@ impl fmt::Debug for MongoError { } impl error::Error for MongoError { - fn description(&self) -> &str { - match *self { - MongoError::Bsonc(ref err) => err.description(), - MongoError::Decoder(ref err) => err.description(), - MongoError::Encoder(ref err) => err.description(), - MongoError::ValueAccessError(ref err) => err.description(), - MongoError::InvalidParams(ref err) => err.description(), - MongoError::Nul(ref err) => err.description() - } - } - - fn cause(&self) -> Option<&error::Error> { + fn cause(&self) -> Option<&dyn error::Error> { match *self { MongoError::Bsonc(ref err) => Some(err), MongoError::Decoder(ref err) => Some(err), diff --git a/src/lib.rs b/src/lib.rs index 12c233a..7f54a51 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,7 +38,7 @@ extern crate serde; use std::ffi::CStr; use std::ptr; use std::result; -use std::sync::{Once,ONCE_INIT}; +use std::sync::Once; use mongoc::bindings; @@ -61,7 +61,7 @@ pub type Result = result::Result; /// Result that's used in bulk operations. pub type BulkOperationResult = result::Result; -static MONGOC_INIT: Once = ONCE_INIT; +static MONGOC_INIT: Once = Once::new(); /// Init mongo driver, needs to be called once before doing /// anything else. From 727bf09e7098b2366c8e4c59973644e5d4473897 Mon Sep 17 00:00:00 2001 From: Thijs Cadier Date: Sun, 24 Jan 2021 22:30:55 +0100 Subject: [PATCH 2/3] Update use of tests dir --- tests/bson_encode_decode.rs | 5 +++++ tests/bulk_operation.rs | 6 ++++-- tests/client.rs | 4 ++++ tests/collection.rs | 5 ++++- tests/cursor.rs | 7 +++++-- tests/database.rs | 7 ++++++- tests/flags.rs | 2 ++ tests/read_prefs.rs | 2 ++ tests/tests.rs | 15 --------------- tests/uri.rs | 1 + tests/write_concern.rs | 2 ++ 11 files changed, 35 insertions(+), 21 deletions(-) delete mode 100644 tests/tests.rs diff --git a/tests/bson_encode_decode.rs b/tests/bson_encode_decode.rs index 3d0d73a..9c5ecf0 100644 --- a/tests/bson_encode_decode.rs +++ b/tests/bson_encode_decode.rs @@ -1,7 +1,12 @@ +extern crate bson; +extern crate chrono; +extern crate mongo_driver; + use chrono::prelude::*; use mongo_driver::client::{ClientPool,Uri}; +use bson::{bson,doc}; use bson::oid::ObjectId; use bson::spec::BinarySubtype; diff --git a/tests/bulk_operation.rs b/tests/bulk_operation.rs index 7a8c115..f9316b6 100644 --- a/tests/bulk_operation.rs +++ b/tests/bulk_operation.rs @@ -1,7 +1,9 @@ -use std::env; +extern crate bson; +extern crate mongo_driver; -use bson; +use std::env; +use bson::{bson,doc}; use mongo_driver::client::{ClientPool,Uri}; #[test] diff --git a/tests/client.rs b/tests/client.rs index ff9d4ee..0eac0c7 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -1,8 +1,12 @@ +extern crate bson; +extern crate mongo_driver; + use std::env; use std::path::PathBuf; use std::sync::Arc; use std::thread; +use bson::{bson,doc}; use mongo_driver::client::{ClientPool,SslOptions,Uri}; #[test] diff --git a/tests/collection.rs b/tests/collection.rs index e4f6ad7..aa9f28b 100644 --- a/tests/collection.rs +++ b/tests/collection.rs @@ -1,4 +1,7 @@ -use bson; +extern crate bson; +extern crate mongo_driver; + +use bson::{bson,doc}; use mongo_driver::CommandAndFindOptions; use mongo_driver::collection::{CountOptions,FindAndModifyOperation}; diff --git a/tests/cursor.rs b/tests/cursor.rs index f99ddb3..a633df4 100644 --- a/tests/cursor.rs +++ b/tests/cursor.rs @@ -1,8 +1,11 @@ +extern crate bson; +extern crate mongo_driver; + use std::sync::Arc; use std::thread; use std::time::Duration; -use bson; +use bson::{bson,doc}; use mongo_driver::client::{ClientPool,Uri}; use mongo_driver::Result; @@ -133,4 +136,4 @@ fn test_batch_cursor() { } collection.drop().unwrap(); -} \ No newline at end of file +} diff --git a/tests/database.rs b/tests/database.rs index 5acfed4..fb70c9e 100644 --- a/tests/database.rs +++ b/tests/database.rs @@ -1,3 +1,8 @@ +extern crate bson; +extern crate mongo_driver; + +use bson::{bson,doc}; + use mongo_driver::client::{ClientPool,Uri}; #[cfg_attr(target_os = "windows", ignore)] @@ -74,4 +79,4 @@ fn test_has_collection() { assert_eq!(COLL_NAME, collection.get_name().to_mut()); assert!(database.has_collection(COLL_NAME).unwrap()); -} \ No newline at end of file +} diff --git a/tests/flags.rs b/tests/flags.rs index 7dd40f5..d63f0d0 100644 --- a/tests/flags.rs +++ b/tests/flags.rs @@ -1,3 +1,5 @@ +extern crate mongo_driver; + use mongo_driver::flags::*; #[test] diff --git a/tests/read_prefs.rs b/tests/read_prefs.rs index 363eb05..f9cbdcd 100644 --- a/tests/read_prefs.rs +++ b/tests/read_prefs.rs @@ -1,3 +1,5 @@ +extern crate mongo_driver; + use mongo_driver::read_prefs::ReadPrefs; #[test] diff --git a/tests/tests.rs b/tests/tests.rs deleted file mode 100644 index 6f83d62..0000000 --- a/tests/tests.rs +++ /dev/null @@ -1,15 +0,0 @@ -extern crate mongo_driver; -extern crate chrono; -#[macro_use] -extern crate bson; - -mod bson_encode_decode; -mod bulk_operation; -mod client; -mod collection; -mod cursor; -mod database; -mod flags; -mod read_prefs; -mod uri; -mod write_concern; diff --git a/tests/uri.rs b/tests/uri.rs index ab65a9b..ac23632 100644 --- a/tests/uri.rs +++ b/tests/uri.rs @@ -1,3 +1,4 @@ +extern crate mongo_driver; use mongo_driver::client::Uri; #[test] diff --git a/tests/write_concern.rs b/tests/write_concern.rs index 04987b9..6fe45df 100644 --- a/tests/write_concern.rs +++ b/tests/write_concern.rs @@ -1,3 +1,5 @@ +extern crate mongo_driver; + use mongo_driver::write_concern::WriteConcern; #[test] From 984fc9ea2bc6eca63bceb7785fff2ab448679a96 Mon Sep 17 00:00:00 2001 From: Thijs Cadier Date: Sun, 24 Jan 2021 22:31:49 +0100 Subject: [PATCH 3/3] Move to 2018 edition --- Cargo.toml | 1 + src/bsonc.rs | 2 +- src/client.rs | 2 +- src/collection.rs | 4 ++-- src/cursor.rs | 2 +- src/database.rs | 4 ++-- src/error.rs | 2 +- src/flags.rs | 2 +- src/lib.rs | 4 ++-- src/read_prefs.rs | 2 +- src/write_concern.rs | 2 +- 11 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 07af0d5..de60dfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ repository = "https://github.com/thijsc/mongo-rust-driver" keywords = ["mongodb", "database"] categories = ["database", "api-bindings"] license = "MIT/Apache-2.0" +edition = "2018" [badges] travis-ci = { repository = "thijsc/mongo-rust-driver" } diff --git a/src/bsonc.rs b/src/bsonc.rs index 8820663..7fdf221 100644 --- a/src/bsonc.rs +++ b/src/bsonc.rs @@ -4,7 +4,7 @@ use std::fmt; use std::slice; use libc::c_void; -use mongoc::bindings; +use crate::mongoc::bindings; use bson; use super::Result; diff --git a/src/client.rs b/src/client.rs index bfbe158..9412cf4 100644 --- a/src/client.rs +++ b/src/client.rs @@ -11,7 +11,7 @@ use std::ptr; use std::io; use std::fs::File; -use mongoc::bindings; +use crate::mongoc::bindings; use bson::Document; diff --git a/src/collection.rs b/src/collection.rs index 2ce86f2..9c4ad9c 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -7,8 +7,8 @@ use std::ffi::CStr; use std::borrow::Cow; use std::time::Duration; -use mongoc::bindings; -use bsonc; +use crate::mongoc::bindings; +use crate::bsonc; use bson::Document; diff --git a/src/cursor.rs b/src/cursor.rs index 2212052..0238f9f 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -6,7 +6,7 @@ use std::thread; use std::time::Duration; use std::collections::VecDeque; -use mongoc::bindings; +use crate::mongoc::bindings; use bson::{self,Bson,Document,oid}; use super::BsoncError; diff --git a/src/database.rs b/src/database.rs index a777899..c4334f9 100644 --- a/src/database.rs +++ b/src/database.rs @@ -4,7 +4,7 @@ use std::ffi::{CString,CStr}; use std::borrow::Cow; use std::ptr; -use mongoc::bindings; +use crate::mongoc::bindings; use bson::Document; use super::Result; @@ -18,7 +18,7 @@ use super::cursor; use super::cursor::Cursor; use super::cursor::BatchCursor; use super::read_prefs::ReadPrefs; -use flags::FlagsValue; +use crate::flags::FlagsValue; #[doc(hidden)] pub enum CreatedBy<'a> { diff --git a/src/error.rs b/src/error.rs index b3bae36..60a8019 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,7 +6,7 @@ use std::ffi::CStr; use bson::{DecoderError,EncoderError,ValueAccessError,Document}; use std::ffi::NulError; -use mongoc::bindings; +use crate::mongoc::bindings; /// Wrapper for all errors that can occur in the driver. pub enum MongoError { diff --git a/src/flags.rs b/src/flags.rs index e3acc89..a866eff 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -1,6 +1,6 @@ //! Flags to configure various MongoDB operations. -use mongoc::bindings; +use crate::mongoc::bindings; use std::collections::BTreeSet; diff --git a/src/lib.rs b/src/lib.rs index 7f54a51..deb41c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,7 @@ use std::ptr; use std::result; use std::sync::Once; -use mongoc::bindings; +use crate::mongoc::bindings; pub mod client; pub mod collection; @@ -53,7 +53,7 @@ pub mod write_concern; mod bsonc; mod error; -pub use error::{MongoError,BsoncError,MongoErrorDomain,MongoErrorCode,InvalidParamsError,BulkOperationError}; +pub use crate::error::{MongoError,BsoncError,MongoErrorDomain,MongoErrorCode,InvalidParamsError,BulkOperationError}; /// Result that's used in all functions that perform operations on the database. pub type Result = result::Result; diff --git a/src/read_prefs.rs b/src/read_prefs.rs index 5cc9888..64ce537 100644 --- a/src/read_prefs.rs +++ b/src/read_prefs.rs @@ -1,6 +1,6 @@ //! Abstraction on top of the MongoDB connection read prefences. -use mongoc::bindings; +use crate::mongoc::bindings; /// Describes how reads should be dispatched. pub enum ReadMode { diff --git a/src/write_concern.rs b/src/write_concern.rs index 9e1f3cd..14e9b9b 100644 --- a/src/write_concern.rs +++ b/src/write_concern.rs @@ -1,6 +1,6 @@ //! Abstraction on top of the MongoDB connection write concern. -use mongoc::bindings; +use crate::mongoc::bindings; /// Possible write concern levels, only default is supported at the moment. pub enum WriteConcernLevel {