Skip to content

Commit

Permalink
Remove anymap dependency (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
intendednull authored Oct 26, 2024
1 parent 0b14ff9 commit fcf42e7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
1 change: 0 additions & 1 deletion crates/yewdux/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ future = []
doctests = []

[dependencies]
anymap = "1.0.0-beta.2"
log = "0.4.16"
serde = { version = "1.0.114", features = ["rc"] }
serde_json = "1.0.64"
Expand Down
37 changes: 37 additions & 0 deletions crates/yewdux/src/anymap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::{
any::{Any, TypeId},
collections::HashMap,
};

#[derive(Default)]
pub(crate) struct AnyMap {
map: HashMap<TypeId, Box<dyn Any>>,
}

impl AnyMap {
pub(crate) fn entry<T: 'static>(&mut self) -> Entry<T> {
Entry {
map: &mut self.map,
_marker: std::marker::PhantomData,
}
}
}

pub(crate) struct Entry<'a, T: 'static> {
map: &'a mut HashMap<TypeId, Box<dyn Any>>,
_marker: std::marker::PhantomData<T>,
}

impl<'a, T: 'static> Entry<'a, T> {
pub(crate) fn or_insert_with<F>(self, default: F) -> &'a mut T
where
F: FnOnce() -> T,
{
let type_id = TypeId::of::<T>();
let value = self
.map
.entry(type_id)
.or_insert_with(|| Box::new(default()));
value.downcast_mut().expect("type id mismatch")
}
}
3 changes: 1 addition & 2 deletions crates/yewdux/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::rc::Rc;

use anymap::AnyMap;

use crate::{
anymap::AnyMap,
mrc::Mrc,
store::{Reducer, Store},
subscriber::{Callable, SubscriberId, Subscribers},
Expand Down
1 change: 1 addition & 0 deletions crates/yewdux/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
//! ```
#![allow(clippy::needless_doctest_main)]

mod anymap;
pub mod context;
pub mod context_provider;
pub mod dispatch;
Expand Down

0 comments on commit fcf42e7

Please sign in to comment.