DynType::try_take_or_default

This commit is contained in:
Dmitry Stepanov
2026-09-03 19:23:05 +03:00
parent a8fb5b7e58
commit 62d8fcc7f3
+10 -1
View File
@@ -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)