Skip to content

Commit

Permalink
Rip send trait from AnyClone
Browse files Browse the repository at this point in the history
  • Loading branch information
JanBerktold committed Apr 9, 2024
1 parent c88f839 commit cc1cc35
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use std::fmt;
use std::hash::{BuildHasherDefault, Hasher};

type AnyMap = HashMap<TypeId, Box<dyn AnyClone + Send + Sync>, BuildHasherDefault<IdHasher>>;
type AnyMap = HashMap<TypeId, Box<dyn AnyClone>, BuildHasherDefault<IdHasher>>;

// With TypeIds as keys, there's no need to hash them. They are already hashes
// themselves, coming from the compiler. The IdHasher just holds the u64 of
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Extensions {
/// assert!(ext.insert(4u8).is_none());
/// assert_eq!(ext.insert(9i32), Some(5i32));
/// ```
pub fn insert<T: Clone + Send + Sync + 'static>(&mut self, val: T) -> Option<T> {
pub fn insert<T: Clone + 'static>(&mut self, val: T) -> Option<T> {
self.map
.get_or_insert_with(Box::default)
.insert(TypeId::of::<T>(), Box::new(val))
Expand All @@ -78,7 +78,7 @@ impl Extensions {
///
/// assert_eq!(ext.get::<i32>(), Some(&5i32));
/// ```
pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T> {
pub fn get<T: 'static>(&self) -> Option<&T> {
self.map
.as_ref()
.and_then(|map| map.get(&TypeId::of::<T>()))
Expand All @@ -97,7 +97,7 @@ impl Extensions {
///
/// assert_eq!(ext.get::<String>().unwrap(), "Hello World");
/// ```
pub fn get_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T> {
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
self.map
.as_mut()
.and_then(|map| map.get_mut(&TypeId::of::<T>()))
Expand All @@ -116,7 +116,7 @@ impl Extensions {
///
/// assert_eq!(*ext.get::<i32>().unwrap(), 3);
/// ```
pub fn get_or_insert<T: Clone + Send + Sync + 'static>(&mut self, value: T) -> &mut T {
pub fn get_or_insert<T: Clone + 'static>(&mut self, value: T) -> &mut T {
self.get_or_insert_with(|| value)
}

Expand All @@ -132,10 +132,7 @@ impl Extensions {
///
/// assert_eq!(*ext.get::<i32>().unwrap(), 3);
/// ```
pub fn get_or_insert_with<T: Clone + Send + Sync + 'static, F: FnOnce() -> T>(
&mut self,
f: F,
) -> &mut T {
pub fn get_or_insert_with<T: Clone + 'static, F: FnOnce() -> T>(&mut self, f: F) -> &mut T {
let out = self
.map
.get_or_insert_with(Box::default)
Expand All @@ -156,7 +153,7 @@ impl Extensions {
///
/// assert_eq!(*ext.get::<i32>().unwrap(), 2);
/// ```
pub fn get_or_insert_default<T: Default + Clone + Send + Sync + 'static>(&mut self) -> &mut T {
pub fn get_or_insert_default<T: Default + Clone + 'static>(&mut self) -> &mut T {
self.get_or_insert_with(T::default)
}

Expand All @@ -173,7 +170,7 @@ impl Extensions {
/// assert_eq!(ext.remove::<i32>(), Some(5i32));
/// assert!(ext.get::<i32>().is_none());
/// ```
pub fn remove<T: Send + Sync + 'static>(&mut self) -> Option<T> {
pub fn remove<T: 'static>(&mut self) -> Option<T> {
self.map
.as_mut()
.and_then(|map| map.remove(&TypeId::of::<T>()))
Expand Down Expand Up @@ -272,14 +269,14 @@ impl fmt::Debug for Extensions {
}

trait AnyClone: Any {
fn clone_box(&self) -> Box<dyn AnyClone + Send + Sync>;
fn clone_box(&self) -> Box<dyn AnyClone>;
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
fn into_any(self: Box<Self>) -> Box<dyn Any>;
}

impl<T: Clone + Send + Sync + 'static> AnyClone for T {
fn clone_box(&self) -> Box<dyn AnyClone + Send + Sync> {
impl<T: Clone + 'static> AnyClone for T {
fn clone_box(&self) -> Box<dyn AnyClone> {
Box::new(self.clone())
}

Expand All @@ -296,7 +293,7 @@ impl<T: Clone + Send + Sync + 'static> AnyClone for T {
}
}

impl Clone for Box<dyn AnyClone + Send + Sync> {
impl Clone for Box<dyn AnyClone> {
fn clone(&self) -> Self {
(**self).clone_box()
}
Expand Down

0 comments on commit cc1cc35

Please sign in to comment.