Skip to content

Commit

Permalink
Add Send trait bound
Browse files Browse the repository at this point in the history
This is required to prevent:
#1
  • Loading branch information
WalterSmuts committed Dec 25, 2022
1 parent 6b04f40 commit e6110fa
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::{cell::RefCell, ops::AddAssign};

use num_traits::{One, Zero};

fn generic_call_counter<T: Zero + One + Copy + AddAssign + 'static>() -> T {
fn generic_call_counter<T: Zero + One + Copy + AddAssign + Send + 'static>() -> T {
let mut count = generic_singleton::get_or_init!(|| RefCell::new(T::zero())).borrow_mut();
*count += T::one();
*count
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub extern crate lazy_static;
/// a * b
/// }
///
/// fn multiply_with_cache<T: Mul<Output = T>>(a: T, b: T) -> T
/// fn multiply_with_cache<T: Mul<Output = T> + Send>(a: T, b: T) -> T
/// where
/// T: std::cmp::Eq,
/// T: Copy,
Expand Down
6 changes: 3 additions & 3 deletions src/static_anymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct StaticAnyMap {
unsafe impl Sync for StaticAnyMap {}

impl StaticAnyMap {
fn get<T>(&'static self) -> Option<&'static T> {
fn get<T: Send>(&'static self) -> Option<&'static T> {
let read_only_guard = self.inner.read();
let val = read_only_guard.get::<Pin<Box<T>>>()?;
// SAFETY:
Expand All @@ -42,7 +42,7 @@ impl StaticAnyMap {
Some(unsafe { convert_to_static_ref(val) })
}

fn init_and_return<T>(&'static self, init: impl FnOnce() -> T) -> &'static T {
fn init_and_return<T: Send>(&'static self, init: impl FnOnce() -> T) -> &'static T {
let mut writeable_map = self.inner.write();

// Check again to be sure that the type wasn't inserted after we dropped the fast-path
Expand All @@ -67,7 +67,7 @@ impl StaticAnyMap {
unsafe { convert_to_static_ref(val) }
}

pub fn get_or_init<T>(&'static self, init: impl FnOnce() -> T) -> &'static T {
pub fn get_or_init<T: Send>(&'static self, init: impl FnOnce() -> T) -> &'static T {
if let Some(val) = self.get() {
val
} else {
Expand Down

0 comments on commit e6110fa

Please sign in to comment.