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

Consume (some) body on drop #926

Closed
wants to merge 4 commits into from
Closed

Consume (some) body on drop #926

wants to merge 4 commits into from

Conversation

algesten
Copy link
Owner

@algesten algesten commented Jan 4, 2025

Close #94

@algesten
Copy link
Owner Author

algesten commented Jan 4, 2025

This is probably not possible to implement currently. If we introduce a Drop trait on Body or BodyReader, we end up in a situation that makes this code impossible:

let reader = ureq::get("http://httpbin.org/bytes/100")
    .call()?
    .body_mut()
    .with_config()
    // Reader will only read 50 bytes
    .limit(50)
    .reader();

The error is

error[E0716]: temporary value dropped while borrowed
  --> src/body/mod.rs:415:14
   |
6  |   let reader = ureq::get("https://httpbin.org/get")
   |  ______________^
7  | |     .call()?
   | |____________^ creates a temporary value which is freed while still in use
...
12 |       .reader();
   |                - temporary value is freed at the end of this statement
13 |   Ok::<_, ureq::Error>(())
14 |   } _doctest_main_src_body_mod_rs_412_0().unwrap() }
   |   - borrow might be used here, when `reader` is dropped and runs the `Drop` code for type `BodyReader`

I believe that introducing a Drop trait anywhere in the body-handling code, will trigger this error. The above code is important for ergonomics (using body_mut().xxx as opposed to intermediate let). Maybe this can be solved in future Rust.

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

Successfully merging this pull request may close these issues.

Attempt to return dropped Body to pool
1 participant