Skip to content

Commit

Permalink
fmt: add rustfmt.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsonin committed Apr 23, 2024
1 parent 85f3cfb commit ab6bc2a
Show file tree
Hide file tree
Showing 18 changed files with 222 additions and 162 deletions.
5 changes: 5 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
edition = "2021"
merge_derives = false
imports_granularity = "Crate"
normalize_comments = true
wrap_comments = true
52 changes: 32 additions & 20 deletions src/collections/blazemap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::borrow::Borrow;
use std::fmt::{Debug, Formatter};
use std::marker::PhantomData;
use std::{
borrow::Borrow,
fmt::{Debug, Formatter},
marker::PhantomData,
};

#[cfg(feature = "serde")]
use {
Expand All @@ -12,14 +14,16 @@ use {
},
};

use crate::collections::blazemap::entries::VacantEntryInner;
pub use crate::collections::blazemap::{
entries::{Entry, OccupiedEntry, VacantEntry},
iters::{Drain, IntoIter, IntoKeys, IntoValues, Iter, IterMut, Keys, Values, ValuesMut},
};
use crate::traits::CapacityInfoProvider;
use crate::traits::KeyByOffsetProvider;
use crate::traits::{BlazeMapId, BlazeMapIdStatic, TypeInfoContainer};
use crate::{
collections::blazemap::entries::VacantEntryInner,
traits::{
BlazeMapId, BlazeMapIdStatic, CapacityInfoProvider, KeyByOffsetProvider, TypeInfoContainer,
},
};

mod entries;
mod iters;
Expand Down Expand Up @@ -58,16 +62,18 @@ impl<K, V> BlazeMap<K, V> {
self.len == 0
}

/// Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
/// Clears the map, removing all key-value pairs. Keeps the allocated memory
/// for reuse.
#[inline]
pub fn clear(&mut self) {
self.inner.clear();
self.len = 0;
}

/// Shrinks the capacity of the map as much as possible.
/// It will drop down as much as possible while maintaining the internal rules
/// and possibly leaving some space in accordance with the resize policy.
/// It will drop down as much as possible while maintaining the internal
/// rules and possibly leaving some space in accordance with the resize
/// policy.
#[inline]
pub fn shrink_to_fit(&mut self) {
if !self.is_empty() {
Expand All @@ -91,7 +97,8 @@ impl<K, V> BlazeMap<K, V> {
///
/// If the returned iterator is dropped before being fully consumed,
/// it drops the remaining key-value pairs.
/// The returned iterator keeps a mutable borrow on the map to optimize its implementation.
/// The returned iterator keeps a mutable borrow on the map to optimize its
/// implementation.
#[inline]
pub fn drain(&mut self) -> Drain<'_, K, V> {
debug_assert_eq!(
Expand All @@ -109,7 +116,8 @@ impl<K, V> BlazeMap<K, V>
where
K: BlazeMapId,
{
/// An iterator visiting all key-value pairs. The iterator element type is `(K, &V)`.
/// An iterator visiting all key-value pairs. The iterator element type is
/// `(K, &V)`.
#[inline]
#[must_use]
pub fn iter(&self) -> Iter<'_, K, V> {
Expand All @@ -125,8 +133,8 @@ where
}
}

/// An iterator visiting all key-value pairs, with mutable references to the values.
/// The iterator element type is `(K, &mut V)`.
/// An iterator visiting all key-value pairs, with mutable references to the
/// values. The iterator element type is `(K, &mut V)`.
#[inline]
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
debug_assert_eq!(
Expand Down Expand Up @@ -163,7 +171,8 @@ where
Values { inner: self.iter() }
}

/// An iterator visiting all values mutably. The iterator element type is `&mut V`.
/// An iterator visiting all values mutably. The iterator element type is
/// `&mut V`.
#[inline]
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
debug_assert_eq!(
Expand Down Expand Up @@ -239,8 +248,8 @@ where
///
/// If the map did not have this key present, None is returned.
///
/// If the map did have this key present, the value is updated, and the old value is returned.
/// The key is not updated, though.
/// If the map did have this key present, the value is updated, and the old
/// value is returned. The key is not updated, though.
#[inline]
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
debug_assert_eq!(
Expand Down Expand Up @@ -277,7 +286,8 @@ where
result
}

/// Gets the given key’s corresponding entry in the map for in-place manipulation.
/// Gets the given key’s corresponding entry in the map for in-place
/// manipulation.
#[inline]
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
debug_assert_eq!(
Expand Down Expand Up @@ -313,7 +323,8 @@ where
}

/// Creates a consuming iterator visiting all the keys.
/// The map cannot be used after calling this. The iterator element type is `K`.
/// The map cannot be used after calling this. The iterator element type is
/// `K`.
#[inline]
#[must_use]
pub fn into_keys(self) -> IntoKeys<K, V> {
Expand All @@ -327,7 +338,8 @@ where
}

/// Creates a consuming iterator visiting all the values.
/// The map cannot be used after calling this. The iterator element type is `V`.
/// The map cannot be used after calling this. The iterator element type is
/// `V`.
#[inline]
#[must_use]
pub fn into_values(self) -> IntoValues<K, V> {
Expand Down
27 changes: 17 additions & 10 deletions src/collections/blazemap/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::prelude::BlazeMapId;
/// A view into a single entry in a map, which may either be vacant or occupied.
///
/// This enum is constructed
/// from the [`entry`] method on [`BlazeMap`](crate::collections::blazemap::BlazeMap).
/// from the [`entry`] method on
/// [`BlazeMap`](crate::collections::blazemap::BlazeMap).
///
/// [`entry`]: crate::collections::blazemap::BlazeMap::entry
pub enum Entry<'a, K, V>
Expand All @@ -18,8 +19,9 @@ where
}

#[derive(Debug)]
/// A view into an occupied entry in a [`BlazeMap`](crate::collections::blazemap::BlazeMap).
/// It is part of the [`Entry`] enum.
/// 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,
Expand All @@ -32,8 +34,9 @@ where
}

#[derive(Debug)]
/// A view into a vacant entry in a [`BlazeMap`](crate::collections::blazemap::BlazeMap).
/// It is part of the [`Entry`] enum.
/// 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,
Expand Down Expand Up @@ -65,8 +68,9 @@ where
}
}

/// Ensures a value is in the entry by inserting the result of the default function if empty,
/// and returns a mutable reference to the value in the entry.
/// Ensures a value is in the entry by inserting the result of the default
/// function if empty, and returns a mutable reference to the value in
/// the entry.
#[inline]
pub fn or_insert_with(self, default: impl FnOnce() -> V) -> &'a mut V {
match self {
Expand Down Expand Up @@ -143,7 +147,8 @@ where
/// Gets a mutable reference to the value in the entry.
///
/// If you need a reference to the [`OccupiedEntry`]
/// which may outlive the destruction of the [`Entry`] value, see [`into_mut`].
/// which may outlive the destruction of the [`Entry`] value, see
/// [`into_mut`].
///
/// [`into_mut`]: Self::into_mut
#[inline]
Expand All @@ -154,7 +159,8 @@ where
/// Converts the [`OccupiedEntry`] into a mutable reference
/// to the value in the entry with a lifetime bound to the map itself.
///
/// If you need multiple references to the [`OccupiedEntry`], see [`get_mut`].
/// If you need multiple references to the [`OccupiedEntry`], see
/// [`get_mut`].
///
/// [`get_mut`]: Self::get_mut
#[inline]
Expand All @@ -181,7 +187,8 @@ impl<'a, K, V> VacantEntry<'a, K, V>
where
K: BlazeMapId,
{
/// Gets the key that would be used when inserting a value through the [`VacantEntry`].
/// Gets the key that would be used when inserting a value through the
/// [`VacantEntry`].
#[inline]
pub fn key(&self) -> K {
self.key
Expand Down
24 changes: 14 additions & 10 deletions src/collections/blazemap/iters.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::borrow::Borrow;
use std::fmt::{Debug, Formatter};
use std::marker::PhantomData;
use std::panic::{RefUnwindSafe, UnwindSafe};

use crate::collections::blazemap::BlazeMap;
use crate::prelude::{BlazeMapId, BlazeMapIdStatic};
use crate::traits::{KeyByOffsetProvider, TypeInfoContainer};
use std::{
borrow::Borrow,
fmt::{Debug, Formatter},
marker::PhantomData,
panic::{RefUnwindSafe, UnwindSafe},
};

use crate::{
collections::blazemap::BlazeMap,
prelude::{BlazeMapId, BlazeMapIdStatic},
traits::{KeyByOffsetProvider, TypeInfoContainer},
};

/// An iterator over the entries of a [`BlazeMap`].
///
Expand Down Expand Up @@ -61,8 +65,8 @@ pub struct Values<'a, K, V> {

/// A mutable iterator over the values of a [`BlazeMap`].
///
/// This `struct` is created by the [`values_mut`] method on [`BlazeMap`]. See its
/// documentation for more.
/// This `struct` is created by the [`values_mut`] method on [`BlazeMap`]. See
/// its documentation for more.
///
/// [`values_mut`]: BlazeMap::values_mut
pub struct ValuesMut<'a, K, V> {
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Implements a [vector-based slab-like map](prelude::BlazeMap)
//! with an interface similar to that of [`HashMap`](std::collections::HashMap),
//! and also provides tools
//! for generating lightweight identifiers that can be type-safely used as keys for this map.
//! for generating lightweight identifiers that can be type-safely used as keys
//! for this map.

/// Collection types.
pub mod collections;
Expand Down Expand Up @@ -34,5 +35,6 @@ pub mod external {

#[cfg(loom)]
pub use loom;
pub use {once_cell, parking_lot};
pub use once_cell;
pub use parking_lot;
}
8 changes: 4 additions & 4 deletions src/loom.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::cmp::Ordering;
use std::marker::PhantomData;
use std::{cmp::Ordering, marker::PhantomData};

use crate::prelude::AllInstancesIter;
#[cfg(feature = "serde")]
use serde::{Serialize, Serializer};

use crate::traits::{BlazeMapId, TypeInfoContainer};

/// Provides `PartialOrd`, `Ord` and `Serialize` traits, which are derived as for original type,
/// for [`BlazeMapId`]s in the [`loom`](crate::external::loom) context.
/// Provides `PartialOrd`, `Ord` and `Serialize` traits, which are derived as
/// for original type, for [`BlazeMapId`]s in the
/// [`loom`](crate::external::loom) context.
#[derive(Debug, Copy, Clone)]
pub struct TestableId<'a, I, C> {
id: I,
Expand Down
44 changes: 27 additions & 17 deletions src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::borrow::Borrow;
use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::ops::{Deref, Range};

/// Provides an interface for `blazemap` id types defined by type-generating macros.
use std::{
borrow::Borrow,
fmt::{Debug, Formatter},
hash::{Hash, Hasher},
marker::PhantomData,
ops::{Deref, Range},
};

/// Provides an interface for `blazemap` id types defined by type-generating
/// macros.
pub trait BlazeMapId: Copy {
/// Original key type.
type OrigType: 'static + Clone + Eq + Hash;
Expand All @@ -24,9 +27,11 @@ pub trait BlazeMapId: Copy {

/// Provides an interface for `blazemap` key-wrapper id types
/// defined by the [`define_key_wrapper`](crate::define_key_wrapper)
/// and [`define_key_wrapper_bounded`](crate::define_key_wrapper_bounded) macros.
/// and [`define_key_wrapper_bounded`](crate::define_key_wrapper_bounded)
/// macros.
pub trait BlazeMapIdWrapper: BlazeMapId {
/// Creates a new instance of [`Self`] based on the [`Self::OrigType`](BlazeMapId::OrigType) instance.
/// Creates a new instance of [`Self`] based on the
/// [`Self::OrigType`](BlazeMapId::OrigType) instance.
unsafe fn new(type_info_container: &Self::TypeInfoContainer, key: Self::OrigType) -> Self;
}

Expand All @@ -46,24 +51,27 @@ pub trait BlazeMapIdStatic: BlazeMapId {
}

/// Returns the static container
/// that holds all the necessary static information for the [`BlazeMapId`] type.
/// that holds all the necessary static information for the [`BlazeMapId`]
/// type.
#[doc(hidden)]
fn static_container() -> &'static Self::TypeInfoContainer;
}

/// Implements an interface for [`BlazeMapId`] key-wrapper static containers.
#[doc(hidden)]
pub trait WrapKey<I: BlazeMapId> {
/// Creates an instance of [`BlazeMapId`] type that is unique to the given key.
/// Creates an instance of [`BlazeMapId`] type that is unique to the given
/// key.
fn wrap_key(&self, key: I::OrigType) -> I;
}

pub trait TypeInfoContainer: 'static {
/// Original key type.
type OrigType;

/// Returns the provider of the current total number of registered unique `Self` identifiers.
/// Note that this provider isn't sequentially consistent.
/// Returns the provider of the current total number of registered unique
/// `Self` identifiers. Note that this provider isn't sequentially
/// consistent.
#[doc(hidden)]
fn capacity_info_provider(&self) -> impl Deref<Target = impl CapacityInfoProvider>;

Expand All @@ -75,15 +83,17 @@ pub trait TypeInfoContainer: 'static {
) -> impl Deref<Target = impl KeyByOffsetProvider<Self::OrigType>>;
}

/// Provides the current total number of registered unique [`BlazeMapId`] identifiers.
/// Note that there is no guarantee of sequential consistency.
/// Provides the current total number of registered unique [`BlazeMapId`]
/// identifiers. Note that there is no guarantee of sequential consistency.
#[doc(hidden)]
pub trait CapacityInfoProvider {
/// Returns the current total number of registered unique [`BlazeMapId`] identifiers.
/// Returns the current total number of registered unique [`BlazeMapId`]
/// identifiers.
fn offset_capacity(&self) -> usize;
}

/// May unsafely return the registered key corresponding to the offset specified.
/// May unsafely return the registered key corresponding to the offset
/// specified.
#[doc(hidden)]
pub trait KeyByOffsetProvider<K> {
/// Returns the registered key corresponding to the offset specified.
Expand Down
Loading

0 comments on commit ab6bc2a

Please sign in to comment.