Skip to content

Commit

Permalink
Merge pull request #177 from stargazing-dino/as_any
Browse files Browse the repository at this point in the history
  • Loading branch information
janhohenheim authored Feb 6, 2024
2 parents 178ea65 + e9fbe3a commit 6dd2c60
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions crates/runtime/src/text_provider.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Adapted from <https://github.com/YarnSpinnerTool/YarnSpinner/blob/da39c7195107d8211f21c263e4084f773b84eaff/YarnSpinner/Dialogue.cs>, 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::*;
Expand All @@ -22,6 +23,12 @@ pub trait TextProvider: Debug + Send + Sync {
fn get_language(&self) -> Option<Language>;
/// 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)]
Expand Down Expand Up @@ -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
}
}
15 changes: 15 additions & 0 deletions crates/runtime/src/variable_storage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Adapted from <https://github.com/YarnSpinnerTool/YarnSpinner/blob/da39c7195107d8211f21c263e4084f773b84eaff/YarnSpinner/Dialogue.cs>, which we split off into multiple files
use std::any::Any;
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::{Arc, RwLock};
Expand Down Expand Up @@ -37,6 +38,12 @@ pub trait VariableStorage: Debug + Send + Sync {
fn variables(&self) -> HashMap<String, YarnValue>;
/// 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<dyn VariableStorage> {
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion crates/yarnspinner/tests/test_base/text_provider.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::sync::{Arc, RwLock};
use std::{
any::Any,
sync::{Arc, RwLock},
};
use yarnspinner_core::prelude::*;
use yarnspinner_runtime::prelude::*;

Expand Down Expand Up @@ -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
}
}

0 comments on commit 6dd2c60

Please sign in to comment.