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

axum::extract::multipart::Field should implement Clone #1186

Closed
haiderlikesrust opened this issue Jul 23, 2022 · 2 comments
Closed

axum::extract::multipart::Field should implement Clone #1186

haiderlikesrust opened this issue Jul 23, 2022 · 2 comments

Comments

@haiderlikesrust
Copy link

haiderlikesrust commented Jul 23, 2022

Bug Report

Version

0.5.5

├── axum v0.5.5
│ ├── axum-core v0.2.4
│ ├── axum-core v0.2.4 (*)

Platform

Linux asus 5.18.12-arch1-1 #1 SMP PREEMPT_DYNAMIC Fri, 15 Jul 2022 15:33:02 +0000 x86_64 GNU/Linux

Description

axum::extract::multipart::Field should implement Clone its really hard to if a field.name() equals to what we are looking for because it can't be cloned, all getter methods are self not &self. Implementing Clone will make it easy or else its really hard.

fn create_post(mut form: Multipart) -> Result<Json<String>, ApiError> {
  while let Some(field) = form.next_field().await.unwrap() {
      // &self - no problem
      match field.name() {
         // Did call field.text() which is &self
         "foo" => {..}
         ...
         _ => ()
      }
      // Checking if the content_type is image or video and then processes it.
      // Error here because field was deleted by text() method
      match field.content_type() { ... }
   }
}
@haiderlikesrust haiderlikesrust changed the title axum::extract::Field should implement Clone axum::extract::multipart::Field should implement Clone Jul 23, 2022
@haiderlikesrust
Copy link
Author

Just made a pull request to fix it

@davidpdrsn
Copy link
Member

Field cannot be Clone because it contains a &mut Multipart. That is required to uphold an invariant.

Think of Field as a "pointer into a the request body stream" meaning it doesn't make sense to clone since you cannot consume a stream twice. That is why the Field::text takes self and is async.

Seems to me that putting a continue in your loop should fix your problem.

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