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

util: add LayerFn which implements Layer for a closure #267

Closed
seanmonstar opened this issue Apr 23, 2019 · 3 comments · Fixed by #491
Closed

util: add LayerFn which implements Layer for a closure #267

seanmonstar opened this issue Apr 23, 2019 · 3 comments · Fixed by #491
Labels
A-layer Area: The tower `Layer` trait A-new-middleware Area: new middleware proposals A-util Area: The tower "util" module C-feature-request Category: A feature request, i.e: not implemented / a PR. E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue. I-needs-decision Issues in need of decision. T-middleware Topic: middleware

Comments

@seanmonstar
Copy link
Collaborator

Many layers are very simple, and it'd be useful to have a way to create one from a closure.

Example implementation

#[derive(Clone, Copy, Debug)]
pub struct LayerFn<F>(F);

impl<F, S, Out> Layer<S> for LayerFn<F>
where
    F: Fn(S) -> Out,
{
    type Service = Out;

    fn layer(&self, inner: S) -> Self::Service {
        (self.0)(inner)
    }
}

Need to decide on an exposed name:

  • tower::layer_fn()
  • tower::layer::from_fn() (similar to std::iter::from_fn)
  • tower::layer::mk() (take from finagle, less clear)
@seanmonstar seanmonstar added the C-enhancement Category: A PR with an enhancement or a proposed on in an issue. label Apr 23, 2019
@carllerche
Copy link
Member

layer_fn to mirror service_fn 👍

@seanmonstar
Copy link
Collaborator Author

I'm kinda partial to following std::iter::from_fn...

@carllerche
Copy link
Member

Should service_fn be renamed then?

@jonhoo jonhoo added A-new-middleware Area: new middleware proposals A-util Area: The tower "util" module C-feature-request Category: A feature request, i.e: not implemented / a PR. E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue. I-needs-decision Issues in need of decision. T-middleware Topic: middleware A-layer Area: The tower `Layer` trait and removed C-enhancement Category: A PR with an enhancement or a proposed on in an issue. labels Mar 31, 2020
hawkw added a commit that referenced this issue Dec 29, 2020
Resolves #267

I went with `layer_fn` over `layer::from_fn` because changing
`service_fn` would be a breaking change. But I don't mind changing it if
you think thats more appropriate 😊 

Co-authored-by: Eliza Weisman <eliza@buoyant.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-layer Area: The tower `Layer` trait A-new-middleware Area: new middleware proposals A-util Area: The tower "util" module C-feature-request Category: A feature request, i.e: not implemented / a PR. E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue. I-needs-decision Issues in need of decision. T-middleware Topic: middleware
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants