Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add as_any and as_any_mut #177

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading