refactor: remove unnecesary cfg_attr and inline macro

This commit is contained in:
shogo-nakano-desu 2023-09-20 08:34:14 +09:00
parent 4b3257a365
commit 00e238e99b

View File

@ -10,21 +10,15 @@
//! This crate has two independent features, each of which provides an implementation providing
//! types `Map`, `AnyMap`, `OccupiedEntry`, `VacantEntry`, `Entry` and `RawMap`:
//!
#![cfg_attr(feature = "std", doc = " - **std** (default, *enabled* in this build):")]
#![cfg_attr(not(feature = "std"), doc = " - **std** (default, *disabled* in this build):")]
//! - **std** (default, *enabled* in this build):
//! an implementation using `std::collections::hash_map`, placed in the crate root
//! (e.g. `anymap::AnyMap`).
//!
#![cfg_attr(feature = "hashbrown", doc = " - **hashbrown** (optional; *enabled* in this build):")]
#![cfg_attr(
not(feature = "hashbrown"),
doc = " - **hashbrown** (optional; *disabled* in this build):"
)]
//! - **hashbrown** (optional; *enabled* in this build):
//! an implementation using `alloc` and `hashbrown::hash_map`, placed in a module `hashbrown`
//! (e.g. `anymap::hashbrown::AnyMap`).
#![warn(missing_docs, unused_results)]
#![cfg_attr(not(feature = "std"), no_std)]
use core::convert::TryInto;
use core::hash::Hasher;
@ -63,17 +57,11 @@ impl Hasher for TypeIdHasher {
}
}
#[cfg(any(feature = "std", feature = "hashbrown"))]
macro_rules! everything {
($example_init:literal, $($parent:ident)::+ $(, $entry_generics:ty)?) => {
use core::any::{Any, TypeId};
use core::hash::BuildHasherDefault;
use core::marker::PhantomData;
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
use ::$($parent)::+::hash_map::{self, HashMap};
use ::std::collections::hash_map::{self, HashMap};
use crate::any::{Downcast, IntoBox};
@ -109,7 +97,7 @@ macro_rules! everything {
/// <code>[anymap::Map][Map]::&lt;[core::any::Any]&gt;::new()</code> instead if desired.)
///
/// ```rust
#[doc = $example_init]
#[doc = "let mut data = anymap::AnyMap::new();"]
/// assert_eq!(data.get(), None::<&i32>);
/// ```
///
@ -136,45 +124,39 @@ macro_rules! everything {
/// Create an empty collection.
#[inline]
pub fn new() -> Map<A> {
Map {
raw: RawMap::with_hasher(Default::default()),
}
Map { raw: RawMap::with_hasher(Default::default()) }
}
/// Returns a reference to the value stored in the collection for the type `T`,
/// if it exists.
#[inline]
pub fn get<T: IntoBox<A>>(&self) -> Option<&T> {
self.raw.get(&TypeId::of::<T>())
.map(|any| unsafe { any.downcast_ref_unchecked::<T>() })
self.raw.get(&TypeId::of::<T>()).map(|any| unsafe { any.downcast_ref_unchecked::<T>() })
}
/// Gets the entry for the given type in the collection for in-place manipulation
#[inline]
pub fn entry<T: IntoBox<A>>(&mut self) -> Entry<A, T> {
match self.raw.entry(TypeId::of::<T>()) {
hash_map::Entry::Occupied(e) => Entry::Occupied(OccupiedEntry {
inner: e,
type_: PhantomData,
}),
hash_map::Entry::Vacant(e) => Entry::Vacant(VacantEntry {
inner: e,
type_: PhantomData,
}),
hash_map::Entry::Occupied(e) => {
Entry::Occupied(OccupiedEntry { inner: e, type_: PhantomData })
}
hash_map::Entry::Vacant(e) => {
Entry::Vacant(VacantEntry { inner: e, type_: PhantomData })
}
}
}
}
/// A view into a single occupied location in an `Map`.
pub struct OccupiedEntry<'a, A: ?Sized + Downcast, V: 'a> {
inner: hash_map::OccupiedEntry<'a, TypeId, Box<A>, $($entry_generics)?>,
inner: hash_map::OccupiedEntry<'a, TypeId, Box<A>>,
type_: PhantomData<V>,
}
/// A view into a single empty location in an `Map`.
pub struct VacantEntry<'a, A: ?Sized + Downcast, V: 'a> {
inner: hash_map::VacantEntry<'a, TypeId, Box<A>, $($entry_generics)?>,
inner: hash_map::VacantEntry<'a, TypeId, Box<A>>,
type_: PhantomData<V>,
}
@ -187,8 +169,6 @@ macro_rules! everything {
}
impl<'a, A: ?Sized + Downcast, V: IntoBox<A>> Entry<'a, A, V> {
/// 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]
@ -220,16 +200,23 @@ macro_rules! everything {
#[cfg(test)]
mod tests {
use crate::CloneAny;
use super::*;
use crate::CloneAny;
#[derive(Clone, Debug, PartialEq)] struct A(i32);
#[derive(Clone, Debug, PartialEq)] struct B(i32);
#[derive(Clone, Debug, PartialEq)] struct C(i32);
#[derive(Clone, Debug, PartialEq)] struct D(i32);
#[derive(Clone, Debug, PartialEq)] struct E(i32);
#[derive(Clone, Debug, PartialEq)] struct F(i32);
#[derive(Clone, Debug, PartialEq)] struct J(i32);
#[derive(Clone, Debug, PartialEq)]
struct A(i32);
#[derive(Clone, Debug, PartialEq)]
struct B(i32);
#[derive(Clone, Debug, PartialEq)]
struct C(i32);
#[derive(Clone, Debug, PartialEq)]
struct D(i32);
#[derive(Clone, Debug, PartialEq)]
struct E(i32);
#[derive(Clone, Debug, PartialEq)]
struct F(i32);
#[derive(Clone, Debug, PartialEq)]
struct J(i32);
#[test]
fn test_varieties() {
@ -249,9 +236,6 @@ macro_rules! everything {
assert_debug::<Map<dyn CloneAny + Send>>();
assert_debug::<Map<dyn CloneAny + Send + Sync>>();
}
}
};
}
#[test]
fn type_id_hasher() {
@ -270,6 +254,4 @@ fn type_id_hasher() {
verify_hashing_with(TypeId::of::<&str>());
verify_hashing_with(TypeId::of::<Vec<u8>>());
}
#[cfg(feature = "std")]
everything!("let mut data = anymap::AnyMap::new();", std::collections);
}