Skip to content

Commit 8d13fb2

Browse files
RUST-1891 Implement Default on results structs (#1289)
1 parent 974465a commit 8d13fb2

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

src/action/insert_many.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ impl<'a> Action for InsertMany<'a> {
121121
Ok(result) => {
122122
let current_batch_size = result.inserted_ids.len();
123123

124-
let cumulative_result =
125-
cumulative_result.get_or_insert_with(InsertManyResult::new);
124+
let cumulative_result = cumulative_result.get_or_insert_with(Default::default);
126125
for (index, id) in result.inserted_ids {
127126
cumulative_result
128127
.inserted_ids
@@ -184,7 +183,7 @@ impl<'a> Action for InsertMany<'a> {
184183
ErrorKind::InsertMany(failure),
185184
Some(error_labels),
186185
)),
187-
None => Ok(cumulative_result.unwrap_or_else(InsertManyResult::new)),
186+
None => Ok(cumulative_result.unwrap_or_default()),
188187
}
189188
}
190189
}

src/results.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub use bulk_write::*;
1919

2020
/// The result of a [`Collection::insert_one`](../struct.Collection.html#method.insert_one)
2121
/// operation.
22-
#[derive(Clone, Debug, Serialize)]
22+
#[derive(Clone, Debug, Serialize, Default)]
2323
#[serde(rename_all = "camelCase")]
2424
#[non_exhaustive]
2525
pub struct InsertOneResult {
@@ -37,26 +37,18 @@ impl InsertOneResult {
3737

3838
/// The result of a [`Collection::insert_many`](../struct.Collection.html#method.insert_many)
3939
/// operation.
40-
#[derive(Debug, Serialize)]
40+
#[derive(Debug, Serialize, Default)]
4141
#[serde(rename_all = "camelCase")]
4242
#[non_exhaustive]
4343
pub struct InsertManyResult {
4444
/// The `_id` field of the documents inserted.
4545
pub inserted_ids: HashMap<usize, Bson>,
4646
}
4747

48-
impl InsertManyResult {
49-
pub(crate) fn new() -> Self {
50-
InsertManyResult {
51-
inserted_ids: HashMap::new(),
52-
}
53-
}
54-
}
55-
5648
/// The result of a [`Collection::update_one`](../struct.Collection.html#method.update_one) or
5749
/// [`Collection::update_many`](../struct.Collection.html#method.update_many) operation.
5850
#[skip_serializing_none]
59-
#[derive(Clone, Debug, Serialize)]
51+
#[derive(Clone, Debug, Serialize, Default)]
6052
#[serde(rename_all = "camelCase")]
6153
#[non_exhaustive]
6254
pub struct UpdateResult {
@@ -74,7 +66,7 @@ pub struct UpdateResult {
7466

7567
/// The result of a [`Collection::delete_one`](../struct.Collection.html#method.delete_one) or
7668
/// [`Collection::delete_many`](../struct.Collection.html#method.delete_many) operation.
77-
#[derive(Clone, Debug, Serialize)]
69+
#[derive(Clone, Debug, Serialize, Default)]
7870
#[serde(rename_all = "camelCase")]
7971
#[non_exhaustive]
8072
pub struct DeleteResult {
@@ -85,7 +77,7 @@ pub struct DeleteResult {
8577

8678
/// Information about the index created as a result of a
8779
/// [`Collection::create_index`](../struct.Collection.html#method.create_index).
88-
#[derive(Debug, Clone, PartialEq)]
80+
#[derive(Debug, Clone, PartialEq, Default)]
8981
#[non_exhaustive]
9082
pub struct CreateIndexResult {
9183
/// The name of the index created in the `createIndex` command.
@@ -94,7 +86,7 @@ pub struct CreateIndexResult {
9486

9587
/// Information about the indexes created as a result of a
9688
/// [`Collection::create_indexes`](../struct.Collection.html#method.create_indexes).
97-
#[derive(Debug, Clone, PartialEq)]
89+
#[derive(Debug, Clone, PartialEq, Default)]
9890
#[non_exhaustive]
9991
pub struct CreateIndexesResult {
10092
/// The list containing the names of all indexes created in the `createIndexes` command.
@@ -120,14 +112,15 @@ pub(crate) struct GetMoreResult {
120112

121113
/// Describes the type of data store returned when executing
122114
/// [`Database::list_collections`](../struct.Database.html#method.list_collections).
123-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
115+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
124116
#[serde(rename_all = "camelCase")]
125117
#[non_exhaustive]
126118
pub enum CollectionType {
127119
/// Indicates that the data store is a view.
128120
View,
129121

130122
/// Indicates that the data store is a collection.
123+
#[default]
131124
Collection,
132125

133126
/// Indicates that the data store is a timeseries.
@@ -140,7 +133,7 @@ pub enum CollectionType {
140133
///
141134
/// See the MongoDB [manual](https://www.mongodb.com/docs/manual/reference/command/listCollections/#listCollections.cursor)
142135
/// for more information.
143-
#[derive(Debug, Clone, Deserialize, Serialize)]
136+
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
144137
#[serde(rename_all = "camelCase")]
145138
#[non_exhaustive]
146139
pub struct CollectionSpecificationInfo {
@@ -155,7 +148,7 @@ pub struct CollectionSpecificationInfo {
155148

156149
/// Information about a collection as reported by
157150
/// [`Database::list_collections`](../struct.Database.html#method.list_collections).
158-
#[derive(Debug, Clone, Deserialize, Serialize)]
151+
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
159152
#[serde(rename_all = "camelCase")]
160153
#[non_exhaustive]
161154
pub struct CollectionSpecification {
@@ -179,7 +172,7 @@ pub struct CollectionSpecification {
179172

180173
/// A struct modeling the information about an individual database returned from
181174
/// [`Client::list_databases`](../struct.Client.html#method.list_databases).
182-
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
175+
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Default)]
183176
#[serde(rename_all = "camelCase")]
184177
#[non_exhaustive]
185178
pub struct DatabaseSpecification {

0 commit comments

Comments
 (0)