Skip to content

Commit 44d79b8

Browse files
authored
RUST-1012 remove Clone impl from ClientSession (#450)
1 parent 653642a commit 44d79b8

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

src/client/session/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ lazy_static! {
9999
/// }
100100
/// }
101101
/// ```
102-
#[derive(Clone, Debug)]
102+
#[derive(Debug)]
103103
pub struct ClientSession {
104104
cluster_time: Option<ClusterTime>,
105105
server_session: ServerSession,

src/test/spec/unified_runner/entity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
Database,
1515
};
1616

17-
#[derive(Clone, Debug)]
17+
#[derive(Debug)]
1818
pub enum Entity {
1919
Client(ClientEntity),
2020
Database(Database),
@@ -33,7 +33,7 @@ pub struct ClientEntity {
3333
observe_sensitive_commands: bool,
3434
}
3535

36-
#[derive(Clone, Debug)]
36+
#[derive(Debug)]
3737
pub struct SessionEntity {
3838
pub lsid: Document,
3939
pub client_session: Option<Box<ClientSession>>,

src/test/spec/unified_runner/mod.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use semver::Version;
1212
use tokio::sync::RwLockWriteGuard;
1313

1414
use crate::{
15-
bson::{doc, Document},
15+
bson::{doc, Bson, Document},
1616
options::{CollectionOptions, FindOptions, ReadConcern, ReadPreference, SelectionCriteria},
1717
test::{run_single_test, run_spec_test, LOCK},
1818
RUNTIME,
@@ -140,14 +140,26 @@ pub async fn run_unified_format_test(test_file: TestFile) {
140140
.execute_entity_operation(id, &mut test_runner)
141141
.await;
142142

143+
// Precache the BSON result, if any, so that other entities can be saved without
144+
// cloning.
145+
#[derive(Debug)]
146+
enum ResultEntity {
147+
Bson(Bson),
148+
Other,
149+
None,
150+
}
151+
152+
let bson_result = match &result {
153+
Ok(Some(Entity::Bson(bson))) => Ok(ResultEntity::Bson(bson.clone())),
154+
Ok(Some(_)) => Ok(ResultEntity::Other),
155+
Ok(None) => Ok(ResultEntity::None),
156+
Err(e) => Err(e.clone()),
157+
};
158+
143159
if let Some(ref id) = operation.save_result_as_entity {
144-
match &result {
160+
match result {
145161
Ok(Some(entity)) => {
146-
if test_runner
147-
.entities
148-
.insert(id.clone(), entity.clone())
149-
.is_some()
150-
{
162+
if test_runner.entities.insert(id.clone(), entity).is_some() {
151163
panic!("Entity with id {} already present in entity map", id);
152164
}
153165
}
@@ -157,22 +169,19 @@ pub async fn run_unified_format_test(test_file: TestFile) {
157169
}
158170

159171
if let Some(expect_error) = operation.expect_error {
160-
let error = result
172+
let error = bson_result
161173
.expect_err(&format!("{} should return an error", operation.name));
162174
expect_error.verify_result(error);
163175
} else {
164-
let result = result.unwrap_or_else(|e| {
176+
let result = bson_result.unwrap_or_else(|e| {
165177
panic!(
166178
"{} should succeed, but the following error: {}",
167179
operation.name, e
168180
)
169181
});
170182
if let Some(ref expect_result) = operation.expect_result {
171-
let result = result.unwrap_or_else(|| {
172-
panic!("{} should return an entity", operation.name)
173-
});
174183
match result {
175-
Entity::Bson(ref result) => {
184+
ResultEntity::Bson(ref result) => {
176185
assert!(
177186
results_match(
178187
Some(result),
@@ -185,10 +194,13 @@ pub async fn run_unified_format_test(test_file: TestFile) {
185194
result
186195
);
187196
}
188-
_ => panic!(
197+
ResultEntity::Other => panic!(
189198
"Incorrect entity type returned from {}, expected BSON",
190199
operation.name
191200
),
201+
ResultEntity::None => {
202+
panic!("{} should return an entity", operation.name)
203+
}
192204
}
193205
}
194206
}

src/test/spec/unified_runner/operation.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ use crate::{
3535
},
3636
selection_criteria::ReadPreference,
3737
test::FailPoint,
38+
Collection,
39+
Database,
3840
IndexModel,
3941
RUNTIME,
4042
};
@@ -566,10 +568,19 @@ impl TestOperation for Aggregate {
566568
async move {
567569
let result = match &self.session {
568570
Some(session_id) => {
569-
let entity = test_runner.entities.get(id).unwrap().clone();
571+
enum AggregateEntity {
572+
Collection(Collection<Document>),
573+
Database(Database),
574+
Other(String),
575+
}
576+
let entity = match test_runner.entities.get(id).unwrap() {
577+
Entity::Collection(c) => AggregateEntity::Collection(c.clone()),
578+
Entity::Database(d) => AggregateEntity::Database(d.clone()),
579+
other => AggregateEntity::Other(format!("{:?}", other)),
580+
};
570581
let session = test_runner.get_mut_session(session_id);
571582
let mut cursor = match entity {
572-
Entity::Collection(collection) => {
583+
AggregateEntity::Collection(collection) => {
573584
collection
574585
.aggregate_with_session(
575586
self.pipeline.clone(),
@@ -578,15 +589,17 @@ impl TestOperation for Aggregate {
578589
)
579590
.await?
580591
}
581-
Entity::Database(db) => {
592+
AggregateEntity::Database(db) => {
582593
db.aggregate_with_session(
583594
self.pipeline.clone(),
584595
self.options.clone(),
585596
session,
586597
)
587598
.await?
588599
}
589-
other => panic!("Cannot execute aggregate on {:?}", &other),
600+
AggregateEntity::Other(debug) => {
601+
panic!("Cannot execute aggregate on {}", &debug)
602+
}
590603
};
591604
cursor
592605
.stream(session)

0 commit comments

Comments
 (0)