use rosidl_runtime_rs::Service;
use crate::{AnyServiceCallback, RequestId, ServiceInfo, Worker, WorkerServiceCallback};
pub trait IntoWorkerServiceCallback<T, Payload, Args>: Send + 'static
where
T: Service,
Payload: 'static + Send,
{
fn into_worker_service_callback(self) -> AnyServiceCallback<T, Payload>;
}
impl<T, Payload, Func> IntoWorkerServiceCallback<T, Payload, ()> for Func
where
T: Service,
Payload: 'static + Send,
Func: FnMut(T::Request) -> T::Response + Send + 'static,
{
fn into_worker_service_callback(mut self) -> AnyServiceCallback<T, Payload> {
let f = Box::new(move |_: &mut Payload, request| self(request));
WorkerServiceCallback::OnlyRequest(f).into()
}
}
impl<T, Payload, Func> IntoWorkerServiceCallback<T, Payload, Worker<Payload>> for Func
where
T: Service,
Payload: 'static + Send,
Func: FnMut(&mut Payload, T::Request) -> T::Response + Send + 'static,
{
fn into_worker_service_callback(self) -> AnyServiceCallback<T, Payload> {
WorkerServiceCallback::OnlyRequest(Box::new(self)).into()
}
}
impl<T, Payload, Func> IntoWorkerServiceCallback<T, Payload, RequestId> for Func
where
T: Service,
Payload: 'static + Send,
Func: FnMut(T::Request, RequestId) -> T::Response + Send + 'static,
{
fn into_worker_service_callback(mut self) -> AnyServiceCallback<T, Payload> {
let f = Box::new(move |_: &mut Payload, request, request_id| self(request, request_id));
WorkerServiceCallback::WithId(f).into()
}
}
impl<T, Payload, Func> IntoWorkerServiceCallback<T, Payload, (Worker<Payload>, RequestId)> for Func
where
T: Service,
Payload: 'static + Send,
Func: FnMut(&mut Payload, T::Request, RequestId) -> T::Response + Send + 'static,
{
fn into_worker_service_callback(self) -> AnyServiceCallback<T, Payload> {
WorkerServiceCallback::WithId(Box::new(self)).into()
}
}
impl<T, Payload, Func> IntoWorkerServiceCallback<T, Payload, ServiceInfo> for Func
where
T: Service,
Payload: 'static + Send,
Func: FnMut(T::Request, ServiceInfo) -> T::Response + Send + 'static,
{
fn into_worker_service_callback(mut self) -> AnyServiceCallback<T, Payload> {
let f = Box::new(move |_: &mut Payload, request, info| self(request, info));
WorkerServiceCallback::WithInfo(f).into()
}
}
impl<T, Payload, Func> IntoWorkerServiceCallback<T, Payload, (Worker<T>, ServiceInfo)> for Func
where
T: Service,
Payload: 'static + Send,
Func: FnMut(&mut Payload, T::Request, ServiceInfo) -> T::Response + Send + 'static,
{
fn into_worker_service_callback(self) -> AnyServiceCallback<T, Payload> {
WorkerServiceCallback::WithInfo(Box::new(self)).into()
}
}