From 62d8fcc7f3a27acf9e6833aecd2a3e56cce25ee0 Mon Sep 17 00:00:00 2001 From: Dmitry Stepanov Date: Thu, 3 Sep 2026 19:23:05 +0300 Subject: [PATCH] `DynType::try_take_or_default` --- fyrox-core/src/dyntype.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/fyrox-core/src/dyntype.rs b/fyrox-core/src/dyntype.rs index 79ed830d8..2641e9925 100644 --- a/fyrox-core/src/dyntype.rs +++ b/fyrox-core/src/dyntype.rs @@ -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(&mut self) -> T { + self.try_take::().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)