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

Add portable-atomic support to tracing-core #3199

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions tracing-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ rust-version = "1.63.0"

[features]
default = ["std"]
alloc = []
std = ["once_cell", "alloc"]
alloc = ["portable-atomic-util?/alloc"]
std = ["once_cell", "alloc", "portable-atomic-util?/std"]
portable-atomic = ["dep:portable-atomic-util", "once_cell?/portable-atomic"]

[badges]
maintenance = { status = "actively-developed" }

[dependencies]
once_cell = { version = "1.13.0", optional = true }
portable-atomic-util = { version = "0.2.4", default-features = false, optional = true }

[package.metadata.docs.rs]
all-features = true
Expand Down
8 changes: 7 additions & 1 deletion tracing-core/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use crate::{span, Dispatch, Event, LevelFilter, Metadata};
use core::any::{Any, TypeId};
use core::ptr::NonNull;

#[cfg(all(feature = "alloc", not(feature = "portable-atomic")))]
use alloc::sync::Arc;

#[cfg(all(feature = "alloc", feature = "portable-atomic"))]
use portable_atomic_util::Arc;

/// Trait representing the functions required to collect trace data.
///
/// Crates that provide implementations of methods for collecting or recording
Expand Down Expand Up @@ -772,7 +778,7 @@ where
}

#[cfg(feature = "alloc")]
impl<C> Collect for alloc::sync::Arc<C>
impl<C> Collect for Arc<C>
where
C: Collect + ?Sized,
{
Expand Down
21 changes: 19 additions & 2 deletions tracing-core/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,12 @@ use std::{
error,
};

#[cfg(feature = "alloc")]
#[cfg(all(feature = "alloc", not(feature = "portable-atomic")))]
use alloc::sync::{Arc, Weak};

#[cfg(all(feature = "alloc", feature = "portable-atomic"))]
use portable_atomic_util::{Arc, Weak};

#[cfg(feature = "alloc")]
use core::ops::Deref;

Expand Down Expand Up @@ -539,8 +542,22 @@ impl Dispatch {
where
C: Collect + Send + Sync + 'static,
{
#[cfg(not(feature = "portable-atomic"))]
let arc = Arc::new(collector);

#[cfg(feature = "portable-atomic")]
let arc = {
use alloc::boxed::Box;

// Workaround for a lack of support for unsized coercion in non-first-party types.
// See https://github.com/rust-lang/rust/issues/18598
let boxed: Box<dyn Collect + Send + Sync> = Box::<C>::new(collector);

Arc::from(boxed)
};

let me = Dispatch {
collector: Kind::Scoped(Arc::new(collector)),
collector: Kind::Scoped(arc),
};
crate::callsite::register_dispatch(&me);
me
Expand Down