From ba431972bffb46e2b080f3b14c54dac781b5158b Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Wed, 3 Jan 2024 17:13:12 -0700 Subject: [PATCH] make phantom Send/Sync --- rpc-toolkit/src/cli.rs | 9 +++---- rpc-toolkit/src/command.rs | 3 +-- rpc-toolkit/src/handler/adapters.rs | 15 +++++------ rpc-toolkit/src/handler/from_fn.rs | 42 ++++++++++++++++++----------- rpc-toolkit/src/handler/marker.rs | 1 - rpc-toolkit/src/handler/mod.rs | 1 - rpc-toolkit/src/handler/parent.rs | 7 +++-- rpc-toolkit/src/util.rs | 33 +++++++++++++---------- 8 files changed, 60 insertions(+), 51 deletions(-) delete mode 100644 rpc-toolkit/src/handler/marker.rs diff --git a/rpc-toolkit/src/cli.rs b/rpc-toolkit/src/cli.rs index c518842..bb84a25 100644 --- a/rpc-toolkit/src/cli.rs +++ b/rpc-toolkit/src/cli.rs @@ -1,7 +1,6 @@ use std::any::TypeId; use std::collections::VecDeque; use std::ffi::OsString; -use std::marker::PhantomData; use clap::{CommandFactory, FromArgMatches}; use imbl_value::Value; @@ -12,7 +11,7 @@ use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader use url::Url; use yajrc::{Id, RpcError}; -use crate::util::{internal_error, parse_error, Flat}; +use crate::util::{internal_error, parse_error, Flat, PhantomData}; use crate::{ AnyHandler, CliBindings, CliBindingsAny, DynHandler, HandleAny, HandleAnyArgs, HandleArgs, Handler, HandlerTypes, IntoContext, Name, ParentHandler, @@ -35,7 +34,7 @@ impl root_handler: ParentHandler, ) -> Self { Self { - _phantom: PhantomData, + _phantom: PhantomData::new(), make_ctx: Box::new(make_ctx), root_handler, } @@ -172,7 +171,7 @@ pub struct CallRemoteHandler { impl CallRemoteHandler { pub fn new(handler: RemoteHandler) -> Self { Self { - _phantom: PhantomData, + _phantom: PhantomData::new(), handler: handler, } } @@ -180,7 +179,7 @@ impl CallRemoteHandler { impl Clone for CallRemoteHandler { fn clone(&self) -> Self { Self { - _phantom: PhantomData, + _phantom: PhantomData::new(), handler: self.handler.clone(), } } diff --git a/rpc-toolkit/src/command.rs b/rpc-toolkit/src/command.rs index 3dabecf..8ad8645 100644 --- a/rpc-toolkit/src/command.rs +++ b/rpc-toolkit/src/command.rs @@ -1,4 +1,3 @@ -use std::marker::PhantomData; use std::sync::Arc; use clap::{ArgMatches, CommandFactory, FromArgMatches}; @@ -9,7 +8,7 @@ use serde::de::DeserializeOwned; use serde::ser::Serialize; use yajrc::RpcError; -use crate::util::{extract, Flat}; +use crate::util::{extract, Flat, PhantomData}; /// Stores a command's implementation for a given context /// Can be created from anything that implements ParentCommand, AsyncCommand, or SyncCommand diff --git a/rpc-toolkit/src/handler/adapters.rs b/rpc-toolkit/src/handler/adapters.rs index d92e4ad..1b08329 100644 --- a/rpc-toolkit/src/handler/adapters.rs +++ b/rpc-toolkit/src/handler/adapters.rs @@ -1,7 +1,6 @@ use std::any::TypeId; use std::collections::VecDeque; use std::fmt::Debug; -use std::marker::PhantomData; use std::sync::Arc; use clap::{CommandFactory, FromArgMatches}; @@ -11,7 +10,7 @@ use serde::de::DeserializeOwned; use serde::Serialize; use yajrc::RpcError; -use crate::util::{internal_error, parse_error, Flat}; +use crate::util::{internal_error, parse_error, Flat, PhantomData}; use crate::{ iter_from_ctx_and_handler, AnyContext, AnyHandler, CallRemote, CliBindings, DynHandler, EitherContext, HandleArgs, Handler, HandlerTypes, IntoContext, IntoHandlers, PrintCliResult, @@ -72,7 +71,7 @@ impl HandlerExt for T { F: Fn(HandleArgs, Self::Ok) -> Result<(), Self::Err>, { CustomDisplayFn { - _phantom: PhantomData, + _phantom: PhantomData::new(), print: display, handler: self, } @@ -85,14 +84,14 @@ impl HandlerExt for T { F: Fn(Params, InheritedParams) -> Self::InheritedParams, { InheritanceHandler { - _phantom: PhantomData, + _phantom: PhantomData::new(), handler: self, inherit: f, } } fn with_remote_cli(self) -> RemoteCli { RemoteCli { - _phantom: PhantomData, + _phantom: PhantomData::new(), handler: self, } } @@ -366,7 +365,7 @@ pub struct CustomDisplayFn { impl Clone for CustomDisplayFn { fn clone(&self) -> Self { Self { - _phantom: PhantomData, + _phantom: PhantomData::new(), print: self.print.clone(), handler: self.handler.clone(), } @@ -508,7 +507,7 @@ pub struct RemoteCli { impl Clone for RemoteCli { fn clone(&self) -> Self { Self { - _phantom: PhantomData, + _phantom: PhantomData::new(), handler: self.handler.clone(), } } @@ -645,7 +644,7 @@ impl Clone { fn clone(&self) -> Self { Self { - _phantom: PhantomData, + _phantom: PhantomData::new(), handler: self.handler.clone(), inherit: self.inherit.clone(), } diff --git a/rpc-toolkit/src/handler/from_fn.rs b/rpc-toolkit/src/handler/from_fn.rs index e9cf099..7c151a3 100644 --- a/rpc-toolkit/src/handler/from_fn.rs +++ b/rpc-toolkit/src/handler/from_fn.rs @@ -1,7 +1,6 @@ use std::any::TypeId; use std::collections::VecDeque; use std::fmt::Display; -use std::marker::PhantomData; use clap::{ArgMatches, Command, CommandFactory, FromArgMatches}; use futures::Future; @@ -10,7 +9,7 @@ use imbl_value::Value; use serde::de::DeserializeOwned; use serde::Serialize; -use crate::marker::LeafHandler; +use crate::util::PhantomData; use crate::{ AnyContext, CliBindings, Empty, HandleArgs, Handler, HandlerTypes, IntoContext, PrintCliResult, }; @@ -30,7 +29,7 @@ impl FromFn { impl Clone for FromFn { fn clone(&self) -> Self { Self { - _phantom: PhantomData, + _phantom: PhantomData::new(), function: self.function.clone(), blocking: self.blocking, metadata: self.metadata.clone(), @@ -44,7 +43,6 @@ impl std::fmt::Debug for FromFn { .finish() } } -impl LeafHandler for FromFn {} impl PrintCliResult for FromFn where Self: HandlerTypes, @@ -62,7 +60,7 @@ where { FromFn { function, - _phantom: PhantomData, + _phantom: PhantomData::new(), blocking: false, metadata: OrdMap::new(), } @@ -74,7 +72,7 @@ where { FromFn { function, - _phantom: PhantomData, + _phantom: PhantomData::new(), blocking: true, metadata: OrdMap::new(), } @@ -85,10 +83,22 @@ pub struct FromFnAsync { function: F, metadata: OrdMap<&'static str, Value>, } +unsafe impl Send for FromFnAsync +where + F: Send, + OrdMap<&'static str, Value>: Send, +{ +} +unsafe impl Sync for FromFnAsync +where + F: Sync, + OrdMap<&'static str, Value>: Sync, +{ +} impl Clone for FromFnAsync { fn clone(&self) -> Self { Self { - _phantom: PhantomData, + _phantom: PhantomData::new(), function: self.function.clone(), metadata: self.metadata.clone(), } @@ -116,7 +126,7 @@ where { FromFnAsync { function, - _phantom: PhantomData, + _phantom: PhantomData::new(), metadata: OrdMap::new(), } } @@ -160,7 +170,7 @@ where impl HandlerTypes for FromFnAsync where F: Fn() -> Fut + Send + Sync + Clone + 'static, - Fut: Future> + Send + Sync + 'static, + Fut: Future> + Send + 'static, T: Send + Sync + 'static, E: Send + Sync + 'static, { @@ -173,7 +183,7 @@ where impl Handler for FromFnAsync where F: Fn() -> Fut + Send + Sync + Clone + 'static, - Fut: Future> + Send + Sync + 'static, + Fut: Future> + Send + 'static, T: Send + Sync + 'static, E: Send + Sync + 'static, { @@ -234,7 +244,7 @@ impl HandlerTypes for FromFnAsync Fut + Send + Sync + Clone + 'static, - Fut: Future> + Send + Sync + 'static, + Fut: Future> + Send + 'static, T: Send + Sync + 'static, E: Send + Sync + 'static, { @@ -248,7 +258,7 @@ impl Handler for FromFnAsync where Context: IntoContext, F: Fn(Context) -> Fut + Send + Sync + Clone + 'static, - Fut: Future> + Send + Sync + 'static, + Fut: Future> + Send + 'static, T: Send + Sync + 'static, E: Send + Sync + 'static, { @@ -314,7 +324,7 @@ impl HandlerTypes for FromFnAsync Fut + Send + Sync + Clone + 'static, - Fut: Future> + Send + Sync + 'static, + Fut: Future> + Send + 'static, Params: DeserializeOwned + Send + Sync + 'static, T: Send + Sync + 'static, E: Send + Sync + 'static, @@ -329,7 +339,7 @@ impl Handler for FromFnAsync Fut + Send + Sync + Clone + 'static, - Fut: Future> + Send + Sync + 'static, + Fut: Future> + Send + 'static, Params: DeserializeOwned + Send + Sync + 'static, T: Send + Sync + 'static, E: Send + Sync + 'static, @@ -407,7 +417,7 @@ impl HandlerTypes where Context: IntoContext, F: Fn(Context, Params, InheritedParams) -> Fut + Send + Sync + Clone + 'static, - Fut: Future> + Send + Sync + 'static, + Fut: Future> + Send + 'static, Params: DeserializeOwned + Send + Sync + 'static, InheritedParams: DeserializeOwned + Send + Sync + 'static, T: Send + Sync + 'static, @@ -424,7 +434,7 @@ impl Handler where Context: IntoContext, F: Fn(Context, Params, InheritedParams) -> Fut + Send + Sync + Clone + 'static, - Fut: Future> + Send + Sync + 'static, + Fut: Future> + Send + 'static, Params: DeserializeOwned + Send + Sync + 'static, InheritedParams: DeserializeOwned + Send + Sync + 'static, T: Send + Sync + 'static, diff --git a/rpc-toolkit/src/handler/marker.rs b/rpc-toolkit/src/handler/marker.rs deleted file mode 100644 index 32a0f18..0000000 --- a/rpc-toolkit/src/handler/marker.rs +++ /dev/null @@ -1 +0,0 @@ -pub trait LeafHandler {} diff --git a/rpc-toolkit/src/handler/mod.rs b/rpc-toolkit/src/handler/mod.rs index f531a5b..36a898e 100644 --- a/rpc-toolkit/src/handler/mod.rs +++ b/rpc-toolkit/src/handler/mod.rs @@ -15,7 +15,6 @@ use crate::util::{internal_error, invalid_params, Flat}; pub mod adapters; pub mod from_fn; -pub mod marker; pub mod parent; pub use adapters::*; diff --git a/rpc-toolkit/src/handler/parent.rs b/rpc-toolkit/src/handler/parent.rs index f742d12..d137a94 100644 --- a/rpc-toolkit/src/handler/parent.rs +++ b/rpc-toolkit/src/handler/parent.rs @@ -1,6 +1,5 @@ use std::any::TypeId; use std::collections::VecDeque; -use std::marker::PhantomData; use std::sync::Arc; use clap::{ArgMatches, Command, CommandFactory, FromArgMatches}; @@ -10,7 +9,7 @@ use serde::de::DeserializeOwned; use serde::Serialize; use yajrc::RpcError; -use crate::util::{combine, Flat}; +use crate::util::{combine, Flat, PhantomData}; use crate::{ AnyContext, AnyHandler, CliBindings, DynHandler, Empty, HandleAny, HandleAnyArgs, HandleArgs, Handler, HandlerTypes, IntoContext, OrEmpty, @@ -104,7 +103,7 @@ pub struct ParentHandler { impl ParentHandler { pub fn new() -> Self { Self { - _phantom: PhantomData, + _phantom: PhantomData::new(), subcommands: SubcommandMap(OrdMap::new()), metadata: OrdMap::new(), } @@ -117,7 +116,7 @@ impl ParentHandler { impl Clone for ParentHandler { fn clone(&self) -> Self { Self { - _phantom: PhantomData, + _phantom: PhantomData::new(), subcommands: self.subcommands.clone(), metadata: self.metadata.clone(), } diff --git a/rpc-toolkit/src/util.rs b/rpc-toolkit/src/util.rs index 7667930..b457e58 100644 --- a/rpc-toolkit/src/util.rs +++ b/rpc-toolkit/src/util.rs @@ -1,4 +1,4 @@ -use std::fmt::Display; +use std::fmt::{Debug, Display}; use futures::future::{BoxFuture, FusedFuture}; use futures::stream::FusedStream; @@ -222,16 +222,21 @@ where } } -// #[derive(Debug)] -// pub enum Infallible {} -// impl From for T { -// fn from(value: Infallible) -> Self { -// match value {} -// } -// } -// impl std::fmt::Display for Infallible { -// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { -// match *self {} -// } -// } -// impl std::error::Error for Infallible {} +pub struct PhantomData(std::marker::PhantomData); +impl PhantomData { + pub fn new() -> Self { + PhantomData(std::marker::PhantomData) + } +} +impl Clone for PhantomData { + fn clone(&self) -> Self { + PhantomData::new() + } +} +impl Debug for PhantomData { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} +unsafe impl Send for PhantomData {} +unsafe impl Sync for PhantomData {}