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

Don't require async functions passed to *_async handlers to have a 'static lifetime #16

Merged
merged 1 commit into from Aug 26, 2021

Conversation

jyn514
Copy link
Contributor

@jyn514 jyn514 commented Aug 24, 2021

I found this while trying to avoid copying in this example from the README:

    router.on_async("/proxy_request/*url", |_req, _env, params| {
        // Must copy the parameters into the heap here for lifetime purposes
        let url = params
            .get("url")
            .unwrap()
            .strip_prefix('/')
            .unwrap()
            .to_string();
        async move { Fetch::Url(&url).send().await }
    })?;

Unfortunately, that particular example still doesn't compile, but this now allows other borrows.

…'static lifetime

I found this while trying to avoid copying in this example from the README:

```
    router.on_async("/proxy_request/*url", |_req, _env, params| {
        // Must copy the parameters into the heap here for lifetime purposes
        let url = params
            .get("url")
            .unwrap()
            .strip_prefix('/')
            .unwrap()
            .to_string();
        async move { Fetch::Url(&url).send().await }
    })?;
```

Unfortunately, that particular example still doesn't compile, but this now allows other borrows.
@jyn514 jyn514 requested a review from nilslice August 24, 2021 21:41
@nilslice
Copy link
Contributor

Thanks, @jyn514 - gonna use this PR as the first for Actions / checks, so it may be a few hours before I merge but all looks good so far.

@nilslice
Copy link
Contributor

Tested manually as we're waiting for some internal things before we can enable Actions... LGTM. thanks!

@nilslice nilslice merged commit 9f095b0 into main Aug 26, 2021
@nilslice nilslice deleted the jnelson/not-static branch August 26, 2021 15:13
@nilslice
Copy link
Contributor

Just for posterity, I've changed the fn to use a hashmap for the parsed route params coming from the matchit Params instead of using them directly. While it's a little bit of a hit to allocations, it is way friendlier from a user experience point of view. Since only one route per request is ever parsed, the impact should be super minimal.

@jyn514
Copy link
Contributor Author

jyn514 commented Aug 30, 2021

Just for posterity, I've changed the fn to use a hashmap for the parsed route params coming from the matchit Params instead of using them directly. While it's a little bit of a hit to allocations, it is way friendlier from a user experience point of view. Since only one route per request is ever parsed, the impact should be super minimal.

Ah, ok - I think that should mean you no longer need to allocate in the example in the readme, then :)

@nilslice
Copy link
Contributor

Correct - it's been updated! Thanks.

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.

None yet

2 participants