diff --git a/src/collections.rs b/src/collections.rs index 65463cd..0ad97d3 100644 --- a/src/collections.rs +++ b/src/collections.rs @@ -1,2 +1,2 @@ /// Defines [`BlazeMap`](crate::prelude::BlazeMap). -pub mod blazemap; \ No newline at end of file +pub mod blazemap; diff --git a/src/collections/blazemap.rs b/src/collections/blazemap.rs index 7772811..0309e22 100644 --- a/src/collections/blazemap.rs +++ b/src/collections/blazemap.rs @@ -9,19 +9,16 @@ use { crate::prelude::BlazeMapIdWrapper, serde::{ de::{MapAccess, Visitor}, - Deserialize, - Deserializer, ser::SerializeMap, - Serialize, - Serializer, + Deserialize, Deserializer, Serialize, Serializer, }, }; +use crate::collections::blazemap::entry::VacantEntryInner; pub use crate::collections::blazemap::{ entry::{Entry, OccupiedEntry, VacantEntry}, iter::{Drain, IntoIter, IntoKeys, IntoValues, Iter, IterMut, Keys, Values, ValuesMut}, }; -use crate::collections::blazemap::entry::VacantEntryInner; use crate::id_wrapper::BlazeMapId; use crate::orig_type_id_map::StaticInfoApi; @@ -30,15 +27,13 @@ mod iter; /// A [`Vec`]-based analogue of a [`HashMap`](std::collections::HashMap). #[derive(Clone, PartialEq, Eq)] -pub struct BlazeMap -{ +pub struct BlazeMap { inner: Vec>, len: usize, phantom: PhantomData, } -impl BlazeMap -{ +impl BlazeMap { /// Creates a new instance of the [`BlazeMap`]. #[inline] pub const fn new() -> Self { @@ -73,7 +68,9 @@ impl BlazeMap /// and possibly leaving some space in accordance with the resize policy. #[inline] pub fn shrink_to_fit(&mut self) { - let elems_to_crop = self.inner.iter() + let elems_to_crop = self + .inner + .iter() .rev() .position(Option::is_some) .unwrap_or(0); @@ -107,24 +104,20 @@ impl BlazeMap /// An iterator visiting all keys. The iterator element type is `K`. #[inline] pub fn keys(&self) -> Keys { - Keys { - inner: self.iter() - } + Keys { inner: self.iter() } } /// An iterator visiting all values. The iterator element type is `&'a V`. #[inline] pub fn values(&self) -> Values { - Values { - inner: self.iter() - } + Values { inner: self.iter() } } /// An iterator visiting all values mutably. The iterator element type is `&'a mut V`. #[inline] pub fn values_mut(&mut self) -> ValuesMut { ValuesMut { - inner: self.iter_mut() + inner: self.iter_mut(), } } @@ -137,22 +130,20 @@ impl BlazeMap #[inline] pub fn drain(&mut self) -> Drain { Drain { - inner: self.iter_mut() + inner: self.iter_mut(), } } } impl BlazeMap - where - K: BlazeMapId +where + K: BlazeMapId, { /// Creates a new instance of the [`BlazeMap`] /// with capacity equal to the current total number of unique `K` instances. #[inline] pub fn with_current_key_wrapper_capacity() -> Self { - let current_capacity = K::static_info() - .read() - .num_elems(); + let current_capacity = K::static_info().read().num_elems(); Self { inner: Vec::with_capacity(current_capacity), len: 0, @@ -172,17 +163,13 @@ impl BlazeMap /// Returns a reference to the value corresponding to the key. #[inline] pub fn get(&self, key: K) -> Option<&V> { - self.inner - .get(key.get_index()) - .and_then(Option::as_ref) + self.inner.get(key.get_index()).and_then(Option::as_ref) } /// Returns a mutable reference to the value corresponding to the key. #[inline] pub fn get_mut(&mut self, key: K) -> Option<&mut V> { - self.inner - .get_mut(key.get_index()) - .and_then(Option::as_mut) + self.inner.get_mut(key.get_index()).and_then(Option::as_mut) } /// Inserts a key-value pair into the map. @@ -198,9 +185,7 @@ impl BlazeMap entry.insert(value); None } - Entry::Occupied(mut entry) => { - Some(entry.insert(value)) - } + Entry::Occupied(mut entry) => Some(entry.insert(value)), } } @@ -251,7 +236,7 @@ impl BlazeMap #[inline] pub fn into_keys(self) -> IntoKeys { IntoKeys { - inner: self.into_iter() + inner: self.into_iter(), } } @@ -260,29 +245,27 @@ impl BlazeMap #[inline] pub fn into_values(self) -> IntoValues { IntoValues { - inner: self.into_iter() + inner: self.into_iter(), } } } impl IntoIterator for BlazeMap - where - K: BlazeMapId +where + K: BlazeMapId, { type Item = (K, V); type IntoIter = IntoIter; #[inline] fn into_iter(self) -> IntoIter { - IntoIter { - inner: self - } + IntoIter { inner: self } } } impl<'a, K, V> IntoIterator for &'a BlazeMap - where - K: BlazeMapId +where + K: BlazeMapId, { type Item = (K, &'a V); type IntoIter = Iter<'a, K, V>; @@ -294,8 +277,8 @@ impl<'a, K, V> IntoIterator for &'a BlazeMap } impl<'a, K, V> IntoIterator for &'a mut BlazeMap - where - K: BlazeMapId +where + K: BlazeMapId, { type Item = (K, &'a mut V); type IntoIter = IterMut<'a, K, V>; @@ -307,20 +290,20 @@ impl<'a, K, V> IntoIterator for &'a mut BlazeMap } impl FromIterator<(K, V)> for BlazeMap - where - K: BlazeMapId +where + K: BlazeMapId, { #[inline] - fn from_iter>(iter: T) -> Self { + fn from_iter>(iter: T) -> Self { let mut result = BlazeMap::with_current_key_wrapper_capacity(); - iter.into_iter() - .for_each(|(key, value)| { result.insert(key, value); }); + iter.into_iter().for_each(|(key, value)| { + result.insert(key, value); + }); result } } -impl Default for BlazeMap -{ +impl Default for BlazeMap { #[inline] fn default() -> Self { Self::new() @@ -331,23 +314,23 @@ macro_rules! blaze_map_orig_key_blocking_iter { ($self:ident, $iter:ident, $guard:ident) => { let $guard = K::static_info(); let $guard = $guard.read(); - let $iter = $self.inner.iter() + let $iter = $self + .inner + .iter() .enumerate() .filter_map(|(idx, value)| Some((idx, value.as_ref()?))) - .map( - |(idx, value)| { - let key = unsafe { $guard.get_key_unchecked(idx) }; - (key, value) - } - ); - } + .map(|(idx, value)| { + let key = unsafe { $guard.get_key_unchecked(idx) }; + (key, value) + }); + }; } impl Debug for BlazeMap - where - K: BlazeMapId, - ::OrigType: Debug, - V: Debug +where + K: BlazeMapId, + ::OrigType: Debug, + V: Debug, { #[inline] fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { @@ -362,15 +345,15 @@ impl Debug for BlazeMap #[cfg(feature = "serde")] impl Serialize for BlazeMap - where - K: BlazeMapId, - ::OrigType: Serialize, - V: Serialize +where + K: BlazeMapId, + ::OrigType: Serialize, + V: Serialize, { #[inline] fn serialize(&self, serializer: S) -> Result - where - S: Serializer + where + S: Serializer, { blaze_map_orig_key_blocking_iter!(self, iter, guard); let mut serializer = serializer.serialize_map(Some(self.len))?; @@ -383,15 +366,15 @@ impl Serialize for BlazeMap #[cfg(feature = "serde")] impl<'de, K, V> Deserialize<'de> for BlazeMap - where - K: BlazeMapIdWrapper, - ::OrigType: Deserialize<'de>, - V: Deserialize<'de> +where + K: BlazeMapIdWrapper, + ::OrigType: Deserialize<'de>, + V: Deserialize<'de>, { #[inline] fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de> + where + D: Deserializer<'de>, { deserializer.deserialize_map(BlazeMapDeserializer(Default::default())) } @@ -402,10 +385,10 @@ struct BlazeMapDeserializer(PhantomData<(K, V)>); #[cfg(feature = "serde")] impl<'de, K, V> Visitor<'de> for BlazeMapDeserializer - where - K: BlazeMapIdWrapper, - ::OrigType: Deserialize<'de>, - V: Deserialize<'de> +where + K: BlazeMapIdWrapper, + ::OrigType: Deserialize<'de>, + V: Deserialize<'de>, { type Value = BlazeMap; @@ -416,8 +399,8 @@ impl<'de, K, V> Visitor<'de> for BlazeMapDeserializer #[inline] fn visit_map(self, mut map: A) -> Result - where - A: MapAccess<'de> + where + A: MapAccess<'de>, { let mut result = BlazeMap::with_current_key_wrapper_capacity(); @@ -428,4 +411,4 @@ impl<'de, K, V> Visitor<'de> for BlazeMapDeserializer result.shrink_to_fit(); Ok(result) } -} \ No newline at end of file +} diff --git a/src/collections/blazemap/entry.rs b/src/collections/blazemap/entry.rs index ea4ea90..1f26916 100644 --- a/src/collections/blazemap/entry.rs +++ b/src/collections/blazemap/entry.rs @@ -8,8 +8,8 @@ use crate::prelude::BlazeMapId; /// /// [`entry`]: crate::collections::blazemap::BlazeMap::entry pub enum Entry<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { /// An occupied entry. Occupied(OccupiedEntry<'a, K, V>), @@ -21,46 +21,39 @@ pub enum Entry<'a, K, V> /// A view into an occupied entry in a [`BlazeMap`](crate::collections::blazemap::BlazeMap). /// It is part of the [`Entry`] enum. pub struct OccupiedEntry<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { - pub(in crate::collections::blazemap) - key: K, + pub(in crate::collections::blazemap) key: K, - pub(in crate::collections::blazemap) - len: &'a mut usize, + pub(in crate::collections::blazemap) len: &'a mut usize, - pub(in crate::collections::blazemap) - value: &'a mut Option, + pub(in crate::collections::blazemap) value: &'a mut Option, } #[derive(Debug)] /// A view into a vacant entry in a [`BlazeMap`](crate::collections::blazemap::BlazeMap). /// It is part of the [`Entry`] enum. pub struct VacantEntry<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { - pub(in crate::collections::blazemap) - key: K, + pub(in crate::collections::blazemap) key: K, - pub(in crate::collections::blazemap) - len: &'a mut usize, + pub(in crate::collections::blazemap) len: &'a mut usize, - pub(in crate::collections::blazemap) - inner: VacantEntryInner<'a, V>, + pub(in crate::collections::blazemap) inner: VacantEntryInner<'a, V>, } #[derive(Debug)] -pub(in crate::collections::blazemap) -enum VacantEntryInner<'a, V> { +pub(in crate::collections::blazemap) enum VacantEntryInner<'a, V> { ShouldBeInserted(&'a mut Option), ShouldBeEnlarged(&'a mut Vec>), } impl<'a, K, V> Entry<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { /// Ensures a value is in the entry by inserting the default if empty, /// and returns a mutable reference to the value in the entry. @@ -106,9 +99,9 @@ impl<'a, K, V> Entry<'a, K, V> } impl<'a, K, V> Entry<'a, K, V> - where - K: BlazeMapId, - V: Default +where + K: BlazeMapId, + V: Default, { /// Ensures a value is in the entry by inserting the default value if empty, /// and returns a mutable reference to the value in the entry. @@ -122,8 +115,8 @@ impl<'a, K, V> Entry<'a, K, V> } impl<'a, K, V> OccupiedEntry<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { /// Gets the key in the entry. #[inline] @@ -134,11 +127,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> /// Take the ownership of the key and value from the map. #[inline] pub fn remove_entry(self) -> (K, V) { - let Self { - key, - len, - value - } = self; + let Self { key, len, value } = self; *len -= 1; let value = unsafe { value.take().unwrap_unchecked() }; (key, value) @@ -181,19 +170,15 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> /// Takes the value out of the entry, and returns it. #[inline] pub fn remove(self) -> V { - let Self { - len, - value, - .. - } = self; + let Self { len, value, .. } = self; *len -= 1; unsafe { value.take().unwrap_unchecked() } } } impl<'a, K, V> VacantEntry<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { /// Gets the key that would be used when inserting a value through the [`VacantEntry`]. #[inline] @@ -205,19 +190,13 @@ impl<'a, K, V> VacantEntry<'a, K, V> /// and returns a mutable reference to it. #[inline] pub fn insert(self, value: V) -> &'a mut V { - let Self { - key, - len, - inner - } = self; + let Self { key, len, inner } = self; *len += 1; let reference = match inner { VacantEntryInner::ShouldBeInserted(reference) => reference, VacantEntryInner::ShouldBeEnlarged(vec) => { let index = key.get_index(); - let new_len = index - .checked_add(1) - .expect("usize overflow"); + let new_len = index.checked_add(1).expect("usize overflow"); vec.resize_with(new_len, || None); unsafe { vec.get_unchecked_mut(index) } } @@ -225,4 +204,4 @@ impl<'a, K, V> VacantEntry<'a, K, V> *reference = Some(value); unsafe { reference.as_mut().unwrap_unchecked() } } -} \ No newline at end of file +} diff --git a/src/collections/blazemap/iter.rs b/src/collections/blazemap/iter.rs index 7f56148..682bada 100644 --- a/src/collections/blazemap/iter.rs +++ b/src/collections/blazemap/iter.rs @@ -16,19 +16,14 @@ use crate::prelude::BlazeMapId; /// documentation for more. /// /// [`iter`]: BlazeMap::iter -pub struct Iter<'a, K, V> -{ - pub(in crate::collections::blazemap) - inner: *const Option, +pub struct Iter<'a, K, V> { + pub(in crate::collections::blazemap) inner: *const Option, - pub(in crate::collections::blazemap) - current_position: usize, + pub(in crate::collections::blazemap) current_position: usize, - pub(in crate::collections::blazemap) - len: usize, + pub(in crate::collections::blazemap) len: usize, - pub(in crate::collections::blazemap) - phantom: PhantomData<(K, &'a V)>, + pub(in crate::collections::blazemap) phantom: PhantomData<(K, &'a V)>, } /// A mutable iterator over the entries of a [`BlazeMap`]. @@ -37,19 +32,14 @@ pub struct Iter<'a, K, V> /// documentation for more. /// /// [`iter_mut`]: BlazeMap::iter_mut -pub struct IterMut<'a, K, V> -{ - pub(in crate::collections::blazemap) - inner: *mut Option, +pub struct IterMut<'a, K, V> { + pub(in crate::collections::blazemap) inner: *mut Option, - pub(in crate::collections::blazemap) - current_position: usize, + pub(in crate::collections::blazemap) current_position: usize, - pub(in crate::collections::blazemap) - len: usize, + pub(in crate::collections::blazemap) len: usize, - pub(in crate::collections::blazemap) - phantom: PhantomData<(K, &'a mut V)>, + pub(in crate::collections::blazemap) phantom: PhantomData<(K, &'a mut V)>, } /// An iterator over the keys of a [`BlazeMap`]. @@ -58,10 +48,8 @@ pub struct IterMut<'a, K, V> /// documentation for more. /// /// [`keys`]: BlazeMap::keys -pub struct Keys<'a, K, V> -{ - pub(in crate::collections::blazemap) - inner: Iter<'a, K, V>, +pub struct Keys<'a, K, V> { + pub(in crate::collections::blazemap) inner: Iter<'a, K, V>, } /// An iterator over the values of a [`BlazeMap`]. @@ -70,10 +58,8 @@ pub struct Keys<'a, K, V> /// documentation for more. /// /// [`values`]: BlazeMap::values -pub struct Values<'a, K, V> -{ - pub(in crate::collections::blazemap) - inner: Iter<'a, K, V>, +pub struct Values<'a, K, V> { + pub(in crate::collections::blazemap) inner: Iter<'a, K, V>, } /// A mutable iterator over the values of a [`BlazeMap`]. @@ -82,10 +68,8 @@ pub struct Values<'a, K, V> /// documentation for more. /// /// [`values_mut`]: BlazeMap::values_mut -pub struct ValuesMut<'a, K, V> -{ - pub(in crate::collections::blazemap) - inner: IterMut<'a, K, V>, +pub struct ValuesMut<'a, K, V> { + pub(in crate::collections::blazemap) inner: IterMut<'a, K, V>, } /// An owning iterator over the entries of a [`BlazeMap`]. @@ -94,10 +78,8 @@ pub struct ValuesMut<'a, K, V> /// (provided by the [`IntoIterator`] trait). See its documentation for more. /// /// [`into_iter`]: IntoIterator::into_iter -pub struct IntoIter -{ - pub(in crate::collections::blazemap) - inner: BlazeMap, +pub struct IntoIter { + pub(in crate::collections::blazemap) inner: BlazeMap, } /// An owning iterator over the keys of a [`BlazeMap`]. @@ -106,10 +88,8 @@ pub struct IntoIter /// See its documentation for more. /// /// [`into_keys`]: BlazeMap::into_keys -pub struct IntoKeys -{ - pub(in crate::collections::blazemap) - inner: IntoIter, +pub struct IntoKeys { + pub(in crate::collections::blazemap) inner: IntoIter, } /// An owning iterator over the values of a [`BlazeMap`]. @@ -118,10 +98,8 @@ pub struct IntoKeys /// See its documentation for more. /// /// [`into_values`]: BlazeMap::into_values -pub struct IntoValues -{ - pub(in crate::collections::blazemap) - inner: IntoIter, +pub struct IntoValues { + pub(in crate::collections::blazemap) inner: IntoIter, } /// A draining iterator over the entries of a [`BlazeMap`]. @@ -130,15 +108,13 @@ pub struct IntoValues /// documentation for more. /// /// [`drain`]: BlazeMap::drain -pub struct Drain<'a, K, V> -{ - pub(in crate::collections::blazemap) - inner: IterMut<'a, K, V>, +pub struct Drain<'a, K, V> { + pub(in crate::collections::blazemap) inner: IterMut<'a, K, V>, } impl<'a, K, V> Iterator for Iter<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { type Item = (K, &'a V); @@ -173,8 +149,8 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> } impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { #[inline] fn len(&self) -> usize { @@ -183,8 +159,8 @@ impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> } impl<'a, K, V> Iterator for IterMut<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { type Item = (K, &'a mut V); @@ -213,8 +189,8 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> } impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { #[inline] fn len(&self) -> usize { @@ -223,8 +199,8 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> } impl<'a, K, V> Iterator for Keys<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { type Item = K; @@ -259,8 +235,8 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> } impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { #[inline] fn len(&self) -> usize { @@ -268,8 +244,7 @@ impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> } } -impl<'a, K, V> Iterator for Values<'a, K, V> -{ +impl<'a, K, V> Iterator for Values<'a, K, V> { type Item = &'a V; #[inline] @@ -299,16 +274,14 @@ impl<'a, K, V> Iterator for Values<'a, K, V> } } -impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> -{ +impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> { #[inline] fn len(&self) -> usize { self.inner.len } } -impl<'a, K, V> Iterator for ValuesMut<'a, K, V> -{ +impl<'a, K, V> Iterator for ValuesMut<'a, K, V> { type Item = &'a mut V; #[inline] @@ -333,8 +306,7 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> } } -impl<'a, K, V> ExactSizeIterator for ValuesMut<'a, K, V> -{ +impl<'a, K, V> ExactSizeIterator for ValuesMut<'a, K, V> { #[inline] fn len(&self) -> usize { self.inner.len @@ -342,18 +314,14 @@ impl<'a, K, V> ExactSizeIterator for ValuesMut<'a, K, V> } impl Iterator for IntoIter - where - K: BlazeMapId +where + K: BlazeMapId, { type Item = (K, V); #[inline] fn next(&mut self) -> Option<(K, V)> { - let BlazeMap { - inner, - len, - .. - } = &mut self.inner; + let BlazeMap { inner, len, .. } = &mut self.inner; while let Some(back) = inner.pop() { if let Some(value) = back { let key = unsafe { K::from_index_unchecked(inner.len()) }; @@ -366,8 +334,8 @@ impl Iterator for IntoIter } impl ExactSizeIterator for IntoIter - where - K: BlazeMapId +where + K: BlazeMapId, { #[inline] fn len(&self) -> usize { @@ -376,18 +344,14 @@ impl ExactSizeIterator for IntoIter } impl Iterator for IntoKeys - where - K: BlazeMapId +where + K: BlazeMapId, { type Item = K; #[inline] fn next(&mut self) -> Option { - let BlazeMap { - inner, - len, - .. - } = &mut self.inner.inner; + let BlazeMap { inner, len, .. } = &mut self.inner.inner; while let Some(back) = inner.pop() { if back.is_some() { let key = unsafe { K::from_index_unchecked(inner.len()) }; @@ -400,8 +364,8 @@ impl Iterator for IntoKeys } impl ExactSizeIterator for IntoKeys - where - K: BlazeMapId +where + K: BlazeMapId, { #[inline] fn len(&self) -> usize { @@ -409,17 +373,12 @@ impl ExactSizeIterator for IntoKeys } } -impl Iterator for IntoValues -{ +impl Iterator for IntoValues { type Item = V; #[inline] fn next(&mut self) -> Option { - let BlazeMap { - inner, - len, - .. - } = &mut self.inner.inner; + let BlazeMap { inner, len, .. } = &mut self.inner.inner; while let Some(back) = inner.pop() { if let Some(value) = back { *len -= 1; @@ -431,8 +390,8 @@ impl Iterator for IntoValues } impl ExactSizeIterator for IntoValues - where - K: BlazeMapId +where + K: BlazeMapId, { #[inline] fn len(&self) -> usize { @@ -441,8 +400,8 @@ impl ExactSizeIterator for IntoValues } impl<'a, K, V> Iterator for Drain<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { type Item = (K, V); @@ -473,8 +432,8 @@ impl<'a, K, V> Iterator for Drain<'a, K, V> } impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> - where - K: BlazeMapId +where + K: BlazeMapId, { #[inline] fn len(&self) -> usize { @@ -482,8 +441,7 @@ impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> } } -impl<'a, K, V> Drop for Drain<'a, K, V> -{ +impl<'a, K, V> Drop for Drain<'a, K, V> { #[inline] fn drop(&mut self) { if !needs_drop::() { @@ -501,43 +459,45 @@ impl<'a, K, V> Drop for Drain<'a, K, V> } unsafe impl<'a, K, V> Send for Iter<'a, K, V> - where - K: Sync, - V: Sync -{} +where + K: Sync, + V: Sync, +{ +} unsafe impl<'a, K, V> Sync for Iter<'a, K, V> - where - K: Sync, - V: Sync -{} +where + K: Sync, + V: Sync, +{ +} -impl<'a, K, V> Unpin for Iter<'a, K, V> -{} +impl<'a, K, V> Unpin for Iter<'a, K, V> {} impl<'a, K, V> UnwindSafe for Iter<'a, K, V> - where - K: RefUnwindSafe, - V: RefUnwindSafe, -{} +where + K: RefUnwindSafe, + V: RefUnwindSafe, +{ +} unsafe impl<'a, K, V> Send for IterMut<'a, K, V> - where - K: Sync, - V: Send -{} +where + K: Sync, + V: Send, +{ +} unsafe impl<'a, K, V> Sync for IterMut<'a, K, V> - where - K: Sync, - V: Sync -{} +where + K: Sync, + V: Sync, +{ +} -impl<'a, K, V> Unpin for IterMut<'a, K, V> -{} +impl<'a, K, V> Unpin for IterMut<'a, K, V> {} -impl<'a, K, V> Clone for Iter<'a, K, V> -{ +impl<'a, K, V> Clone for Iter<'a, K, V> { #[inline] fn clone(&self) -> Self { *self @@ -547,10 +507,10 @@ impl<'a, K, V> Clone for Iter<'a, K, V> impl<'a, K, V> Copy for Iter<'a, K, V> {} impl<'a, K, V> Debug for Iter<'a, K, V> - where - K: BlazeMapId, - K::OrigType: Debug, - V: Debug +where + K: BlazeMapId, + K::OrigType: Debug, + V: Debug, { #[inline] fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { @@ -566,10 +526,10 @@ impl<'a, K, V> Debug for Iter<'a, K, V> } impl<'a, K, V> Debug for IterMut<'a, K, V> - where - K: BlazeMapId, - K::OrigType: Debug, - V: Debug +where + K: BlazeMapId, + K::OrigType: Debug, + V: Debug, { #[inline] fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { @@ -589,8 +549,7 @@ impl<'a, K, V> Debug for IterMut<'a, K, V> } } -impl<'a, K, V> Clone for Keys<'a, K, V> -{ +impl<'a, K, V> Clone for Keys<'a, K, V> { #[inline] fn clone(&self) -> Self { *self @@ -599,8 +558,7 @@ impl<'a, K, V> Clone for Keys<'a, K, V> impl<'a, K, V> Copy for Keys<'a, K, V> {} -impl<'a, K, V> Clone for Values<'a, K, V> -{ +impl<'a, K, V> Clone for Values<'a, K, V> { #[inline] fn clone(&self) -> Self { *self @@ -610,9 +568,9 @@ impl<'a, K, V> Clone for Values<'a, K, V> impl<'a, K, V> Copy for Values<'a, K, V> {} impl<'a, K, V> Debug for Keys<'a, K, V> - where - K: BlazeMapId, - K::OrigType: Debug +where + K: BlazeMapId, + K::OrigType: Debug, { #[inline] fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { @@ -628,8 +586,8 @@ impl<'a, K, V> Debug for Keys<'a, K, V> } impl<'a, K, V> Debug for Values<'a, K, V> - where - V: Debug +where + V: Debug, { #[inline] fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { @@ -638,8 +596,8 @@ impl<'a, K, V> Debug for Values<'a, K, V> } impl<'a, K, V> Debug for ValuesMut<'a, K, V> - where - V: Debug +where + V: Debug, { #[inline] fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { @@ -662,10 +620,10 @@ impl<'a, K, V> Debug for ValuesMut<'a, K, V> } impl Debug for IntoIter - where - K: BlazeMapId, - K::OrigType: Debug, - V: Debug +where + K: BlazeMapId, + K::OrigType: Debug, + V: Debug, { #[inline] fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { @@ -674,9 +632,9 @@ impl Debug for IntoIter } impl Debug for IntoKeys - where - K: BlazeMapId, - K::OrigType: Debug +where + K: BlazeMapId, + K::OrigType: Debug, { #[inline] fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { @@ -685,8 +643,8 @@ impl Debug for IntoKeys } impl Debug for IntoValues - where - V: Debug +where + V: Debug, { #[inline] fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { @@ -695,13 +653,13 @@ impl Debug for IntoValues } impl<'a, K, V> Debug for Drain<'a, K, V> - where - K: BlazeMapId, - K::OrigType: Debug, - V: Debug +where + K: BlazeMapId, + K::OrigType: Debug, + V: Debug, { #[inline] fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { self.inner.fmt(f) } -} \ No newline at end of file +} diff --git a/src/id_wrapper.rs b/src/id_wrapper.rs index d82636d..58f1052 100644 --- a/src/id_wrapper.rs +++ b/src/id_wrapper.rs @@ -10,8 +10,7 @@ use crate::orig_type_id_map::StaticInfoApi; /// Provides interface for `blazemap` id types /// defined by the [`register_blazemap_id_wrapper`](crate::register_blazemap_id_wrapper) /// and [`register_blazemap_id`](crate::register_blazemap_id) macros. -pub trait BlazeMapId: Copy -{ +pub trait BlazeMapId: Copy { /// Original key type. type OrigType: 'static + Clone + Eq + Hash; @@ -19,7 +18,7 @@ pub trait BlazeMapId: Copy type StaticInfoApi: 'static + StaticInfoApi; #[doc(hidden)] - type StaticInfoApiLock: ReadApi; + type StaticInfoApiLock: ReadApi; /// Creates an iterator over all identifiers registered at the time this method was called. #[inline] @@ -43,21 +42,18 @@ pub trait BlazeMapId: Copy /// Provides interface for constructable `blazemap` key-wrapper types /// defined by the [`register_blazemap_id_wrapper`](crate::register_blazemap_id_wrapper) macro. -pub trait BlazeMapIdWrapper: BlazeMapId -{ +pub trait BlazeMapIdWrapper: BlazeMapId { /// Creates a new instance of [`Self`] based on the [`Self::OrigType`](BlazeMapId::OrigType) instance. fn new(key: Self::OrigType) -> Self; } /// Iterator over consecutive `blazemap` identifiers. -pub struct AllInstancesIter -{ +pub struct AllInstancesIter { range: Range, phantom: PhantomData, } -impl Clone for AllInstancesIter -{ +impl Clone for AllInstancesIter { #[inline] fn clone(&self) -> Self { Self { @@ -67,16 +63,14 @@ impl Clone for AllInstancesIter } } -impl Debug for AllInstancesIter -{ +impl Debug for AllInstancesIter { #[inline] fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { write!(f, "{:?}", self.range) } } -impl PartialEq for AllInstancesIter -{ +impl PartialEq for AllInstancesIter { #[inline] fn eq(&self, other: &Self) -> bool { self.range == other.range @@ -85,8 +79,7 @@ impl PartialEq for AllInstancesIter impl Eq for AllInstancesIter {} -impl Hash for AllInstancesIter -{ +impl Hash for AllInstancesIter { #[inline] fn hash(&self, state: &mut H) { self.range.hash(state) @@ -94,8 +87,8 @@ impl Hash for AllInstancesIter } impl Iterator for AllInstancesIter - where - T: BlazeMapId +where + T: BlazeMapId, { type Item = T; @@ -107,8 +100,8 @@ impl Iterator for AllInstancesIter } impl DoubleEndedIterator for AllInstancesIter - where - T: BlazeMapId +where + T: BlazeMapId, { #[inline] fn next_back(&mut self) -> Option { @@ -118,11 +111,11 @@ impl DoubleEndedIterator for AllInstancesIter } impl ExactSizeIterator for AllInstancesIter - where - T: BlazeMapId +where + T: BlazeMapId, { #[inline] fn len(&self) -> usize { self.range.len() } -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index 1b00bf2..357bd42 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,22 +3,21 @@ //! and also implements a [vector-based slab-like map](prelude::BlazeMap) //! with an interface similar to that of [`HashMap`](std::collections::HashMap). -mod id_wrapper; -#[doc(hidden)] -pub mod utils; /// Collection types. pub mod collections; +mod id_wrapper; mod macros; #[doc(hidden)] pub mod orig_type_id_map; +#[doc(hidden)] +pub mod utils; /// Crate prelude. pub mod prelude { pub use crate::{ collections::blazemap::BlazeMap, id_wrapper::{AllInstancesIter, BlazeMapId, BlazeMapIdWrapper}, - register_blazemap_id, - register_blazemap_id_wrapper, + register_blazemap_id, register_blazemap_id_wrapper, }; } @@ -31,13 +30,11 @@ pub mod external { } #[cfg(test)] -mod tests -{ +mod tests { use crate::{register_blazemap_id, register_blazemap_id_wrapper}; #[cfg(feature = "serde")] - mod serde_compatible - { + mod serde_compatible { use crate::{register_blazemap_id, register_blazemap_id_wrapper}; register_blazemap_id_wrapper! { @@ -77,4 +74,4 @@ mod tests Ord } } -} \ No newline at end of file +} diff --git a/src/macros.rs b/src/macros.rs index c0effbe..5fc6730 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -311,8 +311,7 @@ macro_rules! blazemap_id_inner { #[macro_export] macro_rules! blazemap_derive_key_inner { (@DERIVE Default $new_type:ident) => { - impl Default for $new_type - { + impl Default for $new_type { #[inline] fn default() -> Self { Self::new(Default::default()) @@ -320,11 +319,9 @@ macro_rules! blazemap_derive_key_inner { } }; (@DERIVE PartialOrd $new_type:ident) => { - impl PartialOrd for $new_type - { + impl PartialOrd for $new_type { #[inline] - fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> - { + fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> { use $crate::orig_type_id_map::StaticInfoApi; let Self(lhs) = self; @@ -341,19 +338,16 @@ macro_rules! blazemap_derive_key_inner { } }; (@DERIVE Ord $new_type:ident) => { - impl PartialOrd for $new_type - { + impl PartialOrd for $new_type { #[inline] fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> { Some(self.cmp(other)) } } - impl Ord for $new_type - { + impl Ord for $new_type { #[inline] - fn cmp(&self, other: &Self) -> ::std::cmp::Ordering - { + fn cmp(&self, other: &Self) -> ::std::cmp::Ordering { use $crate::orig_type_id_map::StaticInfoApi; let Self(lhs) = self; @@ -370,11 +364,9 @@ macro_rules! blazemap_derive_key_inner { } }; (@DERIVE Debug $new_type:ident) => { - impl ::std::fmt::Debug for $new_type - { + impl ::std::fmt::Debug for $new_type { #[inline] - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result - { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { use $crate::orig_type_id_map::StaticInfoApi; let Self(index) = self; @@ -383,18 +375,14 @@ macro_rules! blazemap_derive_key_inner { let original_key = unsafe { guard.get_key_unchecked(index.into_inner()) }; f.field("original_key", original_key); drop(guard); - f - .field("index", &index.into_inner()) - .finish() + f.field("index", &index.into_inner()).finish() } } }; (@DERIVE Display $new_type:ident) => { - impl ::std::fmt::Display for $new_type - { + impl ::std::fmt::Display for $new_type { #[inline] - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result - { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { use $crate::orig_type_id_map::StaticInfoApi; let Self(index) = self; @@ -405,26 +393,26 @@ macro_rules! blazemap_derive_key_inner { } }; (@DERIVE Deserialize $new_type:ident) => { - impl<'de> $crate::external::serde::Deserialize<'de> for $new_type - { + impl<'de> $crate::external::serde::Deserialize<'de> for $new_type { #[inline] fn deserialize(deserializer: D) -> Result - where - D: $crate::external::serde::Deserializer<'de> + where + D: $crate::external::serde::Deserializer<'de>, { - let original_key: ::OrigType - = $crate::external::serde::Deserialize::deserialize(deserializer)?; - Ok(::new(original_key)) + let original_key: ::OrigType = + $crate::external::serde::Deserialize::deserialize(deserializer)?; + Ok(::new( + original_key, + )) } } }; (@DERIVE Serialize $new_type:ident) => { - impl $crate::external::serde::Serialize for $new_type - { + impl $crate::external::serde::Serialize for $new_type { #[inline] fn serialize(&self, serializer: S) -> Result - where - S: $crate::external::serde::Serializer + where + S: $crate::external::serde::Serializer, { use $crate::orig_type_id_map::StaticInfoApi; @@ -437,15 +425,14 @@ macro_rules! blazemap_derive_key_inner { } } } - } + }; } #[doc(hidden)] #[macro_export] macro_rules! blazemap_derive_assigned_sn { (@DERIVE PartialOrd $new_type:ident) => { - impl PartialOrd for $new_type - { + impl PartialOrd for $new_type { #[inline] fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> { let Self(lhs) = self; @@ -455,16 +442,14 @@ macro_rules! blazemap_derive_assigned_sn { } }; (@DERIVE Ord $new_type:ident) => { - impl PartialOrd for $new_type - { + impl PartialOrd for $new_type { #[inline] fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> { Some(self.cmp(other)) } } - impl Ord for $new_type - { + impl Ord for $new_type { #[inline] fn cmp(&self, other: &Self) -> ::std::cmp::Ordering { let Self(lhs) = self; @@ -472,15 +457,14 @@ macro_rules! blazemap_derive_assigned_sn { lhs.into_inner().cmp(&rhs.into_inner()) } } - } + }; } #[doc(hidden)] #[macro_export] macro_rules! blazemap_id_inner_derive { (@DERIVE PartialOrd $new_type:ident) => { - impl PartialOrd for $new_type - { + impl PartialOrd for $new_type { #[inline] fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> { let Self(lhs) = self; @@ -490,16 +474,14 @@ macro_rules! blazemap_id_inner_derive { } }; (@DERIVE Ord $new_type:ident) => { - impl PartialOrd for $new_type - { + impl PartialOrd for $new_type { #[inline] fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> { Some(self.cmp(other)) } } - impl Ord for $new_type - { + impl Ord for $new_type { #[inline] fn cmp(&self, other: &Self) -> ::std::cmp::Ordering { let Self(lhs) = self; @@ -509,16 +491,15 @@ macro_rules! blazemap_id_inner_derive { } }; (@DERIVE Serialize $new_type:ident) => { - impl $crate::external::serde::Serialize for $new_type - { + impl $crate::external::serde::Serialize for $new_type { #[inline] fn serialize(&self, serializer: S) -> Result - where - S: $crate::external::serde::Serializer + where + S: $crate::external::serde::Serializer, { let Self(index) = self; index.into_inner().serialize(serializer) } } - } -} \ No newline at end of file + }; +} diff --git a/src/orig_type_id_map.rs b/src/orig_type_id_map.rs index e82134b..0b4cb23 100644 --- a/src/orig_type_id_map.rs +++ b/src/orig_type_id_map.rs @@ -1,12 +1,12 @@ use std::borrow::Borrow; #[doc(hidden)] -pub trait StaticInfoApi -{ +pub trait StaticInfoApi { type KeyUnchecked<'a>: Borrow - where Self: 'a; + where + Self: 'a; fn num_elems(&self) -> usize; unsafe fn get_key_unchecked(&self, index: usize) -> Self::KeyUnchecked<'_>; -} \ No newline at end of file +} diff --git a/src/utils.rs b/src/utils.rs index 4cc3009..64f666c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,9 @@ -pub use {private_index::PrivateIndex, static_info::{IdWrapperStaticInfo, TrivialIdStaticInfo}}; +pub use { + private_index::PrivateIndex, + static_info::{IdWrapperStaticInfo, TrivialIdStaticInfo}, +}; #[doc(hidden)] mod private_index; #[doc(hidden)] -mod static_info; \ No newline at end of file +mod static_info; diff --git a/src/utils/private_index.rs b/src/utils/private_index.rs index 6f2a2c0..8e82264 100644 --- a/src/utils/private_index.rs +++ b/src/utils/private_index.rs @@ -13,8 +13,7 @@ use std::hash::Hash; #[doc(hidden)] pub struct PrivateIndex(usize); -impl PrivateIndex -{ +impl PrivateIndex { #[inline(always)] pub unsafe fn new(index: usize) -> Self { Self(index) @@ -27,8 +26,7 @@ impl PrivateIndex } #[cfg(test)] -mod tests -{ +mod tests { use std::fmt::{Debug, Display}; #[cfg(feature = "serde")] @@ -44,4 +42,4 @@ mod tests #[cfg(feature = "serde")] assert_not_impl_any!(PrivateIndex: Serialize, Deserialize<'static>); -} \ No newline at end of file +} diff --git a/src/utils/static_info.rs b/src/utils/static_info.rs index 996037b..e0b5118 100644 --- a/src/utils/static_info.rs +++ b/src/utils/static_info.rs @@ -21,8 +21,7 @@ pub struct TrivialIdStaticInfo { next_id: AtomicUsize, } -impl IdWrapperStaticInfo -{ +impl IdWrapperStaticInfo { /// Creates a new instance of [`IdWrapperStaticInfo`]. #[inline] pub const fn new() -> Self { @@ -34,8 +33,8 @@ impl IdWrapperStaticInfo } impl IdWrapperStaticInfo - where - K: Clone + Eq + Hash +where + K: Clone + Eq + Hash, { #[inline] pub fn get_index(&self, key: &K) -> Option { @@ -43,13 +42,11 @@ impl IdWrapperStaticInfo } #[inline] - pub - unsafe fn insert_new_key_unchecked(&mut self, key: K) -> usize - { + pub unsafe fn insert_new_key_unchecked(&mut self, key: K) -> usize { let next_id = self.num_elems(); let Self { index_to_orig, - orig_to_index + orig_to_index, } = self; index_to_orig.push(key.clone()); orig_to_index.insert(key, next_id); @@ -57,8 +54,7 @@ impl IdWrapperStaticInfo } } -impl TrivialIdStaticInfo -{ +impl TrivialIdStaticInfo { /// Creates a new instance of [`TrivialIdStaticInfo`]. #[inline] pub const fn new(first_id: usize) -> Self { @@ -70,18 +66,17 @@ impl TrivialIdStaticInfo /// Returns the next identifier. #[inline] pub fn next_id(&self) -> usize { - self.next_id.fetch_update( - Ordering::Release, - Ordering::Acquire, - |next_id| next_id.checked_add(1), - ) + self.next_id + .fetch_update(Ordering::Release, Ordering::Acquire, |next_id| { + next_id.checked_add(1) + }) .expect("usize overflow") } } impl StaticInfoApi for IdWrapperStaticInfo - where - K: Clone + Eq + Hash +where + K: Clone + Eq + Hash, { type KeyUnchecked<'a> = &'a K where Self: 'a; @@ -92,14 +87,12 @@ impl StaticInfoApi for IdWrapperStaticInfo } #[inline] - unsafe - fn get_key_unchecked(&self, index: usize) -> &K { + unsafe fn get_key_unchecked(&self, index: usize) -> &K { self.index_to_orig.get_unchecked(index) } } -impl StaticInfoApi for TrivialIdStaticInfo -{ +impl StaticInfoApi for TrivialIdStaticInfo { type KeyUnchecked<'a> = usize where Self: 'a; @@ -112,4 +105,4 @@ impl StaticInfoApi for TrivialIdStaticInfo unsafe fn get_key_unchecked(&self, index: usize) -> usize { index } -} \ No newline at end of file +}