#![cfg_attr(not(feature = "std"), no_std)] use std::marker::PhantomData; use merfolk::{ interfaces::{Backend, Frontend}, Call, Reply, }; use anyhow::Result; use log::trace; #[derive(derive_builder::Builder)] #[builder(pattern = "owned")] #[cfg_attr(not(feature = "std"), builder(no_std))] pub struct Duplex<'a, B, FC, FR> where B: Backend + 'a, FC: Frontend + 'a, FR: Frontend + 'a, { #[builder(private, default = "PhantomData")] __phantom: PhantomData<&'a B>, pub caller: FC, pub receiver: FR, } impl<'a, B, FC, FR> Duplex<'a, B, FC, FR> where B: Backend + 'a, FC: Frontend + 'a, FR: Frontend + 'a, { pub fn builder() -> DuplexBuilder<'a, B, FC, FR> { DuplexBuilder::default() } } unsafe impl<'a, B, FC, FR> Send for Duplex<'a, B, FC, FR> where B: Backend, FC: Frontend, FR: Frontend, { } impl<'a, B, FC, FR> Frontend for Duplex<'a, B, FC, FR> where B: Backend, FC: Frontend, FR: Frontend, { type Backend = B; fn register(&mut self, caller: T) -> Result<()> where T: Fn(Call<::Intermediate>) -> Result::Intermediate>> + 'static + Send + Sync, { trace!("duplex caller"); self.caller.register(caller) } #[allow(clippy::type_complexity)] fn receive(&self, call: Call<::Intermediate>) -> Result::Intermediate>> { trace!("receive call"); self.receiver.receive(call) } }