Skip to content

Commit

Permalink
Clean up StringName pooling and Singleton usages
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Aug 8, 2024
1 parent e5e7d88 commit 3e55e96
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 119 deletions.
1 change: 1 addition & 0 deletions backends/sdl/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ fn convert_scancode(scan_code: SDL_Keycode) -> Option<VirtualKey> {
SDL_KeyCode::SDLK_DOWN => Some(ArrowDown),
SDL_KeyCode::SDLK_LEFT => Some(ArrowLeft),
SDL_KeyCode::SDLK_RIGHT => Some(ArrowRight),
SDL_KeyCode::SDLK_SPACE => Some(Space),
_ => None,
}
}
6 changes: 6 additions & 0 deletions core/common/src/abstractions/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ impl Display for CallbackError {
#[derive(Clone)]
pub struct Callable(Arc<dyn Fn(&[Variant]) -> Result<Variant, CallbackError>>);

/// Represents a value that can be converted into a [`Callable`] function.
pub trait IntoCallable {
/// Converts the value into a [`Callable`] function.
fn into_callable(self) -> Callable;
}

impl Callable {
/// Creates a new boxed callable function from the given function.
pub fn from_function(function: impl Fn(&[Variant]) -> Result<Variant, CallbackError> + 'static) -> Self {
Expand Down
13 changes: 13 additions & 0 deletions core/common/src/abstractions/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,19 @@ impl_variant!(Quat, Quat);
impl_variant!(Color, Color);
impl_variant!(Color32, Color32);

/// Allow [`Variant`] to be converted to/from itself.
impl ToVariant for Variant {
fn to_variant(&self) -> Variant {
self.clone()
}
}

impl FromVariant for Variant {
fn from_variant(variant: Variant) -> Result<Self, VariantError> {
Ok(variant)
}
}

/// Allow [`Arc`] of [`Any`] to be converted to/from Variant.
impl<T: Any> ToVariant for Arc<T> {
fn to_variant(&self) -> Variant {
Expand Down
15 changes: 4 additions & 11 deletions core/common/src/concurrency/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::{
task::{Context, Poll},
};

use crate::Singleton;

/// A continuation that can be executed.
type Continuation = dyn FnOnce() -> ();

Expand Down Expand Up @@ -55,20 +57,12 @@ impl<T> Task<T> {
}

/// A scheduler for [`Task`]s.
#[derive(Default)]
#[derive(Default, Singleton)]
struct TaskScheduler {
continuations: Mutex<crate::SwapVec<Box<Continuation>>>,
}

/// The global task scheduler.
static TASK_SCHEDULER: crate::UnsafeSingleton<TaskScheduler> = crate::UnsafeSingleton::default();

impl TaskScheduler {
/// Returns the current task scheduler.
pub fn current() -> &'static Self {
&TASK_SCHEDULER
}

/// Schedules a [`Continuation`] to be executed.
pub fn schedule(&self, continuation: Box<Continuation>) {
let mut continuations = self.continuations.lock().unwrap();
Expand Down Expand Up @@ -129,11 +123,10 @@ impl<T: Unpin> Future for Task<T> {
#[cfg(test)]
mod tests {
use super::*;
use crate::BlockableFuture;

#[test]
fn test_basic_task_continuations() {
let scheduler = TaskScheduler::current();
let scheduler = TaskScheduler::instance();

scheduler.schedule(Box::new(|| {
println!("Hello, world!");
Expand Down
21 changes: 8 additions & 13 deletions core/common/src/io/virtualfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub use local::*;
pub use memory::*;

use super::{InputStream, OutputStream};
use crate::{StringName, ToStringName, UnsafeSingleton};
use crate::{Singleton, StringName, ToStringName};

mod local;
mod memory;
Expand Down Expand Up @@ -35,12 +35,11 @@ pub trait FileSystem: Send + Sync {
/// This is a singleton that is used to manage [`FileSystem`] implementations.
/// File systems can be registered here, and will be used subsequently for file
/// operations on [`VirtualPath`] instances.
#[derive(Singleton)]
pub struct FileSystemManager {
file_systems: Vec<Box<dyn FileSystem>>,
}

static mut FILE_SYSTEM_MANAGER: UnsafeSingleton<FileSystemManager> = UnsafeSingleton::default();

impl Default for FileSystemManager {
fn default() -> Self {
Self {
Expand All @@ -56,22 +55,18 @@ impl Default for FileSystemManager {
impl FileSystemManager {
/// Registers a new [`FileSystem`] with the manager.
pub fn register(file_system: impl FileSystem + 'static) {
unsafe {
FILE_SYSTEM_MANAGER.file_systems.push(Box::new(file_system));
}
Self::instance().file_systems.push(Box::new(file_system));
}

/// Finds the appropriate [`FileSystem`] for the given [`VirtualPath`].
pub fn with_filesystem<R>(path: &VirtualPath, body: impl FnOnce(&dyn FileSystem) -> R) -> R {
unsafe {
for file_system in &FILE_SYSTEM_MANAGER.file_systems {
if file_system.can_handle(path) {
return body(file_system.as_ref());
}
for file_system in &Self::instance().file_systems {
if file_system.can_handle(path) {
return body(file_system.as_ref());
}

panic!("No file system found for scheme {}", path.scheme());
}

˚ panic!("No file system found for scheme {}", path.scheme());
}
}

Expand Down
4 changes: 3 additions & 1 deletion core/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#![feature(noop_waker)]
#![feature(async_closure)]

extern crate self as common;

pub use abstractions::*;
pub use collections::*;
pub use concurrency::*;
Expand All @@ -33,4 +35,4 @@ mod network;
mod strings;
mod utilities;

pub use macros::{profiling, Asset, Deserialize, Reflect, Serialize, Trace};
pub use macros::{profiling, Asset, Deserialize, Reflect, Serialize, Singleton, Trace};
2 changes: 0 additions & 2 deletions core/common/src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,6 @@ struct LuaArc<T: ?Sized>(Arc<T>);

impl<T: ?Sized> LuaUserData for LuaArc<T> {}

// TODO: think about using Reflect-able types for Lua?

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit 3e55e96

Please sign in to comment.