Skip to content

Instantly share code, notes, and snippets.

@hoelzro
Last active December 1, 2021 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoelzro/a4c15b4b99d8d79fb03084e91f34a9a4 to your computer and use it in GitHub Desktop.
Save hoelzro/a4c15b4b99d8d79fb03084e91f34a9a4 to your computer and use it in GitHub Desktop.
use std::fs::File;
use std::io::Read;
fn validate_signature(_f: impl Read) {
}
struct Headers{}
fn read_database_headers(_f: impl Read) -> Headers {
Headers{}
}
fn read_database(mut f: impl Read) {
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();
let mut bytes = buf.as_slice();
// using bytes here doesn't actually work, since the "cursor"/"position"
// within the buffer isn't ever updated - all reads from bytes when it's
// passed in start at the beginning (because the slice header can't get
// updated). What's surprising to me is that this *compiles*
validate_signature(bytes);
let _headers = read_database_headers(bytes);
let mut cipher_text = Vec::new();
bytes.read_to_end(&mut cipher_text).unwrap();
}
fn main() {
let f = File::open("/dev/null").unwrap();
read_database(f);
}
use std::fs::File;
use std::io::Read;
fn validate_signature(mut _f: impl Read) {
}
struct Headers{}
fn read_database_headers(mut _f: impl Read) -> Headers {
Headers{}
}
fn read_database(mut f: impl Read) {
validate_signature(&mut f);
let _headers = read_database_headers(&mut f);
let mut cipher_text = Vec::new();
f.read_to_end(&mut cipher_text).unwrap();
}
fn main() {
let f = File::open("/dev/null").unwrap();
read_database(f); // it surprises me that I don't need to declare f as `let mut f`, nor do I need to pass in `&mut f`
}
error[E0382]: use of moved value: `f`
--> src/main.rs:16:42
|
13 | fn read_database(f: impl Read) {
| - move occurs because `f` has type `impl Read`, which does not implement the `Copy` trait
14 | validate_signature(f);
| - value moved here
15 |
16 | let _headers = read_database_headers(f);
| ^ value used here after move
|
help: consider further restricting this bound
|
13 | fn read_database(f: impl Read + Copy) {
| ^^^^^^
error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
--> src/main.rs:19:5
|
13 | fn read_database(f: impl Read) {
| - help: consider changing this to be mutable: `mut f`
...
19 | f.read_to_end(&mut cipher_text).unwrap();
| ^ cannot borrow as mutable
error[E0382]: borrow of moved value: `f`
--> src/main.rs:19:5
|
13 | fn read_database(f: impl Read) {
| - move occurs because `f` has type `impl Read`, which does not implement the `Copy` trait
...
16 | let _headers = read_database_headers(f);
| - value moved here
...
19 | f.read_to_end(&mut cipher_text).unwrap();
| ^ value borrowed here after move
|
help: consider further restricting this bound
|
13 | fn read_database(f: impl Read + Copy) {
| ^^^^^^
Some errors have detailed explanations: E0382, E0596.
For more information about an error, try `rustc --explain E0382`.
use std::fs::File;
use std::io::Read;
fn validate_signature(_f: impl Read) {
}
struct Headers{}
fn read_database_headers(_f: impl Read) -> Headers {
Headers{}
}
fn read_database(f: impl Read) {
validate_signature(f);
let _headers = read_database_headers(f);
let mut cipher_text = Vec::new();
f.read_to_end(&mut cipher_text).unwrap();
}
fn main() {
let f = File::open("/dev/null").unwrap();
read_database(f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment