aboutsummaryrefslogtreecommitdiff
path: root/src/numbers/dynnum.rs
diff options
context:
space:
mode:
authorRunasSudo <runassudo@yingtongli.me>2021-10-19 15:31:22 +1100
committerRunasSudo <runassudo@yingtongli.me>2021-10-19 15:31:22 +1100
commitf120cf2eeeba83092e14c2b16f5b36d2776b85ee (patch)
treede30669f89fe5165fc44a8d2157117c526e7e706 /src/numbers/dynnum.rs
parent75ec78b1a66059e62d0a76563fa867ead4d3a2e2 (diff)
Make DynNum thread-safe
Diffstat (limited to 'src/numbers/dynnum.rs')
-rw-r--r--src/numbers/dynnum.rs20
1 files changed, 11 insertions, 9 deletions
diff --git a/src/numbers/dynnum.rs b/src/numbers/dynnum.rs
index eebfdb9..7e4c78b 100644
--- a/src/numbers/dynnum.rs
+++ b/src/numbers/dynnum.rs
@@ -20,6 +20,7 @@ use super::{Assign, Fixed, GuardedFixed, NativeFloat64, Number, Rational};
use num_traits::{Num, One, Zero};
//use wasm_bindgen::prelude::wasm_bindgen;
+use std::cell::Cell;
use std::cmp::{Ord, Ordering};
use std::fmt;
use std::mem::ManuallyDrop;
@@ -27,6 +28,7 @@ use std::ops::{self, Deref, DerefMut};
/// Represents the underlying implementation for [DynNum]s
//#[wasm_bindgen]
+#[derive(Copy, Clone)]
pub enum NumKind {
/// See [crate::numbers::fixed]
Fixed,
@@ -38,15 +40,15 @@ pub enum NumKind {
Rational,
}
-/// Determines which underlying implementation to use
-static mut KIND: NumKind = NumKind::Fixed;
+thread_local! {
+ /// Determines which underlying implementation to use
+ static KIND: Cell<NumKind> = Cell::new(NumKind::Fixed);
+}
/// Returns which underlying implementation is in use
#[inline]
-fn get_kind() -> &'static NumKind {
- unsafe {
- return &KIND;
- }
+fn get_kind() -> NumKind {
+ return KIND.with(|kind_cell| kind_cell.get());
}
/// A wrapper for different numeric types using dynamic dispatch
@@ -60,9 +62,9 @@ pub union DynNum {
impl DynNum {
/// Set which underlying implementation to use
pub fn set_kind(kind: NumKind) {
- unsafe {
- KIND = kind;
- }
+ KIND.with(|kind_cell| {
+ kind_cell.set(kind);
+ });
}
}
Contact (issues, pull requests, etc.) at git@yingtongli.me. Generated by cgit.