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

Store SHOULD_CAPTURE as AtomicU8 #120528

Merged
merged 1 commit into from
Feb 4, 2024
Merged
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
12 changes: 6 additions & 6 deletions library/std/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::any::Any;
use crate::collections;
use crate::panicking;
use crate::sync::atomic::{AtomicUsize, Ordering};
use crate::sync::atomic::{AtomicU8, Ordering};
use crate::sync::{Mutex, RwLock};
use crate::thread::Result;

Expand Down Expand Up @@ -228,15 +228,15 @@ impl BacktraceStyle {
if cfg!(feature = "backtrace") { Some(BacktraceStyle::Full) } else { None }
}

fn as_usize(self) -> usize {
fn as_u8(self) -> u8 {
match self {
BacktraceStyle::Short => 1,
BacktraceStyle::Full => 2,
BacktraceStyle::Off => 3,
}
}

fn from_usize(s: usize) -> Option<Self> {
fn from_u8(s: u8) -> Option<Self> {
Some(match s {
0 => return None,
1 => BacktraceStyle::Short,
Expand All @@ -251,7 +251,7 @@ impl BacktraceStyle {
// that backtrace.
//
// Internally stores equivalent of an Option<BacktraceStyle>.
static SHOULD_CAPTURE: AtomicUsize = AtomicUsize::new(0);
static SHOULD_CAPTURE: AtomicU8 = AtomicU8::new(0);

/// Configure whether the default panic hook will capture and display a
/// backtrace.
Expand All @@ -264,7 +264,7 @@ pub fn set_backtrace_style(style: BacktraceStyle) {
// If the `backtrace` feature of this crate isn't enabled, skip setting.
return;
}
SHOULD_CAPTURE.store(style.as_usize(), Ordering::Release);
SHOULD_CAPTURE.store(style.as_u8(), Ordering::Release);
}

/// Checks whether the standard library's panic hook will capture and print a
Expand Down Expand Up @@ -296,7 +296,7 @@ pub fn get_backtrace_style() -> Option<BacktraceStyle> {
// to optimize away callers.
return None;
}
if let Some(style) = BacktraceStyle::from_usize(SHOULD_CAPTURE.load(Ordering::Acquire)) {
if let Some(style) = BacktraceStyle::from_u8(SHOULD_CAPTURE.load(Ordering::Acquire)) {
return Some(style);
}

Expand Down
Loading