Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.session(...) builder func moves &mut ClientSession preventing it from being reused #1099

Closed
clarkmcc opened this issue May 10, 2024 · 2 comments
Assignees

Comments

@clarkmcc
Copy link

Versions/Environment

  1. What version of Rust are you using? rustc 1.78.0 (9b00956e5 2024-04-29)
  2. What operating system are you using? macOS M1
  3. What versions of the driver and its dependencies are you using? (Run
    cargo pkgid mongodb & cargo pkgid bson) main branch
  4. What version of MongoDB are you using? (Check with the MongoDB shell using db.version()) N/A
  5. What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)? N/A

Describe the bug

A clear and concise description of what the bug is.

After switching to the main branch, I can no longer use session on two separate

col.find_one(doc! {}).session(session).await;
col.find_one(doc! {}).session(session).await;
error[E0382]: use of moved value: `session`
  --> file.rs:21:48
   |
11 |     session: &mut ClientSession,
   |     ------- move occurs because `session` has type `&mut mongodb::ClientSession`, which does not implement the `Copy` trait
...
20 |     let supply = col.find_one(doc! {}).session(session).await;
   |                                                ------- value moved here
21 |     let supply = col.find_one(doc! {}).session(session).await;
   |                                                ^^^^^^^ value used here after move

For reference, I can get this to work

use_session(session);
use_session(session);

fn use_session(session: &mut ClientSession) {
    let session = session;
}

but I can reproduce the error with this

use_session(session);
use_session(session);

fn use_session<'a>(session: impl Into<&'a mut ClientSession>) {
    let session = session.into();
}

BE SPECIFIC:

  • What is the expected behavior and what is actually happening? I would expect to be able to use the session on multiple queries, but due to the Into semantics, Rust thinks the .session(...) method is moving the session.
  • Do you have any particular output that demonstrates this problem? N/A
  • Do you have any ideas on why this may be happening that could give us a
    clue in the right direction? Example illustrates why this is happening, and one potential fix.
  • Did this issue arise out of nowhere, or after an update (of the driver,
    server, and/or Rust)? After switching to the main branch.
clarkmcc added a commit to clarkmcc/mongo-rust-driver that referenced this issue May 10, 2024
@abr-egn abr-egn removed the triage label May 13, 2024
@abr-egn
Copy link
Contributor

abr-egn commented May 13, 2024

Using Into<&mut ClientSession> is needed here because there are different ClientSession types for the async and sync APIs, and this method needs to accept both. Unfortunately, this means that the Rust compiler will no longer insert an implicit reborrow of the reference, so explicit reborrows are required:

col.find_one(doc! {}).session(&mut *session).await;
col.find_one(doc! {}).session(&mut *session).await;

@clarkmcc
Copy link
Author

WIll do, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants