refactor: store either map or attrs in perf calc
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
use crate::Beatmap;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) enum MapOrElse<M, E> {
|
||||
Map(M),
|
||||
Else(E),
|
||||
}
|
||||
|
||||
impl<M, E> MapOrElse<M, E>
|
||||
where
|
||||
M: AsRef<Beatmap>,
|
||||
{
|
||||
// /// Take out the `Else` value of `self` and return it.
|
||||
// ///
|
||||
// /// If `self` is of variant `Else`, `E::default()` will be left in place.
|
||||
// pub(crate) fn take(&mut self) -> MapOrElse<MapRef<'_>, E>
|
||||
// where
|
||||
// E: Default,
|
||||
// {
|
||||
// match self {
|
||||
// Self::Map(ref map) => MapOrElse::from(map.deref()),
|
||||
// Self::Else(ref mut other) => MapOrElse::Else(mem::take(other)),
|
||||
// }
|
||||
// }
|
||||
|
||||
/// Return a mutable reference to `Else`.
|
||||
///
|
||||
/// If `self` is of variant `Map`, store `other` in `self`, and return a
|
||||
/// mutable reference to it.
|
||||
pub(crate) fn else_or_insert(&mut self, other: E) -> &mut E {
|
||||
match self {
|
||||
MapOrElse::Map(_) => {
|
||||
*self = Self::Else(other);
|
||||
|
||||
let Self::Else(ref mut other) = self else {
|
||||
unreachable!()
|
||||
};
|
||||
|
||||
other
|
||||
}
|
||||
MapOrElse::Else(ref mut other) => other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'map, E> From<&'map Beatmap> for MapOrElse<MapRef<'map>, E> {
|
||||
fn from(map: &'map Beatmap) -> Self {
|
||||
Self::Map(MapRef(map))
|
||||
}
|
||||
}
|
||||
|
||||
/// References don't implement [`Deref`] so we implement a wrapper type.
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub(crate) struct MapRef<'map>(&'map Beatmap);
|
||||
|
||||
impl<'map> MapRef<'map> {
|
||||
pub(crate) fn into_inner(self) -> &'map Beatmap {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<Beatmap> for MapRef<'_> {
|
||||
fn as_ref(&self) -> &Beatmap {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
+7
-2
@@ -2,9 +2,14 @@ mod byte_hasher;
|
||||
mod compact_vec;
|
||||
mod float_ext;
|
||||
mod limited_queue;
|
||||
mod map_or_else;
|
||||
mod tandem_sort;
|
||||
|
||||
pub(crate) use self::{
|
||||
byte_hasher::ByteHasher, compact_vec::CompactVec, float_ext::FloatExt,
|
||||
limited_queue::LimitedQueue, tandem_sort::TandemSorter,
|
||||
byte_hasher::ByteHasher,
|
||||
compact_vec::CompactVec,
|
||||
float_ext::FloatExt,
|
||||
limited_queue::LimitedQueue,
|
||||
map_or_else::{MapOrElse, MapRef},
|
||||
tandem_sort::TandemSorter,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user