Add Send + Sync + 'static bounds to K, V and S of sync and future Caches #19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds
Send
,Sync
and'static
bounds to the type parametersK
(key),V
(value) andS
(hasher state) of the following cache implementations:sync::Cache
sync::SegmentedCache
future::Cache
This will prevent the following usage from drawing undefined behavior:
Send
or non-Sync
type (such asstd::rc::Rc
) as key (K
) and/or (Value
).Until now, storing such key or value types was possible in single thread application (including single-threaded async runtime such as Actix-rt) because above caches did not ask these bounds explicitly.
However doing so is incorrect as it can draw undefined behavior. This is because these caches use multiple threads internally for updating internal data structures and evicting cached values.
Adding
Send
,Sync
and'static
bounds should prevent the problem by making such code not to compile by type errors.Note that multi-thread APIs such as
std::thread::spawn
or multi-threaded async runtime (such as the ones provided by Tokio and async-std) will ask those bounds explicitly. So there should have been no problem with them.*1: I wrote the code for illustrating purpose. If you actually run the code, it will always prints
2
. However, this does not mean you hit the UB, but it is actually an expected behavior.sync::Cache
andfuture::Cache
employ concurrent hash tablecht::SegmentedHashMap
as the main key value storage. cht will not drop a removed value immediately by the spec to provide high concurrency and thread safety. See this issue for more details: Gregory-Meyer/cht#23