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::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<Context: crate::Context + Clone, Config: CommandFactory + FromArgMatches>
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<Context, RemoteHandler> {
impl<Context, RemoteHandler> CallRemoteHandler<Context, RemoteHandler> {
pub fn new(handler: RemoteHandler) -> Self {
Self {
_phantom: PhantomData,
_phantom: PhantomData::new(),
handler: handler,
}
}
@@ -180,7 +179,7 @@ impl<Context, RemoteHandler> CallRemoteHandler<Context, RemoteHandler> {
impl<Context, RemoteHandler: Clone> Clone for CallRemoteHandler<Context, RemoteHandler> {
fn clone(&self) -> Self {
Self {
_phantom: PhantomData,
_phantom: PhantomData::new(),
handler: self.handler.clone(),
}
}

View File

@@ -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

View File

@@ -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<T: Handler + Sized> HandlerExt for T {
F: Fn(HandleArgs<Context, Self>, Self::Ok) -> Result<(), Self::Err>,
{
CustomDisplayFn {
_phantom: PhantomData,
_phantom: PhantomData::new(),
print: display,
handler: self,
}
@@ -85,14 +84,14 @@ impl<T: Handler + Sized> HandlerExt for T {
F: Fn(Params, InheritedParams) -> Self::InheritedParams,
{
InheritanceHandler {
_phantom: PhantomData,
_phantom: PhantomData::new(),
handler: self,
inherit: f,
}
}
fn with_remote_cli<Context>(self) -> RemoteCli<Context, Self> {
RemoteCli {
_phantom: PhantomData,
_phantom: PhantomData::new(),
handler: self,
}
}
@@ -366,7 +365,7 @@ pub struct CustomDisplayFn<Context, F, H> {
impl<Context, F: Clone, H: Clone> Clone for CustomDisplayFn<Context, F, H> {
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<Context, H> {
impl<Context, H: Clone> Clone for RemoteCli<Context, H> {
fn clone(&self) -> Self {
Self {
_phantom: PhantomData,
_phantom: PhantomData::new(),
handler: self.handler.clone(),
}
}
@@ -645,7 +644,7 @@ impl<Params, InheritedParams, H: Clone, F: Clone> Clone
{
fn clone(&self) -> Self {
Self {
_phantom: PhantomData,
_phantom: PhantomData::new(),
handler: self.handler.clone(),
inherit: self.inherit.clone(),
}

View File

@@ -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<F, T, E, Args> FromFn<F, T, E, Args> {
impl<F: Clone, T, E, Args> Clone for FromFn<F, T, E, Args> {
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<F, T, E, Args> std::fmt::Debug for FromFn<F, T, E, Args> {
.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>
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<F, Fut, T, E, Args> {
function: F,
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> {
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<F, Fut, T, E> HandlerTypes for FromFnAsync<F, Fut, T, E, ()>
where
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,
E: Send + Sync + 'static,
{
@@ -173,7 +183,7 @@ where
impl<F, Fut, T, E> Handler for FromFnAsync<F, Fut, T, E, ()>
where
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,
E: Send + Sync + 'static,
{
@@ -234,7 +244,7 @@ impl<Context, F, Fut, T, E> HandlerTypes for FromFnAsync<F, Fut, T, E, (Context,
where
Context: IntoContext,
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,
E: Send + Sync + 'static,
{
@@ -248,7 +258,7 @@ impl<Context, F, Fut, T, E> Handler for FromFnAsync<F, Fut, T, E, (Context,)>
where
Context: IntoContext,
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,
E: Send + Sync + 'static,
{
@@ -314,7 +324,7 @@ impl<Context, F, Fut, T, E, Params> HandlerTypes for FromFnAsync<F, Fut, T, E, (
where
Context: IntoContext,
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,
T: 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
Context: IntoContext,
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,
T: Send + Sync + 'static,
E: Send + Sync + 'static,
@@ -407,7 +417,7 @@ impl<Context, F, Fut, T, E, Params, InheritedParams> HandlerTypes
where
Context: IntoContext,
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,
InheritedParams: DeserializeOwned + Send + Sync + 'static,
T: Send + Sync + 'static,
@@ -424,7 +434,7 @@ impl<Context, F, Fut, T, E, Params, InheritedParams> Handler
where
Context: IntoContext,
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,
InheritedParams: DeserializeOwned + 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 from_fn;
pub mod marker;
pub mod parent;
pub use adapters::*;

View File

@@ -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<Params = Empty, InheritedParams = Empty> {
impl<Params, InheritedParams> ParentHandler<Params, InheritedParams> {
pub fn new() -> Self {
Self {
_phantom: PhantomData,
_phantom: PhantomData::new(),
subcommands: SubcommandMap(OrdMap::new()),
metadata: OrdMap::new(),
}
@@ -117,7 +116,7 @@ impl<Params, InheritedParams> ParentHandler<Params, InheritedParams> {
impl<Params, InheritedParams> Clone for ParentHandler<Params, InheritedParams> {
fn clone(&self) -> Self {
Self {
_phantom: PhantomData,
_phantom: PhantomData::new(),
subcommands: self.subcommands.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::stream::FusedStream;
@@ -222,16 +222,21 @@ where
}
}
// #[derive(Debug)]
// pub enum Infallible {}
// impl<T> From<Infallible> 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<T>(std::marker::PhantomData<T>);
impl<T> PhantomData<T> {
pub fn new() -> Self {
PhantomData(std::marker::PhantomData)
}
}
impl<T> Clone for PhantomData<T> {
fn clone(&self) -> Self {
PhantomData::new()
}
}
impl<T> Debug for PhantomData<T> {
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> {}