Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: don't use NoSubscriber as local placeholder (tokio-rs#2001)
## Motivation Currently, it is not actually possible to use `set_default(NoSubscriber)` or similar to temporarily disable the global default subscriber (see tokio-rs#1999). This is because `NoSubscriber` is currently used as a placeholder value when the thread-local cell that stores the current scoped default subscriber is initialized. Therefore, we currently check if the current scoped subscriber is `NoSubscriber`, and if it is, we fall back to returning the global default instead. This was fine, _when `NoSubscriber` was a private internal type only_. However, PR tokio-rs#1549 makes `NoSubscriber` into a public API type. When users can publicly construct `NoSubscriber` instances, it makes sense to want to be able to use `NoSubscriber` to disable the current subscriber. This is not possible when there is a global default set, because the local default being `NoSubscriber` will cause the global default to be returned. ## Solution This branch changes the thread-local cell to store an `Option<Dispatch>` instead, and use the `None` case to indicate no local default is set. This way, when the local default is explicitly set to `NoSubscriber`, we will return `NoSubscriber` rather than falling back. This may also be a slight performance improvement, because we now check if there's no global default by checking if the `Option` is `None`, rather than downcasting it to a `NoSubscriber`. I've also added a test reproducing tokio-rs#1999. Fixes tokio-rs#1999 Signed-off-by: Eliza Weisman <[email protected]>
- Loading branch information