From dba27b4402648b6a9272cda681ab8caaeb5d279d Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 25 Oct 2022 21:51:46 +0200 Subject: refactor: add Debug implementations for castable factories --- src/castable_factory/blocking.rs | 33 +++++++++++++++++++++++++++++++++ src/castable_factory/threadsafe.rs | 36 ++++++++++++++++++++++++++++++++++++ src/interfaces/any_factory.rs | 20 ++------------------ src/provider/async.rs | 1 + src/provider/blocking.rs | 1 + 5 files changed, 73 insertions(+), 18 deletions(-) diff --git a/src/castable_factory/blocking.rs b/src/castable_factory/blocking.rs index 9ef9864..48f426d 100644 --- a/src/castable_factory/blocking.rs +++ b/src/castable_factory/blocking.rs @@ -1,3 +1,6 @@ +use std::any::type_name; +use std::fmt::Debug; + use crate::interfaces::any_factory::AnyFactory; use crate::interfaces::factory::IFactory; use crate::ptr::TransientPtr; @@ -73,6 +76,36 @@ where { } +impl Debug for CastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + #[cfg(not(tarpaulin_include))] + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + let mut args = type_name::(); + + if args.len() < 2 { + return Err(std::fmt::Error::default()); + } + + args = args + .get(1..args.len() - 1) + .map_or_else(|| Err(std::fmt::Error::default()), Ok)?; + + if args.ends_with(',') { + args = args + .get(..args.len() - 1) + .map_or_else(|| Err(std::fmt::Error), Ok)?; + } + + let ret = type_name::>(); + + formatter.write_fmt(format_args!("CastableFactory ({}) -> {}", args, ret)) + } +} + #[cfg(test)] mod tests { diff --git a/src/castable_factory/threadsafe.rs b/src/castable_factory/threadsafe.rs index 8f9c072..c1a90c4 100644 --- a/src/castable_factory/threadsafe.rs +++ b/src/castable_factory/threadsafe.rs @@ -1,3 +1,6 @@ +use std::any::type_name; +use std::fmt::Debug; + use crate::interfaces::any_factory::{AnyFactory, AnyThreadsafeFactory}; use crate::interfaces::factory::IThreadsafeFactory; use crate::ptr::TransientPtr; @@ -86,6 +89,39 @@ where { } +impl Debug for ThreadsafeCastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + #[cfg(not(tarpaulin_include))] + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + let mut args = type_name::(); + + if args.len() < 2 { + return Err(std::fmt::Error::default()); + } + + args = args + .get(1..args.len() - 1) + .map_or_else(|| Err(std::fmt::Error::default()), Ok)?; + + if args.ends_with(',') { + args = args + .get(..args.len() - 1) + .map_or_else(|| Err(std::fmt::Error), Ok)?; + } + + let ret = type_name::>(); + + formatter.write_fmt(format_args!( + "ThreadsafeCastableFactory ({}) -> {}", + args, ret + )) + } +} + #[cfg(test)] mod tests { diff --git a/src/interfaces/any_factory.rs b/src/interfaces/any_factory.rs index 1bf9208..e47018b 100644 --- a/src/interfaces/any_factory.rs +++ b/src/interfaces/any_factory.rs @@ -5,23 +5,7 @@ use std::fmt::Debug; use crate::libs::intertrait::{CastFrom, CastFromSync}; /// Interface for any factory to ever exist. -pub trait AnyFactory: CastFrom {} - -impl Debug for dyn AnyFactory -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result - { - f.write_str("{}") - } -} +pub trait AnyFactory: CastFrom + Debug {} /// Interface for any threadsafe factory to ever exist. -pub trait AnyThreadsafeFactory: CastFromSync {} - -impl Debug for dyn AnyThreadsafeFactory -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result - { - f.write_str("{}") - } -} +pub trait AnyThreadsafeFactory: CastFromSync + Debug {} diff --git a/src/provider/async.rs b/src/provider/async.rs index 53cadc8..8d482cd 100644 --- a/src/provider/async.rs +++ b/src/provider/async.rs @@ -320,6 +320,7 @@ mod tests use crate::interfaces::any_factory::AnyThreadsafeFactory; use crate::ptr::ThreadsafeFactoryPtr; + #[derive(Debug)] struct FooFactory; impl AnyThreadsafeFactory for FooFactory {} diff --git a/src/provider/blocking.rs b/src/provider/blocking.rs index 3de77e4..e1e2aad 100644 --- a/src/provider/blocking.rs +++ b/src/provider/blocking.rs @@ -214,6 +214,7 @@ mod tests use crate::interfaces::any_factory::AnyFactory; use crate::ptr::FactoryPtr; + #[derive(Debug)] struct FooFactory; impl AnyFactory for FooFactory {} -- cgit v1.2.3-18-g5258