generated from Leafwing-Studios/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix broken deserialization for inputs and
InputMap
s (#622)
* Fix the broken deserialization of inputs and `InputMap`s * docs
- Loading branch information
Showing
14 changed files
with
408 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,55 @@ | ||
//! Type tag registration for trait objects | ||
pub use serde_flexitos::{MapRegistry, Registry}; | ||
use std::collections::BTreeMap; | ||
|
||
pub use serde_flexitos::Registry; | ||
use serde_flexitos::{DeserializeFn, GetError}; | ||
|
||
/// A trait for registering type tags. | ||
pub trait RegisterTypeTag<'de, T: ?Sized> { | ||
/// Registers the specified type tag into the [`MapRegistry`]. | ||
fn register_typetag(registry: &mut MapRegistry<T>); | ||
/// Registers the specified type tag into the [`InfallibleMapRegistry`]. | ||
fn register_typetag(registry: &mut InfallibleMapRegistry<T>); | ||
} | ||
|
||
/// An infallible version of [`MapRegistry`](serde_flexitos::MapRegistry) | ||
/// that allows multiple registrations of deserializers. | ||
pub struct InfallibleMapRegistry<O: ?Sized, I = &'static str> { | ||
deserialize_fns: BTreeMap<I, Option<DeserializeFn<O>>>, | ||
trait_object_name: &'static str, | ||
} | ||
|
||
impl<O: ?Sized, I> InfallibleMapRegistry<O, I> { | ||
/// Creates a new registry, using `trait_object_name` as the name of `O` for diagnostic purposes. | ||
#[inline] | ||
pub fn new(trait_object_name: &'static str) -> Self { | ||
Self { | ||
deserialize_fns: BTreeMap::new(), | ||
trait_object_name, | ||
} | ||
} | ||
} | ||
|
||
impl<O: ?Sized, I: Ord> Registry for InfallibleMapRegistry<O, I> { | ||
type Identifier = I; | ||
type TraitObject = O; | ||
|
||
#[inline] | ||
fn register(&mut self, id: I, deserialize_fn: DeserializeFn<O>) { | ||
self.deserialize_fns | ||
.entry(id) | ||
.or_insert_with(|| Some(deserialize_fn)); | ||
} | ||
|
||
#[inline] | ||
fn get_deserialize_fn(&self, id: I) -> Result<&DeserializeFn<O>, GetError<I>> { | ||
match self.deserialize_fns.get(&id) { | ||
Some(Some(deserialize_fn)) => Ok(deserialize_fn), | ||
_ => Err(GetError::NotRegistered { id }), | ||
} | ||
} | ||
|
||
#[inline] | ||
fn get_trait_object_name(&self) -> &'static str { | ||
self.trait_object_name | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.