Generic function parameters take ownership when they are used within another function

I have a small application in which I am passing around the reference to a file in a lot of places.
Now I want to try memmap, and instead of changing all function signatures to accept that, I tried to go the generic route.
At first everything looked good, and I was able to pass a reference to that multiple times in main().
But as soon as I try to do that from another function, which also borrowed this parameter, the called procedure takes ownership if I pass the parameter directly.
Passing a reference instead also does not work because "the trait bound &impl Read + Seek: Seek is not satisfied" then.
There should a way to make this work, right?

This has no function, but it demonstrates my issue:

use std::{
    io::{BufRead, BufReader, Read, Seek, SeekFrom},

fn read_data(r: (impl Read + Seek)) {
    let mut reader = BufReader::with_capacity(10, r); as u64)).unwrap();

fn read_data_2(r: (impl Read + Seek)) {

fn main() {
    let file = File::open("test.bin").unwrap();



   Compiling playground v0.0.1 (/playground)
error[E0382]: use of moved value: `r`
  --> src/
12 | fn read_data_2(r: (impl Read + Seek)) {
   |                - move occurs because `r` has type `impl Read + Seek`, which does not implement the `Copy` trait
13 |     read_data(r);
   |               - value moved here
14 |     read_data(r);
   |               ^ value used here after move
note: consider changing this parameter type in function `read_data` to borrow instead if owning the value isn't necessary
  --> src/
6  | fn read_data(r: (impl Read + Seek)) {
   |    ---------     ^^^^^^^^^^^^^^^^ this parameter takes ownership of the value
   |    |
   |    in this function
help: consider further restricting this bound
12 | fn read_data_2(r: (impl Read + Seek + Copy)) {
   |                                     ++++++

For more information about this error, try `rustc --explain E0382`.
error: could not compile `playground` (lib) due to previous error

Pass mutable references.


Ok, that's less complicated that I thought.

Relevant docs.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.