-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch between shared and exclusive lock for
UserDataRef
depending …
…if `T: Sync` or not.
- Loading branch information
Showing
5 changed files
with
120 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1082,6 +1082,7 @@ mod cell; | |
mod lock; | ||
mod object; | ||
mod registry; | ||
mod util; | ||
|
||
#[cfg(test)] | ||
mod assertions { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use std::cell::Cell; | ||
use std::marker::PhantomData; | ||
|
||
// This is a trick to check if a type is `Sync` or not. | ||
// It uses leaked specialization feature from stdlib. | ||
struct IsSync<'a, T> { | ||
is_sync: &'a Cell<bool>, | ||
_marker: PhantomData<T>, | ||
} | ||
|
||
impl<T> Clone for IsSync<'_, T> { | ||
fn clone(&self) -> Self { | ||
self.is_sync.set(false); | ||
IsSync { | ||
is_sync: self.is_sync, | ||
_marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<T: Sync> Copy for IsSync<'_, T> {} | ||
|
||
pub(crate) fn is_sync<T>() -> bool { | ||
let is_sync = Cell::new(true); | ||
let _ = [IsSync::<T> { | ||
is_sync: &is_sync, | ||
_marker: PhantomData, | ||
}] | ||
.clone(); | ||
is_sync.get() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters