diff --git a/crates/bevy_plugin/src/line_provider/text_provider/shared_text_provider.rs b/crates/bevy_plugin/src/line_provider/text_provider/shared_text_provider.rs index a1e0c67c..cdd92c23 100644 --- a/crates/bevy_plugin/src/line_provider/text_provider/shared_text_provider.rs +++ b/crates/bevy_plugin/src/line_provider/text_provider/shared_text_provider.rs @@ -65,4 +65,12 @@ impl UnderlyingTextProvider for SharedTextProvider { fn are_lines_available(&self) -> bool { self.0.read().unwrap().are_lines_available() } + + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } } diff --git a/crates/bevy_plugin/src/line_provider/text_provider/strings_file_text_provider.rs b/crates/bevy_plugin/src/line_provider/text_provider/strings_file_text_provider.rs index 77847627..f4c8783d 100644 --- a/crates/bevy_plugin/src/line_provider/text_provider/strings_file_text_provider.rs +++ b/crates/bevy_plugin/src/line_provider/text_provider/strings_file_text_provider.rs @@ -105,6 +105,14 @@ impl UnderlyingTextProvider for StringsFileTextProvider { let has_fetched_translation = || self.translation_string_table.is_some(); is_base_language || has_fetched_translation() } + + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } } impl StringsFileTextProvider { diff --git a/crates/runtime/src/text_provider.rs b/crates/runtime/src/text_provider.rs index a4c77add..af64307f 100644 --- a/crates/runtime/src/text_provider.rs +++ b/crates/runtime/src/text_provider.rs @@ -1,6 +1,7 @@ //! Adapted from , which we split off into multiple files use crate::prelude::Language; use log::error; +use std::any::Any; use std::collections::HashMap; use std::fmt::Debug; use yarnspinner_core::prelude::*; @@ -22,6 +23,12 @@ pub trait TextProvider: Debug + Send + Sync { fn get_language(&self) -> Option; /// Returns whether the text for all lines announced by [`TextProvider::accept_line_hints`] are available, i.e. have been loaded and are ready to be used. fn are_lines_available(&self) -> bool; + /// Gets the [`TextProvider`] as a trait object. + /// This allows retrieving the concrete type by downcasting, using the `downcast_ref` method available through the `Any` trait. + fn as_any(&self) -> &dyn Any; + /// Gets the [`TextProvider`] as a mutable trait object. + /// This allows retrieving the concrete type by downcasting, using the `downcast_mut` method available through the `Any` trait. + fn as_any_mut(&mut self) -> &mut dyn Any; } #[allow(missing_docs)] @@ -104,4 +111,12 @@ impl TextProvider for StringTableTextProvider { .map(|(language, _)| language); translation_language == Some(language) } + + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } } diff --git a/crates/runtime/src/variable_storage.rs b/crates/runtime/src/variable_storage.rs index 47a8cb17..f833e6f7 100644 --- a/crates/runtime/src/variable_storage.rs +++ b/crates/runtime/src/variable_storage.rs @@ -1,4 +1,5 @@ //! Adapted from , which we split off into multiple files +use std::any::Any; use std::collections::HashMap; use std::fmt::Debug; use std::sync::{Arc, RwLock}; @@ -37,6 +38,12 @@ pub trait VariableStorage: Debug + Send + Sync { fn variables(&self) -> HashMap; /// Clears all variables in this variable storage. fn clear(&mut self); + /// Gets the [`VariableStorage`] as a trait object. + /// This allows retrieving the concrete type by downcasting, using the `downcast_ref` method available through the `Any` trait. + fn as_any(&self) -> &dyn Any; + /// Gets the [`VariableStorage`] as a mutable trait object. + /// This allows retrieving the concrete type by downcasting, using the `downcast_mut` method available through the `Any` trait. + fn as_any_mut(&mut self) -> &mut dyn Any; } impl Extend<(String, YarnValue)> for Box { @@ -112,6 +119,14 @@ impl VariableStorage for MemoryVariableStorage { fn clear(&mut self) { self.0.write().unwrap().clear(); } + + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } } impl MemoryVariableStorage { diff --git a/crates/yarnspinner/tests/test_base/text_provider.rs b/crates/yarnspinner/tests/test_base/text_provider.rs index 93fe18b9..dabc8b18 100644 --- a/crates/yarnspinner/tests/test_base/text_provider.rs +++ b/crates/yarnspinner/tests/test_base/text_provider.rs @@ -1,4 +1,7 @@ -use std::sync::{Arc, RwLock}; +use std::{ + any::Any, + sync::{Arc, RwLock}, +}; use yarnspinner_core::prelude::*; use yarnspinner_runtime::prelude::*; @@ -38,4 +41,12 @@ impl TextProvider for SharedTextProvider { fn are_lines_available(&self) -> bool { self.0.read().unwrap().are_lines_available() } + + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } }