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

implement Middleware for common wrapper types #55

Open
ezracelli opened this issue Aug 29, 2022 · 3 comments
Open

implement Middleware for common wrapper types #55

ezracelli opened this issue Aug 29, 2022 · 3 comments
Labels
enhancement New feature or request reqwest-middleware Issues related to the core middleware lib

Comments

@ezracelli
Copy link

Motivations

I'm building a type-safe API client around an untyped REST API, and I'm trying to accept dynamic Middleware. Because this client's builder's stack is dynamic, the middleware needs to be represented as Box<dyn Middleware>, which Middleware is not implemented for.

Solution

Middleware should be implemented for common derivatives and wrapper types:

  • &T
  • &mut T
  • Arc<T>
  • Box<T>
  • Cell<T>
  • Cow<T>
  • MutexGuard<T>
  • Pin<T>
  • Rc<T>
  • Ref<T>
  • RefMut<T>

Alternatives

I'm currently wrapping the Box<dyn Middleware> with a newtype, like:

struct BoxedMiddleware(Box<dyn Middleware>);

and impl Middleware for BoxedMiddleware, but this will need to be replicated for each crate (I'll be building multiple wrapper clients), so it'd be much cleaner to have the impls in the source crate!

Additional context

Happy to submit a PR! 😁

@ezracelli ezracelli added the enhancement New feature or request label Aug 29, 2022
@conradludgate
Copy link
Collaborator

conradludgate commented Aug 30, 2022

Great idea, to avoid unnecessary bloat, I'd rather just &T, Box<T> and Arc<T> for now since these are the most common.

One thing to remember when implementing is to ensure that the ?Sized bound is specified otherwise Box<dyn Middleware> won't work (I sometimes forget this...)

impl<T: ?Sized + Middleware> ...
//      ^^^^^^

@conradludgate conradludgate added the reqwest-middleware Issues related to the core middleware lib label Aug 30, 2022
@conradludgate
Copy link
Collaborator

Looking back - I think implementing this on Arc<dyn Middleware> is a bit of an anti-pattern since we manually wrap them in arcs anyway. We should be promoting the use of with_arc instead where applicable

@ezracelli
Copy link
Author

Hmm, yeah that makes sense! I actually have no idea what's considered an anti-pattern here — is Arc<Arc<T>> a normal thing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request reqwest-middleware Issues related to the core middleware lib
Development

No branches or pull requests

2 participants