Skip to content

Commit

Permalink
added as_any and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
stargazing-dino committed Feb 6, 2024
1 parent 178ea65 commit 8978a63
Show file tree
Hide file tree
Showing 6 changed files with 132 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
73 changes: 73 additions & 0 deletions crates/runtime/src/dialogue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::markup::{DialogueTextProcessor, LineParser, MarkupParseError};
use crate::prelude::*;
use log::error;
use std::any::Any;
use std::fmt::Debug;
use thiserror::Error;
use yarnspinner_core::prelude::*;
Expand Down Expand Up @@ -185,6 +186,78 @@ impl Dialogue {
pub fn variable_storage_mut(&mut self) -> &mut dyn VariableStorage {
self.vm.variable_storage_mut()
}

/// Allows for type-agnostic access to the [`TextProvider`]. See [`TextProvider::as_any`].
///
/// # Example
///
/// ```
/// use std::any::Any;
/// use yarnspinner_runtime::prelude::*;
///
/// struct MyTextProvider;
/// impl TextProvider for MyTextProvider { /* .. */ }
///
/// let dialogue = Dialogue::new(/* .. */);
/// let text_provider = dialogue.text_provider_as_any.downcast_ref::<MyTextProvider>();
/// ```
pub fn text_provider_as_any(&self) -> &dyn Any {
self.text_provider().as_any()
}

/// Allows for type-agnostic access to the [`TextProvider`]. See [`TextProvider::as_any_mut`].
///
/// # Example
///
/// ```
/// use std::any::Any;
/// use yarnspinner_runtime::prelude::*;
///
/// struct MyTextProvider;
/// impl TextProvider for MyTextProvider { /* .. */ }
///
/// let mut dialogue = Dialogue::new(/* .. */);
/// let mut text_provider = dialogue.text_provider_mut_as_any.downcast_mut::<MyTextProvider>();
/// ```
pub fn text_provider_mut_as_any(&mut self) -> &mut dyn Any {
self.text_provider_mut().as_any_mut()
}

/// Allows for type-agnostic access to the [`VariableStorage`]. See [`VariableStorage::as_any`].
///
/// # Example
///
/// ```
/// use std::any::Any;
/// use yarnspinner_runtime::prelude::*;
///
/// struct MyVariableStorage;
/// impl VariableStorage for MyVariableStorage { /* .. */ }
///
/// let dialogue = Dialogue::new(/* .. */);
/// let variable_storage = dialogue.variable_storage_as_any.downcast_ref::<MyVariableStorage>();
/// ```
pub fn variable_storage_as_any(&self) -> &dyn Any {
self.variable_storage().as_any()
}

/// Allows for type-agnostic access to the [`VariableStorage`]. See [`VariableStorage::as_any_mut`].
///
/// # Example
///
/// ```
/// use std::any::Any;
/// use yarnspinner_runtime::prelude::*;
///
/// struct MyVariableStorage;
/// impl VariableStorage for MyVariableStorage { /* .. */ }
///
/// let mut dialogue = Dialogue::new(/* .. */);
/// let mut variable_storage = dialogue.variable_storage_mut_as_any.downcast_mut::<MyVariableStorage>();
/// ```
pub fn variable_storage_mut_as_any(&mut self) -> &mut dyn Any {
self.variable_storage_mut().as_any_mut()
}
}

// VM proxy
Expand Down
16 changes: 16 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,13 @@ 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 via [`Any::downcast_ref`].
fn as_any(&self) -> &dyn Any;
/// Gets the [`TextProvider`] as a mutable trait object.
/// This allows retrieving the concrete type via [`Any::downcast_mut`].
fn as_any_mut(&mut self) -> &mut dyn Any;
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -104,4 +112,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 via [`Any::downcast_ref`].
fn as_any(&self) -> &dyn Any;
/// Gets the [`VariableStorage`] as a mutable trait object.
/// This allows retrieving the concrete type via [`Any::downcast_mut`].
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 8978a63

Please sign in to comment.