make phantom Send/Sync

This commit is contained in:
Aiden McClelland
2024-01-03 17:13:12 -07:00
parent e13808674a
commit ba431972bf
8 changed files with 60 additions and 51 deletions

View File

@@ -1,7 +1,6 @@
use std::any::TypeId; use std::any::TypeId;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::ffi::OsString; use std::ffi::OsString;
use std::marker::PhantomData;
use clap::{CommandFactory, FromArgMatches}; use clap::{CommandFactory, FromArgMatches};
use imbl_value::Value; use imbl_value::Value;
@@ -12,7 +11,7 @@ use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader
use url::Url; use url::Url;
use yajrc::{Id, RpcError}; use yajrc::{Id, RpcError};
use crate::util::{internal_error, parse_error, Flat}; use crate::util::{internal_error, parse_error, Flat, PhantomData};
use crate::{ use crate::{
AnyHandler, CliBindings, CliBindingsAny, DynHandler, HandleAny, HandleAnyArgs, HandleArgs, AnyHandler, CliBindings, CliBindingsAny, DynHandler, HandleAny, HandleAnyArgs, HandleArgs,
Handler, HandlerTypes, IntoContext, Name, ParentHandler, Handler, HandlerTypes, IntoContext, Name, ParentHandler,
@@ -35,7 +34,7 @@ impl<Context: crate::Context + Clone, Config: CommandFactory + FromArgMatches>
root_handler: ParentHandler, root_handler: ParentHandler,
) -> Self { ) -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData::new(),
make_ctx: Box::new(make_ctx), make_ctx: Box::new(make_ctx),
root_handler, root_handler,
} }
@@ -172,7 +171,7 @@ pub struct CallRemoteHandler<Context, RemoteHandler> {
impl<Context, RemoteHandler> CallRemoteHandler<Context, RemoteHandler> { impl<Context, RemoteHandler> CallRemoteHandler<Context, RemoteHandler> {
pub fn new(handler: RemoteHandler) -> Self { pub fn new(handler: RemoteHandler) -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData::new(),
handler: handler, handler: handler,
} }
} }
@@ -180,7 +179,7 @@ impl<Context, RemoteHandler> CallRemoteHandler<Context, RemoteHandler> {
impl<Context, RemoteHandler: Clone> Clone for CallRemoteHandler<Context, RemoteHandler> { impl<Context, RemoteHandler: Clone> Clone for CallRemoteHandler<Context, RemoteHandler> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData::new(),
handler: self.handler.clone(), handler: self.handler.clone(),
} }
} }

View File

@@ -1,4 +1,3 @@
use std::marker::PhantomData;
use std::sync::Arc; use std::sync::Arc;
use clap::{ArgMatches, CommandFactory, FromArgMatches}; use clap::{ArgMatches, CommandFactory, FromArgMatches};
@@ -9,7 +8,7 @@ use serde::de::DeserializeOwned;
use serde::ser::Serialize; use serde::ser::Serialize;
use yajrc::RpcError; use yajrc::RpcError;
use crate::util::{extract, Flat}; use crate::util::{extract, Flat, PhantomData};
/// Stores a command's implementation for a given context /// Stores a command's implementation for a given context
/// Can be created from anything that implements ParentCommand, AsyncCommand, or SyncCommand /// Can be created from anything that implements ParentCommand, AsyncCommand, or SyncCommand

View File

@@ -1,7 +1,6 @@
use std::any::TypeId; use std::any::TypeId;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::fmt::Debug; use std::fmt::Debug;
use std::marker::PhantomData;
use std::sync::Arc; use std::sync::Arc;
use clap::{CommandFactory, FromArgMatches}; use clap::{CommandFactory, FromArgMatches};
@@ -11,7 +10,7 @@ use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
use yajrc::RpcError; use yajrc::RpcError;
use crate::util::{internal_error, parse_error, Flat}; use crate::util::{internal_error, parse_error, Flat, PhantomData};
use crate::{ use crate::{
iter_from_ctx_and_handler, AnyContext, AnyHandler, CallRemote, CliBindings, DynHandler, iter_from_ctx_and_handler, AnyContext, AnyHandler, CallRemote, CliBindings, DynHandler,
EitherContext, HandleArgs, Handler, HandlerTypes, IntoContext, IntoHandlers, PrintCliResult, EitherContext, HandleArgs, Handler, HandlerTypes, IntoContext, IntoHandlers, PrintCliResult,
@@ -72,7 +71,7 @@ impl<T: Handler + Sized> HandlerExt for T {
F: Fn(HandleArgs<Context, Self>, Self::Ok) -> Result<(), Self::Err>, F: Fn(HandleArgs<Context, Self>, Self::Ok) -> Result<(), Self::Err>,
{ {
CustomDisplayFn { CustomDisplayFn {
_phantom: PhantomData, _phantom: PhantomData::new(),
print: display, print: display,
handler: self, handler: self,
} }
@@ -85,14 +84,14 @@ impl<T: Handler + Sized> HandlerExt for T {
F: Fn(Params, InheritedParams) -> Self::InheritedParams, F: Fn(Params, InheritedParams) -> Self::InheritedParams,
{ {
InheritanceHandler { InheritanceHandler {
_phantom: PhantomData, _phantom: PhantomData::new(),
handler: self, handler: self,
inherit: f, inherit: f,
} }
} }
fn with_remote_cli<Context>(self) -> RemoteCli<Context, Self> { fn with_remote_cli<Context>(self) -> RemoteCli<Context, Self> {
RemoteCli { RemoteCli {
_phantom: PhantomData, _phantom: PhantomData::new(),
handler: self, handler: self,
} }
} }
@@ -366,7 +365,7 @@ pub struct CustomDisplayFn<Context, F, H> {
impl<Context, F: Clone, H: Clone> Clone for CustomDisplayFn<Context, F, H> { impl<Context, F: Clone, H: Clone> Clone for CustomDisplayFn<Context, F, H> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData::new(),
print: self.print.clone(), print: self.print.clone(),
handler: self.handler.clone(), handler: self.handler.clone(),
} }
@@ -508,7 +507,7 @@ pub struct RemoteCli<Context, H> {
impl<Context, H: Clone> Clone for RemoteCli<Context, H> { impl<Context, H: Clone> Clone for RemoteCli<Context, H> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData::new(),
handler: self.handler.clone(), handler: self.handler.clone(),
} }
} }
@@ -645,7 +644,7 @@ impl<Params, InheritedParams, H: Clone, F: Clone> Clone
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData::new(),
handler: self.handler.clone(), handler: self.handler.clone(),
inherit: self.inherit.clone(), inherit: self.inherit.clone(),
} }

View File

@@ -1,7 +1,6 @@
use std::any::TypeId; use std::any::TypeId;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::fmt::Display; use std::fmt::Display;
use std::marker::PhantomData;
use clap::{ArgMatches, Command, CommandFactory, FromArgMatches}; use clap::{ArgMatches, Command, CommandFactory, FromArgMatches};
use futures::Future; use futures::Future;
@@ -10,7 +9,7 @@ use imbl_value::Value;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
use crate::marker::LeafHandler; use crate::util::PhantomData;
use crate::{ use crate::{
AnyContext, CliBindings, Empty, HandleArgs, Handler, HandlerTypes, IntoContext, PrintCliResult, AnyContext, CliBindings, Empty, HandleArgs, Handler, HandlerTypes, IntoContext, PrintCliResult,
}; };
@@ -30,7 +29,7 @@ impl<F, T, E, Args> FromFn<F, T, E, Args> {
impl<F: Clone, T, E, Args> Clone for FromFn<F, T, E, Args> { impl<F: Clone, T, E, Args> Clone for FromFn<F, T, E, Args> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData::new(),
function: self.function.clone(), function: self.function.clone(),
blocking: self.blocking, blocking: self.blocking,
metadata: self.metadata.clone(), metadata: self.metadata.clone(),
@@ -44,7 +43,6 @@ impl<F, T, E, Args> std::fmt::Debug for FromFn<F, T, E, Args> {
.finish() .finish()
} }
} }
impl<F, T, E, Args> LeafHandler for FromFn<F, T, E, Args> {}
impl<F, T, E, Args> PrintCliResult for FromFn<F, T, E, Args> impl<F, T, E, Args> PrintCliResult for FromFn<F, T, E, Args>
where where
Self: HandlerTypes, Self: HandlerTypes,
@@ -62,7 +60,7 @@ where
{ {
FromFn { FromFn {
function, function,
_phantom: PhantomData, _phantom: PhantomData::new(),
blocking: false, blocking: false,
metadata: OrdMap::new(), metadata: OrdMap::new(),
} }
@@ -74,7 +72,7 @@ where
{ {
FromFn { FromFn {
function, function,
_phantom: PhantomData, _phantom: PhantomData::new(),
blocking: true, blocking: true,
metadata: OrdMap::new(), metadata: OrdMap::new(),
} }
@@ -85,10 +83,22 @@ pub struct FromFnAsync<F, Fut, T, E, Args> {
function: F, function: F,
metadata: OrdMap<&'static str, Value>, metadata: OrdMap<&'static str, Value>,
} }
unsafe impl<F, Fut, T, E, Args> Send for FromFnAsync<F, Fut, T, E, Args>
where
F: Send,
OrdMap<&'static str, Value>: Send,
{
}
unsafe impl<F, Fut, T, E, Args> Sync for FromFnAsync<F, Fut, T, E, Args>
where
F: Sync,
OrdMap<&'static str, Value>: Sync,
{
}
impl<F: Clone, Fut, T, E, Args> Clone for FromFnAsync<F, Fut, T, E, Args> { impl<F: Clone, Fut, T, E, Args> Clone for FromFnAsync<F, Fut, T, E, Args> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData::new(),
function: self.function.clone(), function: self.function.clone(),
metadata: self.metadata.clone(), metadata: self.metadata.clone(),
} }
@@ -116,7 +126,7 @@ where
{ {
FromFnAsync { FromFnAsync {
function, function,
_phantom: PhantomData, _phantom: PhantomData::new(),
metadata: OrdMap::new(), metadata: OrdMap::new(),
} }
} }
@@ -160,7 +170,7 @@ where
impl<F, Fut, T, E> HandlerTypes for FromFnAsync<F, Fut, T, E, ()> impl<F, Fut, T, E> HandlerTypes for FromFnAsync<F, Fut, T, E, ()>
where where
F: Fn() -> Fut + Send + Sync + Clone + 'static, F: Fn() -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = Result<T, E>> + Send + Sync + 'static, Fut: Future<Output = Result<T, E>> + Send + 'static,
T: Send + Sync + 'static, T: Send + Sync + 'static,
E: Send + Sync + 'static, E: Send + Sync + 'static,
{ {
@@ -173,7 +183,7 @@ where
impl<F, Fut, T, E> Handler for FromFnAsync<F, Fut, T, E, ()> impl<F, Fut, T, E> Handler for FromFnAsync<F, Fut, T, E, ()>
where where
F: Fn() -> Fut + Send + Sync + Clone + 'static, F: Fn() -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = Result<T, E>> + Send + Sync + 'static, Fut: Future<Output = Result<T, E>> + Send + 'static,
T: Send + Sync + 'static, T: Send + Sync + 'static,
E: Send + Sync + 'static, E: Send + Sync + 'static,
{ {
@@ -234,7 +244,7 @@ impl<Context, F, Fut, T, E> HandlerTypes for FromFnAsync<F, Fut, T, E, (Context,
where where
Context: IntoContext, Context: IntoContext,
F: Fn(Context) -> Fut + Send + Sync + Clone + 'static, F: Fn(Context) -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = Result<T, E>> + Send + Sync + 'static, Fut: Future<Output = Result<T, E>> + Send + 'static,
T: Send + Sync + 'static, T: Send + Sync + 'static,
E: Send + Sync + 'static, E: Send + Sync + 'static,
{ {
@@ -248,7 +258,7 @@ impl<Context, F, Fut, T, E> Handler for FromFnAsync<F, Fut, T, E, (Context,)>
where where
Context: IntoContext, Context: IntoContext,
F: Fn(Context) -> Fut + Send + Sync + Clone + 'static, F: Fn(Context) -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = Result<T, E>> + Send + Sync + 'static, Fut: Future<Output = Result<T, E>> + Send + 'static,
T: Send + Sync + 'static, T: Send + Sync + 'static,
E: Send + Sync + 'static, E: Send + Sync + 'static,
{ {
@@ -314,7 +324,7 @@ impl<Context, F, Fut, T, E, Params> HandlerTypes for FromFnAsync<F, Fut, T, E, (
where where
Context: IntoContext, Context: IntoContext,
F: Fn(Context, Params) -> Fut + Send + Sync + Clone + 'static, F: Fn(Context, Params) -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = Result<T, E>> + Send + Sync + 'static, Fut: Future<Output = Result<T, E>> + Send + 'static,
Params: DeserializeOwned + Send + Sync + 'static, Params: DeserializeOwned + Send + Sync + 'static,
T: Send + Sync + 'static, T: Send + Sync + 'static,
E: Send + Sync + 'static, E: Send + Sync + 'static,
@@ -329,7 +339,7 @@ impl<Context, F, Fut, T, E, Params> Handler for FromFnAsync<F, Fut, T, E, (Conte
where where
Context: IntoContext, Context: IntoContext,
F: Fn(Context, Params) -> Fut + Send + Sync + Clone + 'static, F: Fn(Context, Params) -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = Result<T, E>> + Send + Sync + 'static, Fut: Future<Output = Result<T, E>> + Send + 'static,
Params: DeserializeOwned + Send + Sync + 'static, Params: DeserializeOwned + Send + Sync + 'static,
T: Send + Sync + 'static, T: Send + Sync + 'static,
E: Send + Sync + 'static, E: Send + Sync + 'static,
@@ -407,7 +417,7 @@ impl<Context, F, Fut, T, E, Params, InheritedParams> HandlerTypes
where where
Context: IntoContext, Context: IntoContext,
F: Fn(Context, Params, InheritedParams) -> Fut + Send + Sync + Clone + 'static, F: Fn(Context, Params, InheritedParams) -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = Result<T, E>> + Send + Sync + 'static, Fut: Future<Output = Result<T, E>> + Send + 'static,
Params: DeserializeOwned + Send + Sync + 'static, Params: DeserializeOwned + Send + Sync + 'static,
InheritedParams: DeserializeOwned + Send + Sync + 'static, InheritedParams: DeserializeOwned + Send + Sync + 'static,
T: Send + Sync + 'static, T: Send + Sync + 'static,
@@ -424,7 +434,7 @@ impl<Context, F, Fut, T, E, Params, InheritedParams> Handler
where where
Context: IntoContext, Context: IntoContext,
F: Fn(Context, Params, InheritedParams) -> Fut + Send + Sync + Clone + 'static, F: Fn(Context, Params, InheritedParams) -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = Result<T, E>> + Send + Sync + 'static, Fut: Future<Output = Result<T, E>> + Send + 'static,
Params: DeserializeOwned + Send + Sync + 'static, Params: DeserializeOwned + Send + Sync + 'static,
InheritedParams: DeserializeOwned + Send + Sync + 'static, InheritedParams: DeserializeOwned + Send + Sync + 'static,
T: Send + Sync + 'static, T: Send + Sync + 'static,

View File

@@ -1 +0,0 @@
pub trait LeafHandler {}

View File

@@ -15,7 +15,6 @@ use crate::util::{internal_error, invalid_params, Flat};
pub mod adapters; pub mod adapters;
pub mod from_fn; pub mod from_fn;
pub mod marker;
pub mod parent; pub mod parent;
pub use adapters::*; pub use adapters::*;

View File

@@ -1,6 +1,5 @@
use std::any::TypeId; use std::any::TypeId;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::marker::PhantomData;
use std::sync::Arc; use std::sync::Arc;
use clap::{ArgMatches, Command, CommandFactory, FromArgMatches}; use clap::{ArgMatches, Command, CommandFactory, FromArgMatches};
@@ -10,7 +9,7 @@ use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
use yajrc::RpcError; use yajrc::RpcError;
use crate::util::{combine, Flat}; use crate::util::{combine, Flat, PhantomData};
use crate::{ use crate::{
AnyContext, AnyHandler, CliBindings, DynHandler, Empty, HandleAny, HandleAnyArgs, HandleArgs, AnyContext, AnyHandler, CliBindings, DynHandler, Empty, HandleAny, HandleAnyArgs, HandleArgs,
Handler, HandlerTypes, IntoContext, OrEmpty, Handler, HandlerTypes, IntoContext, OrEmpty,
@@ -104,7 +103,7 @@ pub struct ParentHandler<Params = Empty, InheritedParams = Empty> {
impl<Params, InheritedParams> ParentHandler<Params, InheritedParams> { impl<Params, InheritedParams> ParentHandler<Params, InheritedParams> {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData::new(),
subcommands: SubcommandMap(OrdMap::new()), subcommands: SubcommandMap(OrdMap::new()),
metadata: OrdMap::new(), metadata: OrdMap::new(),
} }
@@ -117,7 +116,7 @@ impl<Params, InheritedParams> ParentHandler<Params, InheritedParams> {
impl<Params, InheritedParams> Clone for ParentHandler<Params, InheritedParams> { impl<Params, InheritedParams> Clone for ParentHandler<Params, InheritedParams> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData::new(),
subcommands: self.subcommands.clone(), subcommands: self.subcommands.clone(),
metadata: self.metadata.clone(), metadata: self.metadata.clone(),
} }

View File

@@ -1,4 +1,4 @@
use std::fmt::Display; use std::fmt::{Debug, Display};
use futures::future::{BoxFuture, FusedFuture}; use futures::future::{BoxFuture, FusedFuture};
use futures::stream::FusedStream; use futures::stream::FusedStream;
@@ -222,16 +222,21 @@ where
} }
} }
// #[derive(Debug)] pub struct PhantomData<T>(std::marker::PhantomData<T>);
// pub enum Infallible {} impl<T> PhantomData<T> {
// impl<T> From<Infallible> for T { pub fn new() -> Self {
// fn from(value: Infallible) -> Self { PhantomData(std::marker::PhantomData)
// match value {} }
// } }
// } impl<T> Clone for PhantomData<T> {
// impl std::fmt::Display for Infallible { fn clone(&self) -> Self {
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { PhantomData::new()
// match *self {} }
// } }
// } impl<T> Debug for PhantomData<T> {
// impl std::error::Error for Infallible {} fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
unsafe impl<T> Send for PhantomData<T> {}
unsafe impl<T> Sync for PhantomData<T> {}