Skip to content

Commit c2a20f3

Browse files
davidbarskyDavid Barsky
authored andcommitted
chore: fix deprecation and clippy warnings (#1195)
This branch fixes two known warnings. The first fixed warning was in `tracing-core/src/callsite.rs`, where clippy suggested using `std::ptr::eq` to compare addresses instead of casting `&T` to a `*const T`, which `&T` coerces to _anyways_. Below is the warning: ``` error: use `std::ptr::eq` when comparing raw pointers --> tracing-core/src/callsite.rs:238:9 | 238 | self.0 as *const _ as *const () == other.0 as *const _ as *const () | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::eq(self.0 as *const _, other.0 as *const _)` | = note: `-D clippy::ptr-eq` implied by `-D warnings` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_eq ``` The second fixed warning is a bit more complex. As of Rust 1.50, [`AtomicUsize::compare_and_swap`][1] has been deprecated in favor of [`AtomicUsize::compare_exchange`][2] and [`AtomicUsize::compare_exchange_weak`][3]. I've moved `tracing_core::dispatch::set_global_default` to use `AtomicUsize::compare_exchange` as `AtomicUsize::compare_exchange_weak` is designed for atomic loads in loops. Here are a few notes on the differences between `AtomicUsize::compare_and_swap` and `AtomicUsize::compare_exchange`: - `AtomicUsize::compare_exchange` returns a result, where the `Result::Ok(_)` branch contains the prior value. This is equivalent to the now-deprecated [`AtomicUsize::compare_and_swap`][1] returning the `current` parameter upon a successful compare-and-swap operation, but success is now established through a `Result`, not an equality operation. - `AtomicUsize::compare_exchange` accepts an `Ordering` for the failure case of a compare-and-swap. The [migration guide suggests][4] using `Ordering::SeqCst` for both the success and failure cases and I saw no reason to depart from its guidance. After this branch is approved, I'll backport these fixes to the `v0.1.0` branch. [1]: https://doc.rust-lang.org/nightly/std/sync/atomic/struct.AtomicUsize.html#method.compare_and_swap [2]: https://doc.rust-lang.org/nightly/std/sync/atomic/struct.AtomicUsize.html#method.compare_exchange [3]: https://doc.rust-lang.org/nightly/std/sync/atomic/struct.AtomicUsize.html#method.compare_exchange_weak [4]: https://doc.rust-lang.org/nightly/std/sync/atomic/struct.AtomicUsize.html#migrating-to-compare_exchange-and-compare_exchange_weak
1 parent ea91df3 commit c2a20f3

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

tracing-core/src/callsite.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ pub(crate) fn register_dispatch(dispatch: &Dispatch) {
149149

150150
impl PartialEq for Identifier {
151151
fn eq(&self, other: &Identifier) -> bool {
152-
self.0 as *const _ as *const () == other.0 as *const _ as *const ()
152+
core::ptr::eq(
153+
self.0 as *const _ as *const (),
154+
other.0 as *const _ as *const (),
155+
)
153156
}
154157
}
155158

tracing-core/src/dispatcher.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,16 @@ pub fn set_default(dispatcher: &Dispatch) -> DefaultGuard {
280280
/// [`Subscriber`]: ../subscriber/trait.Subscriber.html
281281
/// [`Event`]: ../event/struct.Event.html
282282
pub fn set_global_default(dispatcher: Dispatch) -> Result<(), SetGlobalDefaultError> {
283-
if GLOBAL_INIT.compare_and_swap(UNINITIALIZED, INITIALIZING, Ordering::SeqCst) == UNINITIALIZED
283+
// if `compare_exchange` returns Result::Ok(_), then `new` has been set and
284+
// `current`—now the prior value—has been returned in the `Ok()` branch.
285+
if GLOBAL_INIT
286+
.compare_exchange(
287+
UNINITIALIZED,
288+
INITIALIZING,
289+
Ordering::SeqCst,
290+
Ordering::SeqCst,
291+
)
292+
.is_ok()
284293
{
285294
unsafe {
286295
GLOBAL_DISPATCH = Some(dispatcher);

tracing/tests/support/subscriber.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ impl MockHandle {
536536
}
537537

538538
impl Expect {
539-
fn bad<'a>(&self, name: impl AsRef<str>, what: fmt::Arguments<'a>) {
539+
fn bad(&self, name: impl AsRef<str>, what: fmt::Arguments<'_>) {
540540
let name = name.as_ref();
541541
match self {
542542
Expect::Event(e) => panic!("[{}] expected event {}, but {} instead", name, e, what,),

0 commit comments

Comments
 (0)