Switch from typemap_rev to trait-bound-typemap for context data

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/37/head
trivernis 2 years ago
parent b57dea5bb4
commit 54cd0fbcc8
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

20
Cargo.lock generated

@ -93,7 +93,7 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bromine"
version = "0.18.2"
version = "0.19.0"
dependencies = [
"async-trait",
"bincode",
@ -111,7 +111,7 @@ dependencies = [
"thiserror",
"tokio",
"tracing",
"typemap_rev",
"trait-bound-typemap",
]
[[package]]
@ -538,6 +538,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "multi-trait-object"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a54c9ed2b86c7927b63e7d51f8d7ed4e1f8513c8672828ca1a850ff9d32ab1c"
[[package]]
name = "nb"
version = "0.1.3"
@ -1035,9 +1041,13 @@ dependencies = [
]
[[package]]
name = "typemap_rev"
version = "0.1.5"
source = "git+https://github.com/Trivernis/typemap_rev?rev=750c67bffe8024d2a47725daa473f068ad653fc4#750c67bffe8024d2a47725daa473f068ad653fc4"
name = "trait-bound-typemap"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3631df5ba73c0e41b1aa337df3bcca7f15219f042f8fec1100857bc1eb60c767"
dependencies = [
"multi-trait-object",
]
[[package]]
name = "unicode-width"

@ -1,6 +1,6 @@
[package]
name = "bromine"
version = "0.18.3"
version = "0.19.0"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"
readme = "README.md"
@ -27,15 +27,11 @@ byteorder = "1.4.3"
async-trait = "0.1.52"
num_enum = "0.5.7"
futures-core = "0.3.21"
trait-bound-typemap = "0.3.3"
rmp-serde = { version = "1.0.0", optional = true }
bincode = { version = "1.3.3", optional = true }
serde_json = { version = "1.0.79", optional = true }
[dependencies.typemap_rev]
version = "0.1.5"
git = "https://github.com/Trivernis/typemap_rev"
rev = "750c67bffe8024d2a47725daa473f068ad653fc4"
[dependencies.serde]
optional = true
version = "1.0.136"

@ -12,19 +12,18 @@ use crate::namespaces::namespace::Namespace;
#[cfg(feature = "serialize")]
use crate::payload::DynamicSerializer;
use crate::protocol::AsyncStreamProtocolListener;
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use typemap_rev::{TypeMap, TypeMapKey};
use trait_bound_typemap::{KeyCanExtend, SendSyncTypeMap, TypeMap, TypeMapEntry, TypeMapKey};
/// A builder for the IPC server or client.
/// ```no_run
/// use std::net::ToSocketAddrs;
/// use typemap_rev::TypeMapKey;
/// use trait_bound_typemap::TypeMapKey;
/// use bromine::IPCBuilder;
/// use tokio::net::TcpListener;
/// use bromine::prelude::Response;
@ -61,7 +60,7 @@ pub struct IPCBuilder<L: AsyncStreamProtocolListener> {
handler: EventHandler,
address: Option<L::AddressType>,
namespaces: HashMap<String, Namespace>,
data: TypeMap,
data: SendSyncTypeMap,
timeout: Duration,
#[cfg(feature = "serialize")]
default_serializer: DynamicSerializer,
@ -86,7 +85,7 @@ where
handler,
address: None,
namespaces: HashMap::new(),
data: TypeMap::new(),
data: SendSyncTypeMap::new(),
timeout: Duration::from_secs(60),
#[cfg(feature = "serialize")]
default_serializer: DynamicSerializer::first_available(),
@ -94,14 +93,17 @@ where
}
/// Adds globally shared data
pub fn insert<K: TypeMapKey>(mut self, value: K::Value) -> Self {
pub fn insert<K: TypeMapKey>(mut self, value: K::Value) -> Self
where
<K as TypeMapKey>::Value: Send + Sync,
{
self.data.insert::<K>(value);
self
}
/// Adds all the data from the other given type map
pub fn insert_all<I: IntoIterator<Item = (TypeId, Box<dyn Any + Send + Sync>)>>(
pub fn insert_all<I: IntoIterator<Item = TypeMapEntry<K>>, K: KeyCanExtend<SendSyncTypeMap>>(
mut self,
value: I,
) -> Self {

@ -10,7 +10,7 @@ use std::sync::Arc;
use std::time::Duration;
use tokio::sync::oneshot;
use tokio::sync::RwLock;
use typemap_rev::TypeMap;
use trait_bound_typemap::SendSyncTypeMap;
#[cfg(feature = "serialize")]
use crate::payload::DynamicSerializer;
@ -22,7 +22,7 @@ use crate::payload::DynamicSerializer;
pub struct IPCClient {
pub(crate) handler: EventHandler,
pub(crate) namespaces: HashMap<String, Namespace>,
pub(crate) data: Arc<RwLock<TypeMap>>,
pub(crate) data: Arc<RwLock<SendSyncTypeMap>>,
pub(crate) reply_listeners: ReplyListeners,
pub(crate) timeout: Duration,

@ -7,7 +7,7 @@ use std::sync::Arc;
use tokio::sync::mpsc::Receiver;
use tokio::sync::{mpsc, oneshot, Mutex, RwLock};
use tokio::time::Duration;
use typemap_rev::TypeMap;
use trait_bound_typemap::SendSyncTypeMap;
use crate::error::{Error, Result};
use crate::event::{Event, EventType};
@ -38,7 +38,7 @@ pub struct Context {
emitter: StreamEmitter,
/// Field to store additional context data
pub data: Arc<RwLock<TypeMap>>,
pub data: Arc<RwLock<SendSyncTypeMap>>,
stop_sender: Arc<Mutex<Option<oneshot::Sender<()>>>>,
@ -55,7 +55,7 @@ pub struct Context {
impl Context {
pub(crate) fn new(
emitter: StreamEmitter,
data: Arc<RwLock<TypeMap>>,
data: Arc<RwLock<SendSyncTypeMap>>,
stop_sender: Option<oneshot::Sender<()>>,
reply_listeners: ReplyListeners,
reply_timeout: Duration,

@ -13,7 +13,7 @@ use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use typemap_rev::TypeMap;
use trait_bound_typemap::SendSyncTypeMap;
/// The IPC Server listening for connections.
/// Use the [IPCBuilder](crate::builder::IPCBuilder) to create a server.
@ -21,7 +21,7 @@ use typemap_rev::TypeMap;
pub struct IPCServer {
pub(crate) handler: EventHandler,
pub(crate) namespaces: HashMap<String, Namespace>,
pub(crate) data: TypeMap,
pub(crate) data: SendSyncTypeMap,
pub(crate) timeout: Duration,
#[cfg(feature = "serialize")]

@ -61,7 +61,7 @@
//! Server Example:
//! ```no_run
//! use std::net::ToSocketAddrs;
//! use typemap_rev::TypeMapKey;
//! use trait_bound_typemap::{TypeMapKey, TypeMap};
//! use bromine::IPCBuilder;
//! use bromine::prelude::*;
//! use tokio::net::TcpListener;

@ -4,7 +4,7 @@ use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::sync::RwLock;
use typemap_rev::TypeMapKey;
use trait_bound_typemap::{TypeMap, TypeMapKey};
pub async fn get_counter_from_context(ctx: &Context) -> CallCounter {
let data = ctx.data.read().await;

Loading…
Cancel
Save