Skip to content

Commit

Permalink
WIP implemented Serialize for the new InputLike trait
Browse files Browse the repository at this point in the history
Might be ways to make this cleaner, for now just trying to work towards
compiling to make sure the InputLike trait idea works out.
  • Loading branch information
paul-hansen committed Apr 16, 2023
1 parent dc00333 commit 7d8351a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ derive_more = { version = "0.99", default-features = false, features = [
] }
itertools = "0.10"
serde = { version = "1.0", features = ["derive"] }
erased-serde = "0.3"
fixedbitset = "0.4.2"
once_cell = "1.17.1"

Expand Down
2 changes: 1 addition & 1 deletion src/input_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ where
where
S: serde::de::SeqAccess<'de>,
{
let map = seq.next_element::<HashMap<A, Vec<UserInput>>>()?;
let map = seq.next_element::<HashMap<A, Vec<Box<dyn InputLike>>>>()?;
map.ok_or_else(|| {
serde::de::Error::invalid_length(0, &"one argument with type `map`")
})
Expand Down
21 changes: 20 additions & 1 deletion src/user_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fmt::Debug;
use bevy::prelude::ScanCode;
use bevy::utils::HashSet;
use petitset::PetitSet;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize, Serializer};

use crate::axislike::VirtualAxis;
use crate::scan_codes::QwertyScanCode;
Expand Down Expand Up @@ -39,6 +39,8 @@ pub trait InputLike: Send + Sync + Debug {

/// Enables comparing [`InputLike`] while keeping dynamic dispatch support.
fn eq_dyn(&self, other: &dyn InputLike) -> bool;

fn as_serialize(&self) -> Box<dyn erased_serde::Serialize>;
}

impl Clone for Box<dyn InputLike> {
Expand Down Expand Up @@ -83,6 +85,10 @@ impl InputLike for &dyn InputLike {
fn eq_dyn(&self, other: &dyn InputLike) -> bool {
self.eq_dyn(other)
}

fn as_serialize(&self) -> Box<dyn erased_serde::Serialize> {
self.as_serialize()
}
}

impl InputLike for Box<dyn InputLike> {
Expand All @@ -109,6 +115,19 @@ impl InputLike for Box<dyn InputLike> {
fn eq_dyn(&self, other: &dyn InputLike) -> bool {
self.eq_dyn(other)
}

fn as_serialize(&self) -> Box<dyn erased_serde::Serialize> {
self.as_serialize()
}
}

impl Serialize for dyn InputLike {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.as_serialize().serialize(serializer)
}
}

/// Some combination of user input, which may cross input-mode boundaries.
Expand Down

0 comments on commit 7d8351a

Please sign in to comment.