mirror of
https://github.com/FyroxEngine/Fyrox.git
synced 2026-10-11 22:50:08 +00:00
DynType::try_take_or_default
This commit is contained in:
@@ -24,7 +24,7 @@
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use crate::reflect::TypeInfo;
|
||||
use crate::{reflect::prelude::*, visitor::prelude::*, SafeLock};
|
||||
use crate::{err, reflect::prelude::*, visitor::prelude::*, SafeLock};
|
||||
use fxhash::FxHashMap;
|
||||
use parking_lot::{Mutex, MutexGuard};
|
||||
use std::{
|
||||
@@ -188,6 +188,15 @@ impl DynTypeContainer {
|
||||
}
|
||||
}
|
||||
|
||||
/// Same as [`Self::try_take`], but fallbacks to the default value of the given type and prints
|
||||
/// error reason to the log.
|
||||
pub fn try_take_or_default<T: DynType + Default>(&mut self) -> T {
|
||||
self.try_take::<T>().unwrap_or_else(|err| {
|
||||
err!("Unable to take dyntype data! Reason: {err}",);
|
||||
T::default()
|
||||
})
|
||||
}
|
||||
|
||||
/// Tries to return a reference to the inner value. Returns [`None`] if the container is empty.
|
||||
pub fn value_ref(&self) -> Option<&dyn DynType> {
|
||||
self.0.as_ref().map(|v| &*v.0)
|
||||
|
||||
Reference in New Issue
Block a user