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 Default for Extension #1043

Merged
merged 2 commits into from May 18, 2022
Merged

Conversation

nolanderc
Copy link
Contributor

Motivation

When using an Extension we often have to wrap the inner value in an Arc (and some lock if we need mutability). If we then want to use our extension we need to write out Extension<Arc<Mutex<T>>> each time we want to use that T in a handler. This becomes tedious very quickly. Thus we introduce a type alias for that extension:

type MyExtension = Extension<Arc<Mutex<...>>>;

// which is used like:
async fn handler(ext: MyExtension) -> ... { ... }

With that type alias, it would be really nice if we could easily construct the Arc<Mutex<...>> type just by calling MyExtension::default(). However, Extension<T> does not implement Default, even though it is just a simple newtype wrapper around the inner T.

Solution

We just add an additional #[derive(Default)] to the Extension struct.

Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!

Wanna add a note to the changelog as well?

@nolanderc
Copy link
Contributor Author

Wanna add a note to the changelog as well?

Done!

Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@davidpdrsn davidpdrsn enabled auto-merge (squash) May 18, 2022 15:43
@davidpdrsn davidpdrsn merged commit d7c4e2f into tokio-rs:main May 18, 2022
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