add Converted::unchecked_convert method
This commit is contained in:
@@ -70,17 +70,11 @@ impl<M: IGameMode> Converted<'_, M> {
|
|||||||
/// If the conversion is incompatible the [`Beatmap`] will be returned
|
/// If the conversion is incompatible the [`Beatmap`] will be returned
|
||||||
/// unchanged as `Err`.
|
/// unchanged as `Err`.
|
||||||
#[allow(clippy::result_large_err)]
|
#[allow(clippy::result_large_err)]
|
||||||
pub fn try_from_owned(map: Beatmap) -> Result<Self, Beatmap> {
|
pub fn try_from_owned(mut map: Beatmap) -> Result<Self, Beatmap> {
|
||||||
let mut map = Cow::Owned(map);
|
|
||||||
|
|
||||||
match M::try_convert(&mut map) {
|
match M::try_convert(&mut map) {
|
||||||
ConvertStatus::Noop => Ok(Self::new(map, false)),
|
ConvertStatus::Noop => Ok(Self::new(Cow::Owned(map), false)),
|
||||||
ConvertStatus::Done => Ok(Self::new(map, true)),
|
ConvertStatus::Conversion => Ok(Self::new(Cow::Owned(map), true)),
|
||||||
ConvertStatus::Incompatible => {
|
ConvertStatus::Incompatible => Err(map),
|
||||||
let Cow::Owned(map) = map else { unreachable!() };
|
|
||||||
|
|
||||||
Err(map)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,11 +120,15 @@ impl<'a, M: IGameMode> Converted<'a, M> {
|
|||||||
///
|
///
|
||||||
/// [`&Beatmap`]: Beatmap
|
/// [`&Beatmap`]: Beatmap
|
||||||
pub fn try_from_ref(map: &'a Beatmap) -> Option<Self> {
|
pub fn try_from_ref(map: &'a Beatmap) -> Option<Self> {
|
||||||
let mut map = Cow::Borrowed(map);
|
let mut map = match M::check_convert(map) {
|
||||||
|
ConvertStatus::Noop => return Some(Self::new(Cow::Borrowed(map), false)),
|
||||||
|
ConvertStatus::Conversion => map.to_owned(),
|
||||||
|
ConvertStatus::Incompatible => return None,
|
||||||
|
};
|
||||||
|
|
||||||
match M::try_convert(&mut map) {
|
match M::try_convert(&mut map) {
|
||||||
ConvertStatus::Noop => Some(Self::new(map, false)),
|
ConvertStatus::Conversion => Some(Self::new(Cow::Owned(map), true)),
|
||||||
ConvertStatus::Done => Some(Self::new(map, true)),
|
ConvertStatus::Noop => Some(Self::new(Cow::Owned(map), false)),
|
||||||
ConvertStatus::Incompatible => None,
|
ConvertStatus::Incompatible => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -158,6 +156,18 @@ impl<'a, M: IGameMode> Converted<'a, M> {
|
|||||||
.map_err(|map| Self::new(Cow::Owned(map), self.is_convert)),
|
.map_err(|map| Self::new(Cow::Owned(map), self.is_convert)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convert a [`Converted`] from mode `M` to mode `N`.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if the conversion is incompatible.
|
||||||
|
pub fn unchecked_convert<N: IGameMode>(self) -> Converted<'a, N> {
|
||||||
|
match self.map {
|
||||||
|
Cow::Borrowed(map) => Converted::<N>::unchecked_from_ref(map),
|
||||||
|
Cow::Owned(map) => Converted::<N>::unchecked_from_owned(map),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<M> Clone for Converted<'_, M> {
|
impl<M> Clone for Converted<'_, M> {
|
||||||
|
|||||||
Reference in New Issue
Block a user