-
Notifications
You must be signed in to change notification settings - Fork 18
feat(layered): relax overly restrictive static bounds #373
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
base: main
Are you sure you want to change the base?
Changes from all commits
3f364c7
e7d01e2
1480390
43403ec
75872ad
a0f6770
14bc097
509f52f
a5a97ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -374,15 +374,15 @@ impl<In, Out, S> crate::Layer<S> for InterceptLayer<In, Out> { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| struct OnInput<In>(Arc<dyn Fn(&In) + Send + Sync>); | ||||||
| struct OnInput<In>(Arc<dyn for<'a> Fn(&'a In) + Send + Sync>); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think something like this will get you closer, but the hang-up is that the closure lifetime is much longer than &In... and the closure could borrow and hold using the ref. A stateless function, on the other hand, can't do that. So it solves the problem, but takes away closures. And users who need stateful calls would have to put their "context" data in A cheaper, backward compatible fix would be to document that this isn't supported :)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am starting to realize that this indeed might not be possible with current Rust :( |
||||||
|
|
||||||
| impl<In> Clone for OnInput<In> { | ||||||
| fn clone(&self) -> Self { | ||||||
| Self(Arc::clone(&self.0)) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| struct OnOutput<Out>(Arc<dyn Fn(&Out) + Send + Sync>); | ||||||
| struct OnOutput<Out>(Arc<dyn for<'a> Fn(&'a Out) + Send + Sync>); | ||||||
|
|
||||||
| impl<Out> Clone for OnOutput<Out> { | ||||||
| fn clone(&self) -> Self { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #![allow(missing_docs, reason = "This is a test module")] | ||
| #![cfg(not(miri))] | ||
|
|
||
| //! Integration tests for [`Execute`] service. | ||
|
|
||
| use layered::{Execute, Service}; | ||
|
|
||
| #[tokio::test] | ||
| async fn str_references() { | ||
| let service = Execute::new(|input: &str| async move { input }); | ||
|
|
||
| let input = "hello".to_string(); | ||
| let output = service.execute(input.as_str()).await; | ||
|
|
||
| assert_eq!(output, "hello"); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #![allow(missing_docs, reason = "This is a test module")] | ||
| #![cfg(feature = "intercept")] | ||
|
|
||
| //! Integration tests for [`Intercept`] middleware. | ||
|
|
||
| use layered::{Execute, Intercept, Service, Stack}; | ||
|
|
||
| #[cfg_attr(miri, ignore)] | ||
| #[tokio::test] | ||
| async fn str_references() { | ||
| let stack = ( | ||
| Intercept::<&str, &str, _>::layer() | ||
| .on_input(|input: &&str| { | ||
| assert!(!input.is_empty()); | ||
| }) | ||
| .on_output(|output: &&str| { | ||
| assert!(!output.is_empty()); | ||
| }), | ||
| Execute::new(|input: &str| async move { input }), | ||
| ); | ||
| let service = stack.into_service(); | ||
|
|
||
| let input = "hello".to_string(); | ||
| let output = service.execute(input.as_str()).await; | ||
|
|
||
| assert_eq!(output, "hello"); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.