This project embodies an attempt to create a Discord/IRC (and possibly more extensible) bridge. Not finished.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.6 KiB

// Implements a type for keeping track of sets of Arcs without having to hash the inside every time an
// indexing operation happens. Not all functions are implemented, just the ones I need.
use std::sync::Arc;
use std::collections::hash_map::HashMap;
pub struct RefSet<T, U = Arc<T>> where
T: ?Sized,
U: AsRef<T>,
{
map: HashMap<usize, U>,
_pd: std::marker::PhantomData<T>,
}
type Iter<'a, U> = std::collections::hash_map::Values<'a, usize, U>;
impl<T, U> RefSet<T, U> where
U: AsRef<T>,
T: ?Sized
{
pub fn new() -> RefSet<T, U>
{
RefSet { map: HashMap::new(), _pd: std::marker::PhantomData }
}
#[allow(dead_code)]
pub fn with_capacity(capacity: usize) -> RefSet<T, U>
{
RefSet { map: HashMap::with_capacity(capacity), _pd: std::marker::PhantomData }
}
#[allow(dead_code)]
pub fn capacity(&self) -> usize
{
self.map.capacity()
}
pub fn iter(&self) -> Iter<U>
{
self.map.values()
}
#[allow(dead_code)]
pub fn len(&self) -> usize
{
self.map.len()
}
#[allow(dead_code)]
pub fn is_empty(&self) -> bool
{
self.map.is_empty()
}
#[allow(dead_code)]
pub fn clear(&mut self)
{
self.map.clear()
}
pub fn insert(&mut self, value: U) -> bool
{
match self.map.insert(value.as_ref() as *const T as *const () as usize, value)
{
Some(_) => false,
None => true,
}
}
#[allow(dead_code)]
pub fn remove(&mut self, value: U) -> bool
{
match self.map.remove(&(value.as_ref() as *const T as *const () as usize))
{
Some(_) => true,
None => false,
}
}
}